1
+ package com .fasterxml .jackson .databind .failing ;
2
+
3
+ import com .fasterxml .jackson .annotation .JsonCreator ;
4
+ import com .fasterxml .jackson .annotation .JsonProperty ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
7
+ import com .fasterxml .jackson .databind .records .Jdk8ConstructorParameterNameAnnotationIntrospector ;
8
+ import org .junit .jupiter .api .Test ;
9
+
10
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
11
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
12
+
13
+ // [databind#4452] : JsonProperty not serializing field names properly on JsonCreator in record #4452
14
+ class RecordCreatorSerialization4452Test {
15
+
16
+ public record PlainTestObject (
17
+ @ JsonProperty ("strField" ) String testFieldName ,
18
+ @ JsonProperty ("intField" ) Integer testOtherField
19
+ ) { }
20
+
21
+ public record CreatorTestObject (
22
+ String testFieldName ,
23
+ Integer testOtherField
24
+ ) {
25
+ @ JsonCreator
26
+ public CreatorTestObject (
27
+ @ JsonProperty ("strField" ) String testFieldName ,
28
+ @ JsonProperty ("someOtherIntField" ) Integer testOtherIntField ,
29
+ @ JsonProperty ("intField" ) Integer testOtherField )
30
+ {
31
+ this (testFieldName , testOtherField + testOtherIntField );
32
+ }
33
+ }
34
+
35
+ private final ObjectMapper OBJECT_MAPPER =
36
+ JsonMapper .builder ()
37
+ .annotationIntrospector (new Jdk8ConstructorParameterNameAnnotationIntrospector ())
38
+ .build ();
39
+
40
+ // supposed to pass, and yes it does
41
+ @ Test
42
+ public void testPlain ()
43
+ throws Exception
44
+ {
45
+ String result = OBJECT_MAPPER .writeValueAsString (new PlainTestObject ("test" , 1 ));
46
+ assertEquals ("{\" strField\" :\" test\" ,\" intField\" :1}" , result );
47
+ }
48
+
49
+ // Should pass but doesn't
50
+ // It did pass in 2.15 or earlier versions, but it fails in 2.16 or later
51
+ @ Test
52
+ public void testWithCreator ()
53
+ throws Exception
54
+ {
55
+ String result = OBJECT_MAPPER
56
+ .writeValueAsString (new CreatorTestObject ("test" , 2 , 1 ));
57
+ /*
58
+ Serializes to:
59
+
60
+ {"testFieldName":"test","testOtherField":3}
61
+
62
+ */
63
+ assertTrue (result .contains ("intField" ));
64
+ assertTrue (result .contains ("strField" ));
65
+ }
66
+
67
+ }
0 commit comments