12
12
import static org .junit .jupiter .api .Assertions .assertEquals ;
13
13
14
14
// [databind#562] Allow @JsonAnySetter on Creator constructors
15
- public class RecordCreatorWithAnySetter562Test
15
+ // [databind#3439] Java Record @JsonAnySetter value is null after deserialization
16
+ public class RecordCreatorWithAnySetterTest
16
17
extends DatabindTestUtil
17
18
{
18
- record RecordWithAnySetterCtor (int id ,
19
+ record RecordWithAnySetterCtor562 (int id ,
19
20
Map <String , Integer > additionalProperties ) {
20
21
@ JsonCreator
21
- public RecordWithAnySetterCtor (@ JsonProperty ("regular" ) int id ,
22
+ public RecordWithAnySetterCtor562 (@ JsonProperty ("regular" ) int id ,
22
23
@ JsonAnySetter Map <String , Integer > additionalProperties
23
24
) {
24
25
this .id = id ;
25
26
this .additionalProperties = additionalProperties ;
26
27
}
27
28
}
28
29
30
+ record TestRecord3439 (
31
+ @ JsonProperty String field ,
32
+ @ JsonAnySetter Map <String , Object > anySetter
33
+ ) {}
34
+
29
35
/*
30
36
/**********************************************************************
31
37
/* Test methods, alternate constructors
@@ -34,21 +40,40 @@ public RecordWithAnySetterCtor(@JsonProperty("regular") int id,
34
40
35
41
private final ObjectMapper MAPPER = newJsonMapper ();
36
42
43
+ // [databind#562]
37
44
@ Test
38
45
public void testRecordWithAnySetterCtor () throws Exception
39
46
{
40
47
// First, only regular property mapped
41
- RecordWithAnySetterCtor result = MAPPER .readValue (a2q ("{'regular':13}" ),
42
- RecordWithAnySetterCtor .class );
48
+ RecordWithAnySetterCtor562 result = MAPPER .readValue (a2q ("{'regular':13}" ),
49
+ RecordWithAnySetterCtor562 .class );
43
50
assertEquals (13 , result .id );
44
51
assertEquals (0 , result .additionalProperties .size ());
45
52
46
53
// Then with unknown properties
47
54
result = MAPPER .readValue (a2q ("{'regular':13, 'unknown':99, 'extra':-1}" ),
48
- RecordWithAnySetterCtor .class );
55
+ RecordWithAnySetterCtor562 .class );
49
56
assertEquals (13 , result .id );
50
57
assertEquals (Integer .valueOf (99 ), result .additionalProperties .get ("unknown" ));
51
58
assertEquals (Integer .valueOf (-1 ), result .additionalProperties .get ("extra" ));
52
59
assertEquals (2 , result .additionalProperties .size ());
53
60
}
61
+
62
+ // [databind#3439]
63
+ @ Test
64
+ public void testJsonAnySetterOnRecord () throws Exception {
65
+ String json = """
66
+ {
67
+ "field": "value",
68
+ "unmapped1": "value1",
69
+ "unmapped2": "value2"
70
+ }
71
+ """ ;
72
+
73
+ TestRecord3439 result = MAPPER .readValue (json , TestRecord3439 .class );
74
+
75
+ assertEquals ("value" , result .field ());
76
+ assertEquals (Map .of ("unmapped1" , "value1" , "unmapped2" , "value2" ),
77
+ result .anySetter ());
78
+ }
54
79
}
0 commit comments