-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Introspection includes delegating ctor's only parameter as a property in BeanDescription
#2543
Comments
Hmmmh. If I understand description correctly, yes, creator method should be detected for deserialization purposes. But handling of Creator methods is not done by Given this, I think that:
Does this make sense? So, basically, there may be a problem, but that would need to be shown end-to-end, and not at level of |
One other note: also keep in mind that properties may be needed for serialization, so even when no properties are used for deserialization (where Creators matter), it may be necessary to retain property information for serialization. I am not against pruning information to keep information more sensible for other uses, if possible: a reasonable test is to make sure none of tests fail with proposed changes (or if they fail there is a problem with tests to be fixed, uncovered by changes). |
BeanDescription
I recently encountered the same behaviour and it lead to the deserialization failure. You can find a reproducing test case here - https://github.com/kirmerzlikin/jackson-delegating-creator-property-bug/blob/master/src/test/java/com/example/NamesTest.java. Test case summarypublic class NamesTest {
private final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new ParameterNamesModule());
@Test
void testDeserialization() throws JsonProcessingException{
/*
* Deserialization fails with
* com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid definition for property 'fullName'
* (of type `com.example.NamesTest$Names`): Could not find creator property with name 'fullName'
* (known Creator properties: [firstName, lastName])
*/
Names names = objectMapper.readValue("{\"firstName\":\"John\",\"lastName\":\"Doe\"}", Names.class);
assertThat(names.getFirstName()).isEqualTo("John");
assertThat(names.getLastName()).isEqualTo("Doe");
}
/*
* Tests pass when following annotation is added
* @JsonIgnoreProperties("fullName")
*/
public static class Names {
private final String firstName;
private final String lastName;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Names(@JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static Names fromFullName(String fullName) {
String[] names = fullName.split("\\s+", 2);
return new Names(names[0], names[1]);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
} As far as I can understand, the reason for this failure is that Jackson considers a named argument of a delegating IMHO if it's a delegating creator, then its argument shouldn't be considered a property since it's supposed to be a single piece of data representing the whole object, from which the said object can be created. To achieve this, it would be enough to make |
@kirmerzlikin Yes, no named parameter should be detected for a constructor considered delegating one. Also: good detective work; your understanding is solid here about everything you mention. As to test: there are some tests in In the meantime I'll have a look at your fix: although properly Bean Property Introspection rewrite is needed (there's #3719 as sort of pre-req), to make Creator discovery occur before |
@kirmerzlikin I think your fix just might work; I can't think of an immediate problem. If you could come up with a reproduction using simple bogus |
Thanks for taking a look, @cowtowncoder. |
Thank you in advance @kirmerzlikin ! Along with PR, we'd need CLA (unless we already have one from you; it's one-time-only thing), from: https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf and that is usually easiest do by printing, filling & signing, scanning/photo, sending to Looking forward to getting this all in, thank you once again! |
Thank you @kirmerzlikin ! This went in and to be included in 2.17.0, release of which just started... |
If I have
ParameterNamesModule
and this data class:Then running
objectMapper.getSerializationConfig().introspect(/* Data type */);
will return aBeanDescription
that includesbuilder
as a property.This happens because with
ParameterNamesModule
we are able to infer the name of theJsonCreator
parameter here and when we are, we include this parameter in the properties.I think here we should be checking if the creator factory is a delegating kind that takes a complex value as an input. If maintainers of this repo agree, I will file a PR with the fix.
The text was updated successfully, but these errors were encountered: