jackson-databind: Deserialization using `@JsonCreator` with renamed property failing (since 2.18)
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
Hello,
Since v2.18, some of our data classes failed to be used for deserialization.
Those classes are making use of @JsonProperty et @JsonCreator annotations, and rely on the ParameterNamesModule.
Version Information
2.18.1
Reproduction
Example reproducing the issue:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.google.auto.value.AutoValue;
import org.junit.jupiter.api.Test;
import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
public final class MappingIssueTest {
@AutoValue
public static abstract class DataClassWithPropRenaming {
@JsonProperty("bar")
public abstract String getFoo();
@JsonCreator
public static DataClassWithPropRenaming create(String bar) {
return new AutoValue_MappingIssueTest_DataClassWithPropRenaming(bar);
}
}
@Test
void shouldSupportPropertyRenaming() throws Exception {
var mapper = JsonMapper.builder()
.addModule(new ParameterNamesModule())
.build();
var serializationResult = mapper.valueToTree(DataClassWithPropRenaming.create("42"));
assertThat(newHashSet(serializationResult.fieldNames()))
.containsExactlyInAnyOrder("bar");
var deserializationResult = mapper.treeToValue(serializationResult, DataClassWithPropRenaming.class);
assertThat(deserializationResult)
.isEqualTo(DataClassWithPropRenaming.create("42"));
}
}
Fails with the following error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `MappingIssueTest$DataClassWithPropRenaming` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: UNKNOWN; byte offset: #UNKNOWN]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1754)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1379)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1512)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4893)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3036)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:3500)
Expected behavior
We would expect that the @JsonCreator method parameter name be used to map the field having the same name in the JsonNode.
Additional context
Adding a @JsonProperty("bar") on the creator parameter solves the issue.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 16 (10 by maintainers)
Commits related to this issue
- Fix #4810: heuristics should detect correct mode for creator — committed to FasterXML/jackson-databind by cowtowncoder 2 years ago
- Fix #4810: heuristics should detect correct mode for creator (#4811) — committed to FasterXML/jackson-databind by cowtowncoder 2 years ago
Yes, but I forgot to pass the required
-parameterscompiler option… 🤦♂️Sorry folks, everything works great with the 2.18.2 release!
Thanks again @cowtowncoder
My attempt was made using a local build of the sources containing the patch.
Thanks @cowtowncoder!
Should work - I tested with 2.18.2-SNAPSHOT (generated
Wed Nov 13 03:09:24 UTC 2024) from https://oss.sonatype.org/content/repositories/snapshots.Give it another try?
Eh sorry I meant
PROPERTIESnotDELEGATING, updated that incorrect comment. 😅