Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOML - PropertyNamingStrategies with Record, Jackson doesn't find fields #336

Closed
tomasff opened this issue Sep 17, 2022 · 4 comments
Closed
Labels
TOML Issue related to TOML format backend

Comments

@tomasff
Copy link

tomasff commented Sep 17, 2022

When attempting to deserialize a TOML file into a record with the ObjectMapper set to use the snake_case strategy, Jackson (2.13.4) fails to set the fields.

// Config.java
public record Config(int port, int gossipDelay, String initialValue, List<Peer> peers) {
    public Config {
        Objects.requireNonNull(peers);
        Objects.requireNonNull(initialValue);
    }
}

// Attempting to deserialize
ObjectMapper objectMapper = new TomlMapper()
            .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);

objectMapper.readValue(new File("config.toml"), Config.class);

config.toml

port = 3200
gossip_delay = 10
initial_value = "I'm not sus"

peers = [
    { addr = "127.0.0.1", port = 3201 },
]

Logs when attempting to run this:

Exception in thread "main" com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.tomff.pheme.Config`, problem: `java.lang.NullPointerException`
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
	at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47)
	at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:2047)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:587)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:610)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:293)
	at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:288)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:202)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:519)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1405)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:352)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3494)
	at com.tomff.pheme.PhemeMain.loadConfig(PhemeMain.java:64)
	at com.tomff.pheme.PhemeMain.main(PhemeMain.java:69)
Caused by: java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:208)
	at com.tomff.pheme.Config.<init>(Config.java:9)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:128)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:291)
	... 11 more

Process finished with exit code 1
@tomasff tomasff changed the title TOML - PropertyNamingStrategies with Record TOML - PropertyNamingStrategies with Record, property naming strategy doesn't find fields Sep 17, 2022
@tomasff tomasff changed the title TOML - PropertyNamingStrategies with Record, property naming strategy doesn't find fields TOML - PropertyNamingStrategies with Record, Jackson doesn't find fields Sep 17, 2022
@cowtowncoder
Copy link
Member

I think it is likely this is not specific to TOML module but would also occur with JSON.

@cowtowncoder cowtowncoder added the TOML Issue related to TOML format backend label Sep 18, 2022
@yawkat
Copy link
Member

yawkat commented Sep 19, 2022

probably same as FasterXML/jackson-databind#3102 ?

@cowtowncoder
Copy link
Member

Yes, duplicate of that one. If and when that gets fixed (requires a rewrite of property introspection, alas, but there's hope this could be done for 2.15) this would also be resolved. TOML module itself does not implement functionality, it just exposes token streams.

@yawkat
Copy link
Member

yawkat commented Feb 28, 2023

closing as fixed by FasterXML/jackson-databind#3102

@yawkat yawkat closed this as completed Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TOML Issue related to TOML format backend
Projects
None yet
Development

No branches or pull requests

3 participants