-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Labels
2.18Issues planned at 2.18 or laterIssues planned at 2.18 or later
Milestone
Description
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
I have this class:
public class Foo {
private String id;
private String name;
public Foo() {
}
public Foo(String id) {
this.id = id;
}
// getter setter omitted
} I use ConstructorDetector.USE_PROPERTIES_BASED configuration.
JsonMapper objectMapper = JsonMapper.builder().constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED).build();Since Jackson 2.18.0 I am no able to deserialize any JSON to this object. It worked in previous versions.
The error is:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Invalid type definition for type `com.example.Foo`: Argument #0 of Creator [constructor for `com.example.Foo` (1 arg), annotations: [null] has no property name (and is not Injectable): can not use as property-based Creator
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:62)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadTypeDefinition(DeserializationContext.java:1866)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._addSelectedPropertiesBasedCreator(BasicDeserializerFactory.java:549)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:269)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:219)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:262)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:151)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:471)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:415)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:317)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:284)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:174)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:669)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:5048)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4918)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3828)
It does not deserialize these JSONs
{}
{"id": "something"}
{"id": "something", "name": "name"}
"something"I would expect that it will work at least for {} (no args constructor) and {"id": "something"} (1 arg constructor with the same parameter name).
Version Information
2.18.0, 2.18.1 and 2.18.2 is affected
2.17.2 works
Reproduction
<-- Any of the following
- Brief code sample/snippet: include here in preformatted/code section
- Longer example stored somewhere else (diff repo, snippet), add a link
- Textual explanation: include here
-->
public class Foo {
private String id;
private String name;
public Foo() {
}
public Foo(String id) {
this.id = id;
}
// getter setter omitted
public static void main(String[] args) {
// Test
JsonMapper objectMapper = JsonMapper.builder()
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED).build();
Foo foo = objectMapper.readValue("{}", Foo.class);
}
}Expected behavior
I would expect that it will deserialize JSON instead of throwing an exception.
Additional context
The object for deserialization is an external class and I cannot change it. I can use mixin to override it but I am afraid that this issue can happen with other external objects only if someone adds two constructors.s
Metadata
Metadata
Assignees
Labels
2.18Issues planned at 2.18 or laterIssues planned at 2.18 or later