Skip to content

Commit

Permalink
Add test for, mark as fixed, #3439
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 22, 2024
1 parent f3c84db commit fb82b5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project: jackson-databind
#3241: `constructorDetector` seems to invalidate `defaultSetterInfo`
for nullability
(reported by @joca-bt)
#3439: Java Record `@JsonAnySetter` value is null after deserialization
(reported by @oujesky)
#4085: `@JsonView` does not work on class-level for records
(reported by Ulf D)
#4119: Exception when deserialization uses a record with a constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

// [databind#562] Allow @JsonAnySetter on Creator constructors
public class RecordCreatorWithAnySetter562Test
// [databind#3439] Java Record @JsonAnySetter value is null after deserialization
public class RecordCreatorWithAnySetterTest
extends DatabindTestUtil
{
record RecordWithAnySetterCtor(int id,
record RecordWithAnySetterCtor562(int id,
Map<String, Integer> additionalProperties) {
@JsonCreator
public RecordWithAnySetterCtor(@JsonProperty("regular") int id,
public RecordWithAnySetterCtor562(@JsonProperty("regular") int id,
@JsonAnySetter Map<String, Integer> additionalProperties
) {
this.id = id;
this.additionalProperties = additionalProperties;
}
}

record TestRecord3439(
@JsonProperty String field,
@JsonAnySetter Map<String, Object> anySetter
) {}

/*
/**********************************************************************
/* Test methods, alternate constructors
Expand All @@ -34,21 +40,40 @@ public RecordWithAnySetterCtor(@JsonProperty("regular") int id,

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#562]
@Test
public void testRecordWithAnySetterCtor() throws Exception
{
// First, only regular property mapped
RecordWithAnySetterCtor result = MAPPER.readValue(a2q("{'regular':13}"),
RecordWithAnySetterCtor.class);
RecordWithAnySetterCtor562 result = MAPPER.readValue(a2q("{'regular':13}"),
RecordWithAnySetterCtor562.class);
assertEquals(13, result.id);
assertEquals(0, result.additionalProperties.size());

// Then with unknown properties
result = MAPPER.readValue(a2q("{'regular':13, 'unknown':99, 'extra':-1}"),
RecordWithAnySetterCtor.class);
RecordWithAnySetterCtor562.class);
assertEquals(13, result.id);
assertEquals(Integer.valueOf(99), result.additionalProperties.get("unknown"));
assertEquals(Integer.valueOf(-1), result.additionalProperties.get("extra"));
assertEquals(2, result.additionalProperties.size());
}

// [databind#3439]
@Test
public void testJsonAnySetterOnRecord() throws Exception {
String json = """
{
"field": "value",
"unmapped1": "value1",
"unmapped2": "value2"
}
""";

TestRecord3439 result = MAPPER.readValue(json, TestRecord3439.class);

assertEquals("value", result.field());
assertEquals(Map.of("unmapped1", "value1", "unmapped2", "value2"),
result.anySetter());
}
}

0 comments on commit fb82b5b

Please sign in to comment.