Skip to content

Commit f3fbb08

Browse files
committed
Fix #4589: remove MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER, change default sorting logic
1 parent a20921d commit f3fbb08

File tree

4 files changed

+11
-57
lines changed

4 files changed

+11
-57
lines changed

src/main/java/tools/jackson/databind/MapperFeature.java

-17
Original file line numberDiff line numberDiff line change
@@ -302,23 +302,6 @@ public enum MapperFeature
302302
*/
303303
SORT_CREATOR_PROPERTIES_FIRST(true),
304304

305-
/**
306-
* Feature that defines whether Creator properties (ones passed through
307-
* constructor or static factory method) should be sorted in their declaration
308-
* order if {@link #SORT_CREATOR_PROPERTIES_FIRST} is also enabled.
309-
* This is usually used to prevent alphabetic sorting for
310-
* Creator properties even if {@link #SORT_PROPERTIES_ALPHABETICALLY} is
311-
* enabled for other types of properties.
312-
*<p>
313-
* NOTE: if {@link #SORT_CREATOR_PROPERTIES_FIRST} is disabled, this feature
314-
* has no effect.
315-
*<p>
316-
* Feature is disabled by default (for backwards compatibility)
317-
*
318-
* @since 2.18
319-
*/
320-
SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER(false),
321-
322305
/*
323306
/**********************************************************************
324307
/* Name-related features

src/main/java/tools/jackson/databind/introspect/POJOPropertiesCollector.java

+3-18
Original file line numberDiff line numberDiff line change
@@ -1530,24 +1530,9 @@ protected void _sortProperties(Map<String, POJOPropertyBuilder> props)
15301530
* order. Related question of creator vs non-creator is punted for now,
15311531
* so creator properties still fully predate non-creator ones.
15321532
*/
1533-
Collection<POJOPropertyBuilder> cr;
1534-
// 18-Jun-2024, tatu: [databind#4580] We may want to retain declaration
1535-
// order regardless
1536-
boolean sortCreatorPropsByAlpha = sortAlpha
1537-
&& !_config.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER);
1538-
if (sortCreatorPropsByAlpha) {
1539-
TreeMap<String, POJOPropertyBuilder> sorted =
1540-
new TreeMap<String,POJOPropertyBuilder>();
1541-
for (POJOPropertyBuilder prop : _creatorProperties) {
1542-
if (prop != null) {
1543-
sorted.put(prop.getName(), prop);
1544-
}
1545-
}
1546-
cr = sorted.values();
1547-
} else {
1548-
cr = _creatorProperties;
1549-
}
1550-
for (POJOPropertyBuilder prop : cr) {
1533+
// 18-Jun-2024, tatu: Except in Jackson 3.0, we do NOT sort creator properties
1534+
// alphabetically if they are to be sorted before other properties.
1535+
for (POJOPropertyBuilder prop : _creatorProperties) {
15511536
if (prop == null) {
15521537
continue;
15531538
}

src/test-jdk17/java/tools/jackson/databind/records/RecordSerializationOrderTest.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,14 @@ public void testSerializationOrderWithJsonPropertyOrder() throws Exception {
7979
// [databind#4580]
8080
@Test
8181
public void testSerializationOrderWrtCreatorAlphabetic() throws Exception {
82-
// In 3.0, sorting by Alphabetic enabled by default so
83-
assertEquals(a2q("{'a':'a','b':'b','c':'c'}"),
84-
MAPPER.writeValueAsString(new CABRecord("c", "a", "b")));
85-
// But can disable
82+
// In 3.0, sorting by Alphabetic enabled by default BUT it won't affect Creator props
8683
assertEquals(a2q("{'c':'c','a':'a','b':'b'}"),
84+
MAPPER.writeValueAsString(new CABRecord("c", "a", "b")));
85+
// Unless we disable Creator-props-first setting:
86+
assertEquals(a2q("{'a':'a','b':'b','c':'c'}"),
8787
jsonMapperBuilder()
88-
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
88+
.disable(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
8989
.build()
9090
.writeValueAsString(new CABRecord("c", "a", "b")));
91-
// Except if we tell it not to:
92-
assertEquals(a2q("{'c':'c','a':'a','b':'b'}"),
93-
jsonMapperBuilder()
94-
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
95-
.enable(MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER)
96-
.build()
97-
.writeValueAsString(new CABRecord("c", "a", "b")));
9891
}
9992
}

src/test/java/tools/jackson/databind/ser/SerializationOrderTest.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,12 @@ public void testCreatorVsExplicitOrdering() throws Exception
205205
@Test
206206
public void testAlphaAndCreatorOrdering() throws Exception
207207
{
208-
assertEquals(a2q("{'a':1,'b':2}"),
208+
assertEquals(a2q("{'b':2,'a':1}"),
209209
ALPHA_MAPPER.writeValueAsString(new BeanForGH311(2, 1)));
210-
}
211-
212-
// [databind#4580]
213-
@Test
214-
public void testAlphaAndCreatorDeclarationOrdering() throws Exception
215-
{
216210
final ObjectMapper mapper = jsonMapperBuilder()
217-
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
218-
.enable(MapperFeature.SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER)
211+
.disable(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
219212
.build();
220-
assertEquals(a2q("{'b':2,'a':1}"),
213+
assertEquals(a2q("{'a':1,'b':2}"),
221214
mapper.writeValueAsString(new BeanForGH311(2, 1)));
222215
}
223216

0 commit comments

Comments
 (0)