-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #5342 @JsonAnyGetter
method serialization can override @JsonProperty
on name conflict
#5346
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
base: 2.19
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.fasterxml.jackson.databind.ser; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.*; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
// [databind#5342] JsonAnyGetter method serialization can override JsonProperty serialization on serialized name conflict | ||
public class AnyGetterNameConflictSerialization5342Test | ||
extends DatabindTestUtil | ||
{ | ||
public static class Pojo5342 { | ||
@JsonIgnore | ||
private Map<String, Object> additionalProperties; | ||
@JsonProperty(value = "additionalProperties") | ||
private Map<String, Object> hidden; | ||
Comment on lines
+22
to
+23
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. Modified to hidden, so we get better view of what's actually conflicting -- |
||
|
||
@JsonAnySetter | ||
private void additionalProperties(String key, Object value) { | ||
if (additionalProperties == null) { | ||
additionalProperties = new HashMap<>(); | ||
} | ||
additionalProperties.put(key.replace("\\.", "."), value); | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> additionalProperties() { | ||
return additionalProperties; | ||
} | ||
|
||
public Map<String, Object> hidden() { | ||
return hidden; | ||
} | ||
|
||
public void hidden(Map<String, Object> additionalPropertiesProperty) { | ||
this.hidden = additionalPropertiesProperty; | ||
} | ||
} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
@Test | ||
public void testOverwrite() | ||
throws Exception | ||
{ | ||
Pojo5342 pojo = new Pojo5342(); | ||
pojo.additionalProperties("foo", "bar"); | ||
|
||
Map<String, Object> hidden = new HashMap<>(); | ||
hidden.put("fizz", "buzz"); | ||
pojo.hidden(hidden); | ||
|
||
|
||
String JSON = MAPPER.writeValueAsString(pojo); | ||
// was in 2.18 : {"foo":"bar","additionalProperties": {"fizz":"buzz"}} | ||
// now in 2.19 : {"foo":"bar"}... need FIX! | ||
// hidden field | ||
assertTrue(JSON.contains("\"additionalProperties\":{\"fizz\":\"buzz\"}")); | ||
// any-getter | ||
assertTrue(JSON.contains("\"foo\":\"bar\"")); | ||
|
||
// Try deserializaing back | ||
Pojo5342 actual = MAPPER.readValue(JSON, Pojo5342.class); | ||
assertEquals(1, actual.additionalProperties.size()); | ||
assertEquals(1, actual.hidden().size()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,6 +127,7 @@ public Map<String, Object> secondProperties() { | |
} | ||
} | ||
|
||
// | ||
@JsonPropertyOrder({ "firstProperty", "secondProperties", "thirdProperty", "forthProperty" }) | ||
static class PrivateAnyGetterPojoSorted extends PrivateAnyGetterPojo { | ||
public Map<String, Object> getSecondProperties() { | ||
|
@@ -277,20 +278,6 @@ public void testPrivateAnyGetter() throws Exception { | |
json); | ||
} | ||
|
||
@Test | ||
public void testPrivateAnyGetterSorted() throws Exception { | ||
PrivateAnyGetterPojoSorted pojo = new PrivateAnyGetterPojoSorted(); | ||
pojo.add("secondProperty", 2); | ||
String json = MAPPER.writeValueAsString(pojo); | ||
|
||
assertEquals(a2q("{" + | ||
"'firstProperty':1," + | ||
"'secondProperty':2," + // private accesor, wont' work here | ||
"'thirdProperty':3," + | ||
"'forthProperty':4}"), | ||
json); | ||
} | ||
|
||
Comment on lines
-280
to
-293
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 deleted so we have better visibility on subject matter) So this one is now failing.
... this, but I can't figure out why there is trailing Moreover, I am more confused because it the ordering like above should've worked even before since we have public Map<String, Object> getSecondProperties() {
... declared at line 133. 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 don't know for sure, but my guess is that the change broke linkage between
|
||
private void _configureValues(BaseWithProperties base) { | ||
base.entityId = 1; | ||
base.entityName = "Bob"; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
I am not sure about this one: why is the change necessary/correct?
At least requires a comment on why we are looking at method/field name of concrete accessor, instead of processed property name.