From c3fdd3bc802face750d41c617e55c464de16393e Mon Sep 17 00:00:00 2001 From: Giovanni van der Schelde Date: Fri, 18 Oct 2024 12:25:37 +0200 Subject: [PATCH 1/4] Solve ordering issue of record constructor parameters --- .../jackson/jr/ob/impl/ValueReaderLocator.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java index 5daa77b1..1ab6783b 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java @@ -452,7 +452,6 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) final Map propMap; Map aliasMapping = null; - boolean isRecord = RecordsHelpers.isRecordType(raw); if (len == 0) { propMap = Collections.emptyMap(); } else { @@ -461,6 +460,7 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) // 13-May-2021, tatu: Let's retain ordering here: : new LinkedHashMap<>(); final boolean useFields = JSON.Feature.USE_FIELDS.isEnabled(_features); + final boolean isRecord = RecordsHelpers.isRecordType(raw); for (int i = 0; i < len; ++i) { POJODefinition.Prop rawProp = rawProps.get(i); Method setter = rawProp.setter; @@ -496,7 +496,17 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) } } - propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, i)); + if (isRecord) { + Parameter[] parameters = constructors._recordCtor.getParameters(); + for (int j = 0; j < parameters.length; j++) { + Parameter parameter = parameters[j]; + if (!parameter.getName().isBlank() && parameter.getName().equals(rawProp.name)) { + propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, j)); + } + } + } else { + propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, i)); + } // 25-Jan-2020, tatu: Aliases are a bit different because we can not tie them into // specific reader instance, due to resolution of cyclic dependencies. Instead, From 4d0d6289052eb883111d307e1d56218e4e269f92 Mon Sep 17 00:00:00 2001 From: Giovanni van der Schelde Date: Fri, 18 Oct 2024 12:31:07 +0200 Subject: [PATCH 2/4] Move test to not be excluded in build --- .../test-jdk17/java/jr/{failing => }/RecordDeser167Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename jr-record-test/src/test-jdk17/java/jr/{failing => }/RecordDeser167Test.java (98%) 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; From 16c9ff0666715a41a3b60b8223a97e337e5a9e05 Mon Sep 17 00:00:00 2001 From: Giovanni van der Schelde Date: Fri, 18 Oct 2024 13:24:28 +0200 Subject: [PATCH 3/4] Only loop over constructor parameters once --- .../jr/ob/impl/ValueReaderLocator.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java index 1ab6783b..3842c288 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java @@ -452,6 +452,18 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) final Map propMap; Map aliasMapping = null; + final Map ctorParameterIndexMap = new HashMap<>(); + final boolean isRecord = RecordsHelpers.isRecordType(raw); + if (isRecord) { + final Parameter[] parameters = constructors._recordCtor.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + if (!parameter.getName().isBlank()) { + ctorParameterIndexMap.put(parameter.getName(), i); + } + } + } + if (len == 0) { propMap = Collections.emptyMap(); } else { @@ -460,7 +472,6 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) // 13-May-2021, tatu: Let's retain ordering here: : new LinkedHashMap<>(); final boolean useFields = JSON.Feature.USE_FIELDS.isEnabled(_features); - final boolean isRecord = RecordsHelpers.isRecordType(raw); for (int i = 0; i < len; ++i) { POJODefinition.Prop rawProp = rawProps.get(i); Method setter = rawProp.setter; @@ -497,13 +508,8 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) } if (isRecord) { - Parameter[] parameters = constructors._recordCtor.getParameters(); - for (int j = 0; j < parameters.length; j++) { - Parameter parameter = parameters[j]; - if (!parameter.getName().isBlank() && parameter.getName().equals(rawProp.name)) { - propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, j)); - } - } + final Integer ctorIndex = ctorParameterIndexMap.get(rawProp.name); + propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, ctorIndex)); } else { propMap.put(rawProp.name, new BeanPropertyReader(rawProp.name, field, setter, i)); } From 87a43d96e37f88a56167beee937df4fd5bd89834 Mon Sep 17 00:00:00 2001 From: Giovanni van der Schelde Date: Fri, 18 Oct 2024 16:20:48 +0200 Subject: [PATCH 4/4] Use .isEmpty() for JDK8 --- .../com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java index 3842c288..eb82ad1c 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java @@ -458,7 +458,7 @@ protected BeanReader _resolveBeanForDeser(Class raw, POJODefinition beanDef) final Parameter[] parameters = constructors._recordCtor.getParameters(); for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; - if (!parameter.getName().isBlank()) { + if (!parameter.getName().isEmpty()) { ctorParameterIndexMap.put(parameter.getName(), i); } }