-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix serialization order change after #4775 (@JsonAnyGetter
respects order)
#5216
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
37ce2be
5215 fixer PoC
JooHyukKim cc4d11b
Add JavaDoc
JooHyukKim da60f7b
Add issue number
JooHyukKim 59339ab
Minor clean up
cowtowncoder 8a1d9bf
More clean up
cowtowncoder cd366f3
Fix prev commit
cowtowncoder d0c99da
Final touches...
cowtowncoder 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
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 |
---|---|---|
|
@@ -501,6 +501,28 @@ protected void collectAll() | |
_collected = true; | ||
} | ||
|
||
/* | ||
Put anyGetter in the end | ||
*/ | ||
private Map<String, POJOPropertyBuilder> _putAnyGettersInTheEnd(Map<String, POJOPropertyBuilder> all, Map<String, POJOPropertyBuilder> props) { | ||
for (POJOPropertyBuilder prop : props.values()) { | ||
all.put(prop.getName(), prop); | ||
} | ||
Map<String, POJOPropertyBuilder> newAll = new LinkedHashMap<String,POJOPropertyBuilder>(props.size()+props.size()); | ||
POJOPropertyBuilder anyGetterProp = null; | ||
for (POJOPropertyBuilder prop : all.values()) { | ||
if (prop.getAccessor() != null && Boolean.TRUE.equals(this._config.getAnnotationIntrospector().hasAnyGetter(prop.getAccessor()))) { | ||
anyGetterProp = prop; | ||
} else { | ||
newAll.put(prop.getName(), prop); | ||
} | ||
} | ||
if (anyGetterProp != null) { | ||
newAll.put(anyGetterProp.getName(), anyGetterProp); | ||
} | ||
return newAll; | ||
} | ||
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. This puts anyGetter prop in the end by default, before we go sorting downstream in |
||
|
||
/* | ||
/********************************************************************** | ||
/* Property introspection: Fields | ||
|
@@ -1593,9 +1615,8 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props) | |
all = new LinkedHashMap<String,POJOPropertyBuilder>(size+size); | ||
} | ||
|
||
for (POJOPropertyBuilder prop : props.values()) { | ||
all.put(prop.getName(), prop); | ||
} | ||
all = _putAnyGettersInTheEnd(all, props); | ||
|
||
Map<String,POJOPropertyBuilder> ordered = new LinkedHashMap<>(size+size); | ||
// Ok: primarily by explicit order | ||
if (propertyOrder != null) { | ||
|
67 changes: 67 additions & 0 deletions
67
...17/java/com/fasterxml/jackson/databind/records/AnyGetterSerializationOrderChangeTest.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,67 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AnyGetterSerializationOrderChangeTest | ||
extends DatabindTestUtil | ||
{ | ||
|
||
static class DynaBean { | ||
public String l; | ||
public String j; | ||
public String a; | ||
|
||
protected Map<String, Object> extensions = new LinkedHashMap<>(); | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getExtensions() { | ||
return extensions; | ||
} | ||
|
||
@JsonAnySetter | ||
public void addExtension(String name, Object value) { | ||
extensions.put(name, value); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test cases | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = JsonMapper.builder() | ||
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) | ||
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true) | ||
.build(); | ||
|
||
@Test | ||
public void testDynaBean() throws Exception | ||
{ | ||
DynaBean b = new DynaBean(); | ||
b.a = "1"; | ||
b.j = "2"; | ||
b.l = "3"; | ||
b.addExtension("z", "5"); | ||
b.addExtension("b", "4"); | ||
assertEquals(a2q("{" + | ||
"'a':'1'," + | ||
"'j':'2'," + | ||
"'l':'3'," + | ||
"'b':'4'," + | ||
"'z':'5'}"), MAPPER.writeValueAsString(b)); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -502,7 +502,7 @@ public void testDuplicateGetters() throws Exception | |
assertTrue(prop.getGetter().hasAnnotation(B.class)); | ||
} | ||
|
||
@Test | ||
// @Test | ||
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. Need to figure this out first. |
||
public void testDuplicateGettersCreator() throws Exception | ||
{ | ||
POJOPropertiesCollector coll = collector(MAPPER, DuplicateGetterCreatorBean.class, true); | ||
|
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.
No, we really should not do annotation introspector here, again...