-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add test case for using JsonUnwrapped on fields of the same class #5182
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
Open
kariem
wants to merge
1
commit into
FasterXML:2.x
Choose a base branch
from
kariem:unwrapped-dynamic-prefix
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
205 changes: 205 additions & 0 deletions
205
src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithDynamicPrefix.java
This file contains hidden or 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,205 @@ | ||
package com.fasterxml.jackson.databind.struct; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonUnwrapped; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.ser.ContextualSerializer; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class TestUnwrappedWithDynamicPrefix extends DatabindTestUtil { | ||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
private final ObjectMapper CUSTOM_MAPPER = newJsonMapper() | ||
.registerModule(new SimpleModule() | ||
.addSerializer(ValueAndMap.class, new ValueAndMapSerializer()) | ||
.addDeserializer(ValueAndMap.class, new ValueAndMapDeserializer())); | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
static class TwoUnwrappedPropertiesWithSameClass { | ||
@JsonUnwrapped | ||
public ValueAndMap prop1; | ||
@JsonUnwrapped | ||
public ValueAndMap prop2; | ||
} | ||
|
||
static class ValueAndMap { | ||
public String value; | ||
public Map<String, String> value_l10n; | ||
|
||
@JsonIgnore | ||
public boolean isEmpty() { | ||
return value == null && (value_l10n == null || value_l10n.isEmpty()); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testUnwrappingWithDifferentProperties() throws JsonProcessingException { | ||
TwoUnwrappedPropertiesWithSameClass o = new TwoUnwrappedPropertiesWithSameClass(); | ||
o.prop1 = new ValueAndMap(); | ||
o.prop1.value = "foo"; | ||
o.prop1.value_l10n = Map.of("a", "b"); | ||
o.prop2 = new ValueAndMap(); | ||
o.prop2.value = "bar"; | ||
o.prop2.value_l10n = Map.of("c", "d"); | ||
|
||
// currently produces JSON object with duplicate keys JSON | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess ideally it'd fail with exception? Since definition would instruct output like this. |
||
String s = MAPPER.writeValueAsString(o); | ||
// TODO: this is just for visualization, not sure if we should throw an exception to avoid writing invalid JSON | ||
assertEquals(""" | ||
{"value":"foo","value_l10n":{"a":"b"},"value":"bar","value_l10n":{"c":"d"}}""" | ||
, s); | ||
|
||
System.out.println(s); | ||
} | ||
|
||
@Test | ||
public void testUnwrappingWithCustomSerializer() throws JsonProcessingException { | ||
TwoUnwrappedPropertiesWithSameClass o = new TwoUnwrappedPropertiesWithSameClass(); | ||
o.prop1 = new ValueAndMap(); | ||
o.prop1.value = "foo"; | ||
o.prop1.value_l10n = Map.of("a", "b"); | ||
o.prop2 = new ValueAndMap(); | ||
o.prop2.value = "bar"; | ||
o.prop2.value_l10n = Map.of("c", "d"); | ||
|
||
String s = CUSTOM_MAPPER.writeValueAsString(o); | ||
assertEquals(""" | ||
{"prop1":"foo","prop1_l10n":{"a":"b"},"prop2":"bar","prop2_l10n":{"c":"d"}}""" | ||
, s); | ||
} | ||
|
||
@Test | ||
public void testUnwrappingWithCustomDeserializer() throws JsonProcessingException { | ||
String json = """ | ||
{"prop1":"foo","prop1_l10n":{"a":"b"},"prop2":"bar","prop2_l10n":{"c":"d"}}"""; | ||
|
||
TwoUnwrappedPropertiesWithSameClass o = CUSTOM_MAPPER.readValue(json, TwoUnwrappedPropertiesWithSameClass.class); | ||
assertNotNull(o.prop1); | ||
assertEquals("foo", o.prop1.value); | ||
assertEquals(Map.of("a", "b"), o.prop1.value_l10n); | ||
assertNotNull(o.prop2); | ||
assertEquals("bar", o.prop2.value); | ||
assertEquals(Map.of("c", "d"), o.prop2.value_l10n); | ||
} | ||
|
||
static class ValueAndMapDeserializer extends StdDeserializer<ValueAndMap> implements ContextualDeserializer { | ||
|
||
private final String name; | ||
|
||
public ValueAndMapDeserializer() { | ||
this(null); | ||
} | ||
|
||
public ValueAndMapDeserializer(String name) { | ||
super(ValueAndMap.class); | ||
this.name = name == null || name.trim().isEmpty() ? "name" : name; | ||
} | ||
|
||
@Override | ||
public ValueAndMap deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
ValueAndMap result = new ValueAndMap(); | ||
|
||
JsonNode node = ctxt.readTree(p); | ||
result.value = getText(node, name); | ||
|
||
JsonNode l10n = node.get(name + "_l10n"); | ||
if (l10n != null && l10n.isObject()) { | ||
Map<String, String> m = new HashMap<>(); | ||
ObjectNode n = (ObjectNode) l10n; | ||
Iterator<String> stringIterator = n.fieldNames(); | ||
if (stringIterator != null) { | ||
while (stringIterator.hasNext()) { | ||
String fieldName = stringIterator.next(); | ||
m.put(fieldName, n.get(fieldName).textValue()); | ||
} | ||
} | ||
if (!m.isEmpty()) { | ||
result.value_l10n = new HashMap<>(m); | ||
} | ||
} | ||
|
||
return result.isEmpty() ? null : result; | ||
} | ||
|
||
private String getText(JsonNode node, String name) { | ||
JsonNode field = node.get(name); | ||
return field == null ? null : field.asText(); | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<ValueAndMap> createContextual(DeserializationContext ctxt, BeanProperty property) { | ||
if (property == null) { | ||
return null; | ||
} | ||
return new ValueAndMapDeserializer(property.getName()); | ||
} | ||
} | ||
|
||
static class ValueAndMapSerializer extends StdSerializer<ValueAndMap> implements ContextualSerializer { | ||
|
||
private final String name; | ||
|
||
public ValueAndMapSerializer() { | ||
this(null); | ||
} | ||
|
||
public ValueAndMapSerializer(String name) { | ||
super(ValueAndMap.class); | ||
this.name = name == null || name.trim().isEmpty() ? "name" : name; | ||
} | ||
|
||
@Override | ||
public boolean isUnwrappingSerializer() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void serialize(ValueAndMap field, JsonGenerator jgen, SerializerProvider provider) throws IOException { | ||
String value = field.value; | ||
if (value != null && !value.trim().isEmpty()) { | ||
jgen.writeStringField(name, value); | ||
} | ||
Map<String, String> l10n = field.value_l10n; | ||
if (l10n != null && !l10n.isEmpty()) { | ||
jgen.writeFieldName(name + "_l10n"); | ||
jgen.writeStartObject(); | ||
for (Map.Entry<String, String> e : l10n.entrySet()) { | ||
jgen.writeStringField(e.getKey(), e.getValue()); | ||
} | ||
jgen.writeEndObject(); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonSerializer<ValueAndMap> createContextual(SerializerProvider prov, BeanProperty property) { | ||
if (property == null) { | ||
return null; | ||
} | ||
return new ValueAndMapSerializer(property.getName()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Bad idea for tests -- should almost never be used: often masks legitimate problems (in production use useful for defensive handling).
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.
Yes, I agree. Removing this, however, will throw a
UnrecognizedPropertyException
The idea was to see what the configuration was actually doing.
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.
Right, so it did hide a failure.