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

Fix issues 4697 and 2461 (@JsonUnwrapped caching) #4722

Merged
merged 8 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -24,6 +24,8 @@
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.NameTransformer;

import static com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.emptyForProperties;
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved

/**
* Base bean property handler class, which implements common parts of
* reflection-based functionality for accessing a property value and serializing
Expand Down Expand Up @@ -222,8 +224,7 @@ public BeanPropertyWriter(BeanPropertyDefinition propDef,

_declaredType = declaredType;
_serializer = (JsonSerializer<Object>) ser;
_dynamicSerializers = (ser == null) ? PropertySerializerMap
.emptyForProperties() : null;
_dynamicSerializers = (ser == null) ? PropertySerializerMap.emptyForProperties() : null;
_typeSerializer = typeSer;
_cfgSerializationType = serType;

Expand Down Expand Up @@ -325,7 +326,11 @@ protected BeanPropertyWriter(BeanPropertyWriter base, PropertyName name) {
base._internalSettings);
}
_cfgSerializationType = base._cfgSerializationType;
_dynamicSerializers = base._dynamicSerializers;
if (base.needToResetSerialization()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure check is needed, should probably simply reset always; I'll add a general note to PR.

_dynamicSerializers = PropertySerializerMap.emptyForProperties();
} else {
_dynamicSerializers = base._dynamicSerializers;
}
_suppressNulls = base._suppressNulls;
_suppressableValue = base._suppressableValue;
_includeInViews = base._includeInViews;
Expand All @@ -350,8 +355,12 @@ protected BeanPropertyWriter(BeanPropertyWriter base, SerializedString name) {
base._internalSettings);
}
_cfgSerializationType = base._cfgSerializationType;
_dynamicSerializers = base._dynamicSerializers;
_suppressNulls = base._suppressNulls;
if (base.needToResetSerialization()) {
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
} else {
_dynamicSerializers = base._dynamicSerializers;
}
_suppressNulls = base._suppressNulls;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these constructors called? Does this mean serializer information from 'base' should be lost?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_dynamicSerializers is purely performance optimization, caching to avoid unnecessary full introspection -- should never be needed for correctness. Hence if necessary we can just use empty instance for copy constructors.

_suppressableValue = base._suppressableValue;
_includeInViews = base._includeInViews;
_typeSerializer = base._typeSerializer;
Expand Down Expand Up @@ -988,4 +997,4 @@ public String toString() {
sb.append(')');
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public <A extends Annotation> A findAnnotation(Class<A> acls) {
/**********************************************************
*/

/**
* A way to reset serialization to correctly populate serializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately doesn't really describe anything....

*/
public boolean needToResetSerialization() {
return false;
}

/**
* The main serialization method called by filter when property is to be written normally.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,9 @@ protected JsonSerializer<Object> _findAndAddDynamic(PropertySerializerMap map,
_dynamicSerializers = _dynamicSerializers.newWith(type, serializer);
return serializer;
}

@Override
public boolean needToResetSerialization() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public Third() {
}
}

public static class AnotherFirst {
public Third thrid = new Third();
}

public static class Common {
public String a;
public String b;
public Common() {}
}

@JacksonTestFailureExpected
@Test
public void testInconsistentSer() throws Exception {
First first = new First();
Expand All @@ -51,8 +54,23 @@ public void testInconsistentSer() throws Exception {
ObjectMapper secondMapper = newJsonMapper();

firstMapper.writeValueAsString(first);
firstMapper.writeValueAsString(second);
assertEquals(
firstMapper.writeValueAsString(second),
secondMapper.writeValueAsString(second));
}
@Test
public void testInconsistentSer1() throws Exception {
AnotherFirst first = new AnotherFirst();
Second second = new Second();

ObjectMapper firstMapper = newJsonMapper();
ObjectMapper secondMapper = newJsonMapper();

firstMapper.writeValueAsString(first);
firstMapper.writeValueAsString(second);
assertEquals(
firstMapper.writeValueAsString(second),
secondMapper.writeValueAsString(second));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static class OuterContainer {
}

// [databind#2461]
@JacksonTestFailureExpected
@Test
void unwrappedCaching() throws Exception {
final InnerContainer inner = new InnerContainer(new Base("12345"));
Expand Down