Skip to content

Commit fb82b5b

Browse files
committed
Add test for, mark as fixed, #3439
1 parent f3c84db commit fb82b5b

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project: jackson-databind
1616
#3241: `constructorDetector` seems to invalidate `defaultSetterInfo`
1717
for nullability
1818
(reported by @joca-bt)
19+
#3439: Java Record `@JsonAnySetter` value is null after deserialization
20+
(reported by @oujesky)
1921
#4085: `@JsonView` does not work on class-level for records
2022
(reported by Ulf D)
2123
#4119: Exception when deserialization uses a record with a constructor
Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@
1212
import static org.junit.jupiter.api.Assertions.assertEquals;
1313

1414
// [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
1617
extends DatabindTestUtil
1718
{
18-
record RecordWithAnySetterCtor(int id,
19+
record RecordWithAnySetterCtor562(int id,
1920
Map<String, Integer> additionalProperties) {
2021
@JsonCreator
21-
public RecordWithAnySetterCtor(@JsonProperty("regular") int id,
22+
public RecordWithAnySetterCtor562(@JsonProperty("regular") int id,
2223
@JsonAnySetter Map<String, Integer> additionalProperties
2324
) {
2425
this.id = id;
2526
this.additionalProperties = additionalProperties;
2627
}
2728
}
2829

30+
record TestRecord3439(
31+
@JsonProperty String field,
32+
@JsonAnySetter Map<String, Object> anySetter
33+
) {}
34+
2935
/*
3036
/**********************************************************************
3137
/* Test methods, alternate constructors
@@ -34,21 +40,40 @@ public RecordWithAnySetterCtor(@JsonProperty("regular") int id,
3440

3541
private final ObjectMapper MAPPER = newJsonMapper();
3642

43+
// [databind#562]
3744
@Test
3845
public void testRecordWithAnySetterCtor() throws Exception
3946
{
4047
// 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);
4350
assertEquals(13, result.id);
4451
assertEquals(0, result.additionalProperties.size());
4552

4653
// Then with unknown properties
4754
result = MAPPER.readValue(a2q("{'regular':13, 'unknown':99, 'extra':-1}"),
48-
RecordWithAnySetterCtor.class);
55+
RecordWithAnySetterCtor562.class);
4956
assertEquals(13, result.id);
5057
assertEquals(Integer.valueOf(99), result.additionalProperties.get("unknown"));
5158
assertEquals(Integer.valueOf(-1), result.additionalProperties.get("extra"));
5259
assertEquals(2, result.additionalProperties.size());
5360
}
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+
}
5479
}

0 commit comments

Comments
 (0)