-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for issue 3342 (using JsonTypeInfo.As.EXTERNAL_PROPERTY…
… with Records).
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordTypeInfo3342Test.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,58 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
// [databind#3102] | ||
public class RecordTypeInfo3342Test extends BaseMapTest | ||
{ | ||
public enum SpiceLevel { | ||
LOW, | ||
HIGH | ||
} | ||
|
||
public interface SpiceTolerance { | ||
} | ||
|
||
public record LowSpiceTolerance(String food) implements SpiceTolerance { | ||
} | ||
|
||
public record HighSpiceTolerance(String food) implements SpiceTolerance { | ||
} | ||
|
||
public record Example( | ||
SpiceLevel level, | ||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.EXTERNAL_PROPERTY, | ||
property = "level") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = LowSpiceTolerance.class, name = "LOW"), | ||
@JsonSubTypes.Type(value = HighSpiceTolerance.class, name = "HIGH") | ||
}) | ||
SpiceTolerance tolerance) { } | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
public void testSerializeDeserializeJsonSubType_LOW() throws Exception { | ||
Example record = new Example(SpiceLevel.LOW, new LowSpiceTolerance("Tomato")); | ||
|
||
String json = MAPPER.writeValueAsString(record); | ||
assertEquals("{\"level\":\"LOW\",\"tolerance\":{\"food\":\"Tomato\"}}", json); | ||
|
||
Example value = MAPPER.readValue(json, Example.class); | ||
assertEquals(record, value); | ||
} | ||
|
||
public void testSerializeDeserializeJsonSubType_HIGH() throws Exception { | ||
Example record = new Example(SpiceLevel.HIGH, new HighSpiceTolerance("Chilli")); | ||
|
||
String json = MAPPER.writeValueAsString(record); | ||
assertEquals("{\"level\":\"HIGH\",\"tolerance\":{\"food\":\"Chilli\"}}", json); | ||
|
||
Example value = MAPPER.readValue(json, Example.class); | ||
assertEquals(record, value); | ||
} | ||
} |