Skip to content

Commit 1661f47

Browse files
committed
Fix #4452 by introspecting Creators for Record serialization too
1 parent c95c2ca commit 1661f47

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Project: jackson-databind
66

77
2.18.0 (not yet released)
88

9+
#4452: `@JsonProperty` not serializing field names properly
10+
on `@JsonCreator` in Record
11+
(reported by @Incara)
912
#4453: Allow JSON Integer to deserialize into a single-arg constructor of
1013
parameter type `double`
1114
(contributed by David M)

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ protected void collectAll()
430430
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
431431
// inner classes, see [databind#1502]
432432
// 13-May-2023, PJ: Need to avoid adding creators for Records when serializing [databind#3925]
433-
if (!_classDef.isNonStaticInnerClass() && !(_forSerialization && isRecord)) {
433+
// 18-May-2024, tatu: Serialization side does, however, require access to renaming
434+
// etc (see f.ex [databind#4452]) so let's not skip
435+
if (!_classDef.isNonStaticInnerClass()) { // && !(_forSerialization && isRecord)) {
434436
_addCreators(props);
435437
}
436438

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package com.fasterxml.jackson.databind.failing;
1+
package com.fasterxml.jackson.databind.records;
2+
3+
import java.util.*;
4+
5+
import org.junit.jupiter.api.Test;
26

37
import com.fasterxml.jackson.annotation.JsonCreator;
48
import com.fasterxml.jackson.annotation.JsonProperty;
59
import com.fasterxml.jackson.databind.ObjectMapper;
610
import com.fasterxml.jackson.databind.json.JsonMapper;
7-
import com.fasterxml.jackson.databind.records.Jdk8ConstructorParameterNameAnnotationIntrospector;
8-
import org.junit.jupiter.api.Test;
911

1012
import static org.junit.jupiter.api.Assertions.assertEquals;
11-
import static org.junit.jupiter.api.Assertions.assertTrue;
1213

1314
// [databind#4452] : JsonProperty not serializing field names properly on JsonCreator in record #4452
1415
class RecordCreatorSerialization4452Test {
@@ -37,32 +38,22 @@ public CreatorTestObject(
3738
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector())
3839
.build();
3940

40-
// supposed to pass, and yes it does
4141
@Test
42-
public void testPlain()
43-
throws Exception
42+
public void testPlain() throws Exception
4443
{
4544
String result = OBJECT_MAPPER.writeValueAsString(new PlainTestObject("test", 1));
4645
assertEquals("{\"strField\":\"test\",\"intField\":1}", result);
4746
}
4847

49-
// Should pass but doesn't
50-
// It did pass in 2.15 or earlier versions, but it fails in 2.16 or later
5148
@Test
52-
public void testWithCreator()
53-
throws Exception
49+
public void testWithCreator() throws Exception
5450
{
55-
String result = OBJECT_MAPPER
51+
String json = OBJECT_MAPPER
5652
.writeValueAsString(new CreatorTestObject("test", 2, 1));
53+
//System.err.println("JSON: "+json);
5754

58-
/*
59-
Serializes to (using System.err.println("JSON: "+result); )
60-
61-
{"testFieldName":"test","testOtherField":3}
62-
63-
*/
64-
assertTrue(result.contains("intField"));
65-
assertTrue(result.contains("strField"));
55+
@SuppressWarnings("unchecked")
56+
Map<String, Object> asMap = (Map<String, Object>) OBJECT_MAPPER.readValue(json, Map.class);
57+
assertEquals(new HashSet<>(Arrays.asList("intField", "strField")), asMap.keySet());
6658
}
67-
68-
}
59+
}

0 commit comments

Comments
 (0)