From 1661f472acd3ac6fc0993cbabf80b3f369ab9e9f Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 18 May 2024 19:16:47 -0700 Subject: [PATCH] Fix #4452 by introspecting Creators for Record serialization too --- release-notes/VERSION-2.x | 3 ++ .../introspect/POJOPropertiesCollector.java | 4 ++- .../RecordCreatorSerialization4452Test.java | 35 +++++++------------ 3 files changed, 19 insertions(+), 23 deletions(-) rename src/test-jdk17/java/com/fasterxml/jackson/databind/{failing => records}/RecordCreatorSerialization4452Test.java (67%) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 9afa1348b6..4d608245e7 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -6,6 +6,9 @@ Project: jackson-databind 2.18.0 (not yet released) +#4452: `@JsonProperty` not serializing field names properly + on `@JsonCreator` in Record + (reported by @Incara) #4453: Allow JSON Integer to deserialize into a single-arg constructor of parameter type `double` (contributed by David M) diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index 820fff485d..3bcbc9358a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -430,7 +430,9 @@ protected void collectAll() // 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static // inner classes, see [databind#1502] // 13-May-2023, PJ: Need to avoid adding creators for Records when serializing [databind#3925] - if (!_classDef.isNonStaticInnerClass() && !(_forSerialization && isRecord)) { + // 18-May-2024, tatu: Serialization side does, however, require access to renaming + // etc (see f.ex [databind#4452]) so let's not skip + if (!_classDef.isNonStaticInnerClass()) { // && !(_forSerialization && isRecord)) { _addCreators(props); } diff --git a/src/test-jdk17/java/com/fasterxml/jackson/databind/failing/RecordCreatorSerialization4452Test.java b/src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordCreatorSerialization4452Test.java similarity index 67% rename from src/test-jdk17/java/com/fasterxml/jackson/databind/failing/RecordCreatorSerialization4452Test.java rename to src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordCreatorSerialization4452Test.java index b3b4748272..e40d228769 100644 --- a/src/test-jdk17/java/com/fasterxml/jackson/databind/failing/RecordCreatorSerialization4452Test.java +++ b/src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordCreatorSerialization4452Test.java @@ -1,14 +1,15 @@ -package com.fasterxml.jackson.databind.failing; +package com.fasterxml.jackson.databind.records; + +import java.util.*; + +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; -import com.fasterxml.jackson.databind.records.Jdk8ConstructorParameterNameAnnotationIntrospector; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; // [databind#4452] : JsonProperty not serializing field names properly on JsonCreator in record #4452 class RecordCreatorSerialization4452Test { @@ -37,32 +38,22 @@ public CreatorTestObject( .annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector()) .build(); - // supposed to pass, and yes it does @Test - public void testPlain() - throws Exception + public void testPlain() throws Exception { String result = OBJECT_MAPPER.writeValueAsString(new PlainTestObject("test", 1)); assertEquals("{\"strField\":\"test\",\"intField\":1}", result); } - // Should pass but doesn't - // It did pass in 2.15 or earlier versions, but it fails in 2.16 or later @Test - public void testWithCreator() - throws Exception + public void testWithCreator() throws Exception { - String result = OBJECT_MAPPER + String json = OBJECT_MAPPER .writeValueAsString(new CreatorTestObject("test", 2, 1)); + //System.err.println("JSON: "+json); - /* - Serializes to (using System.err.println("JSON: "+result); ) - - {"testFieldName":"test","testOtherField":3} - - */ - assertTrue(result.contains("intField")); - assertTrue(result.contains("strField")); + @SuppressWarnings("unchecked") + Map asMap = (Map) OBJECT_MAPPER.readValue(json, Map.class); + assertEquals(new HashSet<>(Arrays.asList("intField", "strField")), asMap.keySet()); } - -} \ No newline at end of file +}