Skip to content

StdDelegatingSerializer does not consider a Converter that may return null for a non-null input #4576

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

Merged
merged 3 commits into from
Jun 13, 2024
Merged
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 @@ -174,6 +174,11 @@ public void serializeWithType(Object value, JsonGenerator gen, SerializerProvide
// 03-Oct-2012, tatu: This is actually unlikely to work ok... but for now,
// let's give it a chance?
Object delegateValue = convertValue(value);
// consider null (to be consistent with serialize method above)
if (delegateValue == null) {
provider.defaultSerializeNull(gen);
return;
}
JsonSerializer<Object> ser = _delegateSerializer;
if (ser == null) {
ser = _findSerializer(value, provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharacterEscapes;
Expand All @@ -25,6 +23,8 @@
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.StdConverter;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -191,6 +191,55 @@ public String getId() {
}
}

// [databind#4575]
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes(
{
@JsonSubTypes.Type(Sub4575.class)
}
)
@JsonTypeName("Super")
static class Super4575 {
public static final Super4575 NULL = new Super4575();
}

@JsonTypeName("Sub")
static class Sub4575 extends Super4575 { }

static class NullSerializer4575 extends StdDelegatingSerializer {
public NullSerializer4575(Converter<Object, ?> converter, JavaType delegateType, JsonSerializer<?> delegateSerializer) {
super(converter, delegateType, delegateSerializer);
}

public NullSerializer4575(TypeFactory typeFactory, JsonSerializer<?> delegateSerializer) {
this(
new Converter<Object, Object>() {
@Override
public Object convert(Object value) {
return value == Super4575.NULL ? null : value;
}

@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructType(delegateSerializer.handledType());
}

@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructType(delegateSerializer.handledType());
}
},
typeFactory.constructType(delegateSerializer.handledType() == null ? Object.class : delegateSerializer.handledType()),
delegateSerializer
);
}

@Override
protected StdDelegatingSerializer withDelegate(Converter<Object, ?> converter, JavaType delegateType, JsonSerializer<?> delegateSerializer) {
return new NullSerializer4575(converter, delegateType, delegateSerializer);
}
}

/*
/**********************************************************
/* Unit tests
Expand All @@ -214,7 +263,6 @@ public void testCustomization() throws Exception
@Test
public void testCustomLists() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
JsonSerializer<?> ser = new CollectionSerializer(null, false, null, null);
final JsonSerializer<Object> collectionSerializer = (JsonSerializer<Object>) ser;
Expand All @@ -231,15 +279,16 @@ public void serialize(Collection value, JsonGenerator gen, SerializerProvider pr
}
}
});
mapper.registerModule(module);
ObjectMapper mapper = jsonMapperBuilder()
.addModule(module)
.build();
assertEquals("null", mapper.writeValueAsString(new ArrayList<Object>()));
}

// [databind#87]: delegating serializer
@Test
public void testDelegating() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(new StdDelegatingSerializer(Immutable.class,
new StdConverter<Immutable, Map<String,Integer>>() {
Expand All @@ -252,7 +301,9 @@ public Map<String, Integer> convert(Immutable value)
return map;
}
}));
mapper.registerModule(module);
ObjectMapper mapper = jsonMapperBuilder()
.addModule(module)
.build();
assertEquals("{\"x\":3,\"y\":7}", mapper.writeValueAsString(new Immutable()));
}

Expand Down Expand Up @@ -290,8 +341,9 @@ public void testWithCustomElements() throws Exception

SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(String.class, new UCStringSerializer());
ObjectMapper mapper = new ObjectMapper()
.registerModule(module);
ObjectMapper mapper = jsonMapperBuilder()
.addModule(module)
.build();

assertEquals(q("FOOBAR"), mapper.writeValueAsString("foobar"));
assertEquals(a2q("['FOO',null]"),
Expand All @@ -318,4 +370,28 @@ public void testIssue2475() throws Exception {
assertEquals(a2q("{'id':'ID-2','set':[]}"),
writer.writeValueAsString(new Item2475(new HashSet<String>(), "ID-2")));
}

@Test
public void testIssue4575() throws Exception {
com.fasterxml.jackson.databind.Module module = new SimpleModule() {
{
setSerializerModifier(new BeanSerializerModifier() {
@Override
public JsonSerializer<?> modifySerializer(
SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer
) {
return new NullSerializer4575(config.getTypeFactory(), serializer);
}
});
}
};

ObjectMapper mapper = jsonMapperBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

Using builder since "ObjectMapper.registerModule()" removed from 3.0, builder-approach easier to merge forward.

.addModule(module)
.build();

assertEquals("{\"@type\":\"Super\"}", mapper.writeValueAsString(new Super4575()));
assertEquals("{\"@type\":\"Sub\"}", mapper.writeValueAsString(new Sub4575()));
assertEquals("null", mapper.writeValueAsString(Super4575.NULL));
}
}