From f879990ba3c50a614e39b00c52998fcaf1a5a718 Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 9 Jun 2024 21:06:34 +0900 Subject: [PATCH 1/3] Change default to true --- .../java/tools/jackson/databind/MapperFeature.java | 5 +++-- .../java/tools/jackson/databind/ObjectMapperTest.java | 10 +++++----- .../databind/jsontype/ExistingPropertyTest.java | 6 +++--- .../databind/jsontype/TestPolymorphicDeduction.java | 4 ++-- .../tools/jackson/databind/jsontype/TestTypeNames.java | 2 +- .../jackson/databind/ser/SerializationOrderTest.java | 2 +- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/tools/jackson/databind/MapperFeature.java b/src/main/java/tools/jackson/databind/MapperFeature.java index ac2503393c..0ad5e0c629 100644 --- a/src/main/java/tools/jackson/databind/MapperFeature.java +++ b/src/main/java/tools/jackson/databind/MapperFeature.java @@ -277,10 +277,11 @@ public enum MapperFeature *

* Note: does not apply to {@link java.util.Map} serialization (since * entries are not considered Bean/POJO properties. + * *

- * Feature is disabled by default. + * Feature is enabled by default. */ - SORT_PROPERTIES_ALPHABETICALLY(false), + SORT_PROPERTIES_ALPHABETICALLY(true), /** * Feature that defines whether Creator properties (ones passed through diff --git a/src/test/java/tools/jackson/databind/ObjectMapperTest.java b/src/test/java/tools/jackson/databind/ObjectMapperTest.java index 30ebd98186..8f80d04453 100644 --- a/src/test/java/tools/jackson/databind/ObjectMapperTest.java +++ b/src/test/java/tools/jackson/databind/ObjectMapperTest.java @@ -106,15 +106,15 @@ public void testConfigForPropertySorting() throws Exception { ObjectMapper m = newJsonMapper(); - // sort-alphabetically is disabled by default: - assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); + // sort-alphabetically is enabled by default: + assertTrue(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); assertTrue(m.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)); SerializationConfig sc = m.serializationConfig(); - assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); - assertFalse(sc.shouldSortPropertiesAlphabetically()); + assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)); + assertTrue(sc.shouldSortPropertiesAlphabetically()); assertTrue(sc.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)); DeserializationConfig dc = m.deserializationConfig(); - assertFalse(dc.shouldSortPropertiesAlphabetically()); + assertTrue(dc.shouldSortPropertiesAlphabetically()); assertTrue(dc.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)); // but when enabled, should be visible: diff --git a/src/test/java/tools/jackson/databind/jsontype/ExistingPropertyTest.java b/src/test/java/tools/jackson/databind/jsontype/ExistingPropertyTest.java index c69aff469c..7c680dcf07 100644 --- a/src/test/java/tools/jackson/databind/jsontype/ExistingPropertyTest.java +++ b/src/test/java/tools/jackson/databind/jsontype/ExistingPropertyTest.java @@ -267,16 +267,16 @@ static class DefaultShape3271 extends Shape3271 {} private static final String fruitListJson = "[" + pinguoJson + "," + mandarinJson + "]"; private static final Cat beelzebub = new Cat("Beelzebub", "tabby"); - private static final String beelzebubJson = "{\"name\":\"Beelzebub\",\"furColor\":\"tabby\",\"type\":\"kitty\"}"; + private static final String beelzebubJson = "{\"furColor\":\"tabby\",\"name\":\"Beelzebub\",\"type\":\"kitty\"}"; private static final Dog rover = new Dog("Rover", 42); - private static final String roverJson = "{\"name\":\"Rover\",\"boneCount\":42,\"type\":\"doggie\"}"; + private static final String roverJson = "{\"boneCount\":42,\"name\":\"Rover\",\"type\":\"doggie\"}"; private static final AnimalWrapper beelzebubWrapper = new AnimalWrapper(beelzebub); private static final String beelzebubWrapperJson = "{\"animal\":" + beelzebubJson + "}"; private static final List animalList = Arrays.asList(beelzebub, rover); private static final String animalListJson = "[" + beelzebubJson + "," + roverJson + "]"; private static final Camry camry = new Camry("Sweet Ride", "candy-apple-red"); - private static final String camryJson = "{\"name\":\"Sweet Ride\",\"exteriorColor\":\"candy-apple-red\",\"type\":\"camry\"}"; + private static final String camryJson = "{\"exteriorColor\":\"candy-apple-red\",\"name\":\"Sweet Ride\",\"type\":\"camry\"}"; private static final Accord accord = new Accord("Road Rage", 6); private static final String accordJson = "{\"name\":\"Road Rage\",\"speakerCount\":6,\"type\":\"accord\"}"; private static final CarWrapper camryWrapper = new CarWrapper(camry); diff --git a/src/test/java/tools/jackson/databind/jsontype/TestPolymorphicDeduction.java b/src/test/java/tools/jackson/databind/jsontype/TestPolymorphicDeduction.java index 09133d4372..d9740e2a00 100644 --- a/src/test/java/tools/jackson/databind/jsontype/TestPolymorphicDeduction.java +++ b/src/test/java/tools/jackson/databind/jsontype/TestPolymorphicDeduction.java @@ -71,8 +71,8 @@ static enum Enum3711 { A, B } /********************************************************** */ - private static final String deadCatJson = a2q("{'name':'Felix','causeOfDeath':'entropy'}"); - private static final String liveCatJson = a2q("{'name':'Felix','angry':true}"); + private static final String deadCatJson = a2q("{'causeOfDeath':'entropy','name':'Felix'}"); + private static final String liveCatJson = a2q("{'angry':true,'name':'Felix'}"); private static final String luckyCatJson = a2q("{'name':'Felix','angry':true,'lives':8}"); private static final String ambiguousCatJson = a2q("{'name':'Felix','age':2}"); private static final String fleabagJson = a2q("{}"); diff --git a/src/test/java/tools/jackson/databind/jsontype/TestTypeNames.java b/src/test/java/tools/jackson/databind/jsontype/TestTypeNames.java index e0ddee780c..4cd5abed73 100644 --- a/src/test/java/tools/jackson/databind/jsontype/TestTypeNames.java +++ b/src/test/java/tools/jackson/databind/jsontype/TestTypeNames.java @@ -72,7 +72,7 @@ public void testSerialization() throws Exception // static type on serialization. If we had root static types, // could use those; but at the moment root type is dynamic - assertEquals("[{\"doggy\":{\"name\":\"Spot\",\"ageInYears\":3}}]", + assertEquals("[{\"doggy\":{\"ageInYears\":3,\"name\":\"Spot\"}}]", MAPPER.writeValueAsString(new Animal[] { new Dog("Spot", 3) })); assertEquals("[{\"MaineCoon\":{\"name\":\"Belzebub\",\"purrs\":true}}]", MAPPER.writeValueAsString(new Animal[] { new MaineCoon("Belzebub", true)})); diff --git a/src/test/java/tools/jackson/databind/ser/SerializationOrderTest.java b/src/test/java/tools/jackson/databind/ser/SerializationOrderTest.java index 9c664c6c20..26236cce57 100644 --- a/src/test/java/tools/jackson/databind/ser/SerializationOrderTest.java +++ b/src/test/java/tools/jackson/databind/ser/SerializationOrderTest.java @@ -150,7 +150,7 @@ public BeanForStrictOrdering(@JsonProperty("c") int c, @JsonProperty("a") int a) @Test public void testImplicitOrderByCreator() throws Exception { - assertEquals("{\"c\":1,\"a\":2,\"b\":0}", + assertEquals("{\"a\":2,\"c\":1,\"b\":0}", MAPPER.writeValueAsString(new BeanWithCreator(1, 2))); } From 8995e93bf91199299985d21b82315256bb49156d Mon Sep 17 00:00:00 2001 From: "Kim, Joo Hyuk" Date: Sun, 9 Jun 2024 21:15:56 +0900 Subject: [PATCH 2/3] Fix failing record tests --- .../databind/records/RecordCreatorSerialization4452Test.java | 2 +- .../jackson/databind/records/RecordExplicitCreatorsTest.java | 2 +- .../databind/records/RecordSerializationOrderTest.java | 4 ++-- .../jackson/databind/records/RecordWithView4085Test.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test-jdk17/java/tools/jackson/databind/records/RecordCreatorSerialization4452Test.java b/src/test-jdk17/java/tools/jackson/databind/records/RecordCreatorSerialization4452Test.java index 57b2310c48..3f39fc332c 100644 --- a/src/test-jdk17/java/tools/jackson/databind/records/RecordCreatorSerialization4452Test.java +++ b/src/test-jdk17/java/tools/jackson/databind/records/RecordCreatorSerialization4452Test.java @@ -43,7 +43,7 @@ public CreatorTestObject( public void testPlain() throws Exception { String result = OBJECT_MAPPER.writeValueAsString(new PlainTestObject("test", 1)); - assertEquals("{\"strField\":\"test\",\"intField\":1}", result); + assertEquals("{\"intField\":1,\"strField\":\"test\"}", result); } @Test diff --git a/src/test-jdk17/java/tools/jackson/databind/records/RecordExplicitCreatorsTest.java b/src/test-jdk17/java/tools/jackson/databind/records/RecordExplicitCreatorsTest.java index 1f176ccfb4..d2ed7e577e 100644 --- a/src/test-jdk17/java/tools/jackson/databind/records/RecordExplicitCreatorsTest.java +++ b/src/test-jdk17/java/tools/jackson/databind/records/RecordExplicitCreatorsTest.java @@ -153,7 +153,7 @@ public void testDeserializeUsingCanonicalConstructor_WhenJsonPropertyConstructor } catch (DatabindException e) { verifyException(e, "Unrecognized property \"id\""); verifyException(e, "RecordWithTwoJsonPropertyWithoutJsonCreator"); - verifyException(e, "2 known properties: \"the_id\", \"the_email\""); + verifyException(e, "2 known properties: \"the_email\", \"the_id\""); } } diff --git a/src/test-jdk17/java/tools/jackson/databind/records/RecordSerializationOrderTest.java b/src/test-jdk17/java/tools/jackson/databind/records/RecordSerializationOrderTest.java index ae75d294a4..6361928864 100644 --- a/src/test-jdk17/java/tools/jackson/databind/records/RecordSerializationOrderTest.java +++ b/src/test-jdk17/java/tools/jackson/databind/records/RecordSerializationOrderTest.java @@ -39,7 +39,7 @@ public void testSerializationOrder() throws Exception { NestedRecordTwo nestedRecordTwo = new NestedRecordTwo("2", "111110"); NestedRecordOne nestedRecordOne = new NestedRecordOne("1", "test@records.com", nestedRecordTwo); final String output = MAPPER.writeValueAsString(nestedRecordOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedRecordTwo\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + final String expected = "{\"email\":\"test@records.com\",\"id\":\"1\",\"nestedRecordTwo\":{\"id\":\"2\",\"passport\":\"111110\"}}"; assertEquals(expected, output); } @@ -49,7 +49,7 @@ public void testSerializationOrderWithJsonProperty() throws Exception { NestedRecordOneWithJsonProperty nestedRecordOne = new NestedRecordOneWithJsonProperty("1", "test@records.com", nestedRecordTwo); final String output = MAPPER.writeValueAsString(nestedRecordOne); - final String expected = "{\"id\":\"1\",\"email\":\"test@records.com\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; + final String expected = "{\"email\":\"test@records.com\",\"id\":\"1\",\"nestedProperty\":{\"id\":\"2\",\"passport\":\"111110\"}}"; assertEquals(expected, output); } diff --git a/src/test-jdk17/java/tools/jackson/databind/records/RecordWithView4085Test.java b/src/test-jdk17/java/tools/jackson/databind/records/RecordWithView4085Test.java index 252f11481c..85615a1704 100644 --- a/src/test-jdk17/java/tools/jackson/databind/records/RecordWithView4085Test.java +++ b/src/test-jdk17/java/tools/jackson/databind/records/RecordWithView4085Test.java @@ -22,7 +22,7 @@ public record Record4085(int total, @JsonView(View4085Field.class) int current) public void testRecordWithView4085() throws Exception { final Record4085 input = new Record4085(1, 2); - final String EXP = a2q("{'total':1,'current':2}"); + final String EXP = a2q("{'current':2,'total':1}"); final ObjectWriter w = newJsonMapper().writer(); // by default, all properties included, without view From c0e7ceab83554dc4caaf1e53ccc877dde41e97c0 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 13 Jun 2024 19:02:58 -0700 Subject: [PATCH 3/3] Add release note, javadoc default comment --- release-notes/VERSION | 2 ++ src/main/java/tools/jackson/databind/MapperFeature.java | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/release-notes/VERSION b/release-notes/VERSION index 4f3d3bbfa7..d66b241dfc 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -62,6 +62,8 @@ Versions: 3.x (for earlier see VERSION-2.x) (contributed by Joo-Hyuk K) #4567: Enable `SerializationFeature.WRITE_ENUMS_USING_TO_STRING` by default (3.0) (contributed by Joo-Hyuk K) +#4572: Change `MapperFeature.SORT_PROPERTIES_ALPHABETICALLY` default to true + (contributed by Joo-Hyuk K) - Remove `MappingJsonFactory` - Add context parameter for `TypeSerializer` contextualization (`forProperty()`) - Default for `JsonNodeFeature.STRIP_TRAILING_BIGDECIMAL_ZEROES` changed to `false` for 3.0 diff --git a/src/main/java/tools/jackson/databind/MapperFeature.java b/src/main/java/tools/jackson/databind/MapperFeature.java index 0ad5e0c629..b71972b073 100644 --- a/src/main/java/tools/jackson/databind/MapperFeature.java +++ b/src/main/java/tools/jackson/databind/MapperFeature.java @@ -40,7 +40,7 @@ public enum MapperFeature * precedence than setters, so they are only used if no * setter is found for the Map/Collection property. *

- * Feature is disabled by default since 3.0 (with 2.x was enabled) + * Feature is disabled by default since 3.0 (in 2.x was enabled) */ USE_GETTERS_AS_SETTERS(false), @@ -277,9 +277,8 @@ public enum MapperFeature *

* Note: does not apply to {@link java.util.Map} serialization (since * entries are not considered Bean/POJO properties. - * *

- * Feature is enabled by default. + * Feature is enabled by default since 3.0 (in 2.x was disabled) */ SORT_PROPERTIES_ALPHABETICALLY(true),