From d69613339320d3ed903960039bea067c4360a2e8 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 21 Oct 2024 19:18:22 -0700 Subject: [PATCH] Fix #167 by retaining Record field declaration order (#170) --- .../jackson/jr/ob/impl/BeanPropertyIntrospector.java | 12 +++++++++++- .../java/jr/{failing => }/RecordDeser167Test.java | 2 +- release-notes/CREDITS-2.x | 6 ++++++ release-notes/VERSION-2.x | 5 +++++ 4 files changed, 23 insertions(+), 2 deletions(-) rename jr-record-test/src/test-jdk17/java/jr/{failing => }/RecordDeser167Test.java (98%) diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanPropertyIntrospector.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanPropertyIntrospector.java index 0a809e65..1f7095fc 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanPropertyIntrospector.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanPropertyIntrospector.java @@ -46,7 +46,13 @@ public POJODefinition pojoDefinitionForSerialization(JSONWriter w, Class pojo private POJODefinition _introspectDefinition(Class beanType, boolean forSerialization, int features, boolean isRecord) { - Map propsByName = new TreeMap<>(); + // 21-Oct-2024, tatu: [jackson-jr#167] Need to retain property order + // for Deserialization, to keep Record properties ordered. + // For Serialization OTOH we need sorting (although would probably + // be better to sort after the fact, maybe in future) + + Map propsByName = forSerialization ? + new TreeMap<>() : new LinkedHashMap<>(); _introspect(beanType, propsByName, features, isRecord); final BeanConstructors constructors; @@ -131,6 +137,10 @@ private static void _introspect(Class currType, Map prop } if (Modifier.isPublic(f.getModifiers())) { _propFrom(props, f.getName()).withField(f); + } else if (isRecord) { + // 21-Oct-2024, tatu: [jackson-jr#167] Need to retain ordering of Record + // properties. One way is to pre-create properties like so. + _propFrom(props, f.getName()); } } diff --git a/jr-record-test/src/test-jdk17/java/jr/failing/RecordDeser167Test.java b/jr-record-test/src/test-jdk17/java/jr/RecordDeser167Test.java similarity index 98% rename from jr-record-test/src/test-jdk17/java/jr/failing/RecordDeser167Test.java rename to jr-record-test/src/test-jdk17/java/jr/RecordDeser167Test.java index fe0be53e..9ae96f9d 100644 --- a/jr-record-test/src/test-jdk17/java/jr/failing/RecordDeser167Test.java +++ b/jr-record-test/src/test-jdk17/java/jr/RecordDeser167Test.java @@ -1,4 +1,4 @@ -package jr.failing; +package jr; import com.fasterxml.jackson.jr.ob.JSON; diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 219773cf..bce4274e 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -69,3 +69,9 @@ Tomasz Gawęda (@TomaszGaweda) * Contributed #162: Add support for deserializing Java Records (2.18.0) + +Giovanni van der Schelde (@Giovds) + +* Reported, suggested a fix for #167: Deserialization of record fails + on constructor parameter ordering + (2.18.1) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 457740c9..cb0ccb1c 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -11,6 +11,11 @@ Modules: === Releases === ------------------------------------------------------------------------ +2.18.1 (not yet released) + +#167: Deserialization of record fails on constructor parameter ordering + (reported, fix suggested by Giovanni V-d-S) + 2.18.0 (26-Sep-2024) #162: Add support for deserializing Java Records