forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Records+JsonAnySetter workarounds shared in FasterXML#3439, as te…
…st cases.
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordWithJsonAnySetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; | ||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class RecordWithJsonAnySetterTest extends DatabindTestUtil { | ||
|
||
// Workaround mentioned in [databind#3439] | ||
record RecordWithAnySetterMethod(int id, Map<String, Object> any) { | ||
@JsonCreator | ||
public RecordWithAnySetterMethod(@JsonProperty("id") int id) { | ||
this(id, new LinkedHashMap<>()); | ||
} | ||
|
||
@JsonAnySetter | ||
private void updateProperty(String name, Object value) { | ||
any.put(name, value); | ||
} | ||
} | ||
|
||
// Workaround mentioned in [databind#3439] | ||
record RecordWithAnySetterComponentAltCtor(int id, @JsonAnySetter Map<String, Object> any) { | ||
@JsonCreator | ||
public RecordWithAnySetterComponentAltCtor(@JsonProperty("id") int id) { | ||
this(id, new LinkedHashMap<>()); | ||
} | ||
} | ||
|
||
// A proper one without workaround | ||
record RecordWithAnySetterComponentCanonicalCtor(int id, @JsonAnySetter Map<String, Object> any) { | ||
} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
@Test | ||
public void testDeserialize_WithAnySetterMethod() throws Exception { | ||
RecordWithAnySetterMethod value = MAPPER.readValue( | ||
a2q("{'id':123,'any1':'Any 1','any2':'Any 2'}"), RecordWithAnySetterMethod.class); | ||
|
||
Map<String, Object> expectedAny = new LinkedHashMap<>(); | ||
expectedAny.put("any1", "Any 1"); | ||
expectedAny.put("any2", "Any 2"); | ||
assertEquals(new RecordWithAnySetterMethod(123, expectedAny), value); | ||
} | ||
|
||
@Test | ||
public void testDeserialize_WithAnySetterComponent_WithAltConstructor() throws Exception { | ||
try { | ||
RecordWithAnySetterComponentAltCtor value = MAPPER.readValue( | ||
a2q("{'id':123,'any1':'Any 1','any2':'Any 2'}"), RecordWithAnySetterComponentAltCtor.class); | ||
fail("Used to fail, but is now: " + value); | ||
} catch (UnrecognizedPropertyException e) { | ||
// Used to work, but stopped working starting from 2.15.0 due to [databind#3737] | ||
verifyException(e, "Unrecognized field \"any1\""); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDeserialize_WithAnySetterComponent_WithCanonicalConstructor() throws Exception { | ||
RecordWithAnySetterComponentCanonicalCtor value = MAPPER.readValue( | ||
a2q("{'id':123,'any1':'Any 1','any2':'Any 2'}"), RecordWithAnySetterComponentCanonicalCtor.class); | ||
|
||
Map<String, Object> expectedAny = new LinkedHashMap<>(); | ||
expectedAny.put("any1", "Any 1"); | ||
expectedAny.put("any2", "Any 2"); | ||
assertEquals(new RecordWithAnySetterComponentCanonicalCtor(123, expectedAny), value); | ||
} | ||
} |