1
1
package com .fasterxml .jackson .databind .records ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonCreator ;
4
+ import com .fasterxml .jackson .annotation .JsonProperty ;
3
5
import com .fasterxml .jackson .annotation .JsonSubTypes ;
4
6
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
7
+
5
8
import com .fasterxml .jackson .databind .ObjectMapper ;
6
9
import com .fasterxml .jackson .databind .testutil .DatabindTestUtil ;
10
+
7
11
import org .junit .jupiter .api .Test ;
8
12
9
13
import static org .junit .jupiter .api .Assertions .assertEquals ;
14
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
10
15
11
16
// [databind#3102]
12
17
public class RecordTypeInfo3342Test extends DatabindTestUtil
@@ -37,6 +42,36 @@ public record Example(
37
42
})
38
43
SpiceTolerance tolerance ) { }
39
44
45
+ // Test from https://github.com/FasterXML/jackson-modules-base/pull/249
46
+
47
+ static record RootRecord249 (AbstractMember249 member ) {
48
+ }
49
+
50
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , include = JsonTypeInfo .As .PROPERTY , property = "@class" )
51
+ @ JsonSubTypes ({
52
+ @ JsonSubTypes .Type (value = StringMember .class , name = "string" ),
53
+ @ JsonSubTypes .Type (value = IntMember .class , name = "int" )
54
+ })
55
+ static abstract class AbstractMember249 { }
56
+
57
+ static final class StringMember extends AbstractMember249 {
58
+ final String val ;
59
+
60
+ @ JsonCreator
61
+ public StringMember (@ JsonProperty ("val" ) String val ) {
62
+ this .val = val ;
63
+ }
64
+ }
65
+
66
+ static final class IntMember extends AbstractMember249 {
67
+ final int val ;
68
+
69
+ @ JsonCreator
70
+ public IntMember (@ JsonProperty ("val" ) int val ) {
71
+ this .val = val ;
72
+ }
73
+ }
74
+
40
75
private final ObjectMapper MAPPER = newJsonMapper ();
41
76
42
77
@ Test
@@ -60,4 +95,15 @@ public void testSerializeDeserializeJsonSubType_HIGH() throws Exception {
60
95
Example value = MAPPER .readValue (json , Example .class );
61
96
assertEquals (record , value );
62
97
}
98
+
99
+ // Test from https://github.com/FasterXML/jackson-modules-base/pull/249
100
+ @ Test
101
+ public void testDeserializeRecordWithAbstractMember () throws Exception {
102
+ RootRecord249 value = MAPPER .readValue (
103
+ "{\" member\" :{\" @class\" :\" string\" ,\" val\" :\" Hello, abstract member!\" }}" ,
104
+ RootRecord249 .class );
105
+ assertNotNull (value .member ());
106
+ assertEquals (StringMember .class , value .member ().getClass ());
107
+ assertEquals ("Hello, abstract member!" , ((StringMember )value .member ()).val );
108
+ }
63
109
}
0 commit comments