-
-
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
BeanDeserializerModifier::updateBuilder shall work for immutable beans too (#4356) #4357
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/com/fasterxml/jackson/databind/module/DeserializerUpdateBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.fasterxml.jackson.databind.module; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder; | ||
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; | ||
import com.fasterxml.jackson.databind.deser.SettableBeanProperty; | ||
|
||
public class DeserializerUpdateBuilderTest extends BaseMapTest | ||
{ | ||
static class MutableBean { | ||
|
||
private String a; | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
|
||
public void setA(String a) { | ||
this.a = a; | ||
} | ||
} | ||
|
||
static class ImmutableBean { | ||
|
||
private final String a; | ||
|
||
@JsonCreator | ||
public ImmutableBean(@JsonProperty("a") String a) { | ||
this.a = a; | ||
} | ||
|
||
public String getA() { | ||
return a; | ||
} | ||
} | ||
|
||
@SuppressWarnings("serial") | ||
static SimpleModule getSimpleModuleWithDeserializerModifier() { | ||
return new SimpleModule().setDeserializerModifier(new BeanDeserializerModifier() { | ||
@Override | ||
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) { | ||
for (Iterator<SettableBeanProperty> properties = builder.getProperties(); properties.hasNext();) { | ||
SettableBeanProperty property = properties.next(); | ||
if (property.getName().equals("a")) { | ||
builder.addOrReplaceProperty(property.withValueDeserializer(new ConvertingStringDeserializer()), true); | ||
} | ||
} | ||
return builder; | ||
} | ||
}); | ||
} | ||
|
||
static final String CUSTOM_DESERIALIZER_VALUE = "Custom deserializer value"; | ||
|
||
static class ConvertingStringDeserializer extends JsonDeserializer<String> { | ||
|
||
@Override | ||
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { | ||
p.skipChildren(); | ||
return CUSTOM_DESERIALIZER_VALUE; | ||
} | ||
} | ||
|
||
final ObjectMapper objectMapper = jsonMapperBuilder().addModules(getSimpleModuleWithDeserializerModifier()).build(); | ||
|
||
// Succeeds | ||
public void testMutableBeanUpdateBuilder() throws JsonProcessingException { | ||
MutableBean recreatedBean = objectMapper.readValue("{\"a\": \"Some value\"}", MutableBean.class); | ||
|
||
assertEquals(CUSTOM_DESERIALIZER_VALUE, recreatedBean.getA()); | ||
} | ||
|
||
// Fails without fix | ||
public void testImmutableBeanUpdateBuilder() throws JsonProcessingException { | ||
ImmutableBean recreatedBean = objectMapper.readValue("{\"a\": \"Some value\"}", ImmutableBean.class); | ||
|
||
assertEquals(CUSTOM_DESERIALIZER_VALUE, recreatedBean.getA()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels wrong, as in it should not be necessary. But I'll need to spend some time figuring out why creator property in question was not updated.
Looking bit further ahead, commented out code-section at line 687 might be what is needed, and/or indicates there is a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, that won't help as apparently property has value deserializer but somehow
creatorProps
has not been properly updated earlier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I guess
creatorProps
returned by[Std]ValueInstantiator
has not been synced with changes made viaupdateBuilder()
. So that is legit problem to resolve, just not sure where it should be done.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that #4515 is completed, can rule out problem with linkage. I do think that
BeanDeserializerBuilder
needs access to the property-based creator, and that the issue here really is thatCreatorProperty
within that Creator does not properly updated when property in_properties
is updated.So the question is then how to pass it along, update properly.