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

BeanDeserializerModifier::updateBuilder shall work for immutable beans too (#4356) #4357

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ public void resolve(DeserializationContext ctxt) throws JsonMappingException
}
SettableBeanProperty newProp = prop.withValueDeserializer(deser);
_replaceProperty(_beanProperties, creatorProps, prop, newProp);
} else if ((creatorProps != null) && prop instanceof CreatorProperty) {
for (int i = 0, len = creatorProps.length; i < len; ++i) {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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 via updateBuilder(). So that is legit problem to resolve, just not sure where it should be done.

Copy link
Member

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 that CreatorProperty 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.

SettableBeanProperty creatorProp = creatorProps[i];
if (!creatorProp.hasValueDeserializer() && creatorProp.getFullName().equals(prop.getFullName())) {
creatorProps[i] = creatorProp.withValueDeserializer(prop.getValueDeserializer());
break;
}
}
}
}

Expand Down
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());
}
}