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 65660de5..24675f84 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -15,6 +15,11 @@ Modules: - +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