Skip to content

Commit 10ef231

Browse files
committed
Merge branch '2.18'
2 parents 99bc5c1 + 5e0deda commit 10ef231

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

src/test-jdk17/java/tools/jackson/databind/records/RecordTypeInfo3342Test.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tools.jackson.databind.records;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
35
import com.fasterxml.jackson.annotation.JsonSubTypes;
46
import com.fasterxml.jackson.annotation.JsonTypeInfo;
57

@@ -9,6 +11,7 @@
911
import tools.jackson.databind.testutil.DatabindTestUtil;
1012

1113
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1215

1316
// [databind#3102]
1417
public class RecordTypeInfo3342Test extends DatabindTestUtil
@@ -39,6 +42,36 @@ public record Example(
3942
})
4043
SpiceTolerance tolerance) { }
4144

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+
4275
private final ObjectMapper MAPPER = newJsonMapper();
4376

4477
@Test
@@ -62,4 +95,15 @@ public void testSerializeDeserializeJsonSubType_HIGH() throws Exception {
6295
Example value = MAPPER.readValue(json, Example.class);
6396
assertEquals(record, value);
6497
}
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+
}
65109
}

src/test/java/tools/jackson/databind/deser/creators/ConstructorDetectorTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,56 @@ public SingleArgNotAnnotated(@ImplicitName("value") int value) {
2929
}
3030
}
3131

32+
static class SingleArgByte {
33+
protected byte v;
34+
35+
SingleArgByte() { v = -1; }
36+
37+
public SingleArgByte(@ImplicitName("value") byte value) {
38+
v = value;
39+
}
40+
}
41+
42+
static class SingleArgShort {
43+
protected short v;
44+
45+
SingleArgShort() { v = -1; }
46+
47+
public SingleArgShort(@ImplicitName("value") short value) {
48+
v = value;
49+
}
50+
}
51+
52+
static class SingleArgLong {
53+
protected long v;
54+
55+
SingleArgLong() { v = -1; }
56+
57+
public SingleArgLong(@ImplicitName("value") long value) {
58+
v = value;
59+
}
60+
}
61+
62+
static class SingleArgFloat {
63+
protected float v;
64+
65+
SingleArgFloat() { v = -1.0f; }
66+
67+
public SingleArgFloat(@ImplicitName("value") float value) {
68+
v = value;
69+
}
70+
}
71+
72+
static class SingleArgDouble {
73+
protected double v;
74+
75+
SingleArgDouble() { v = -1.0; }
76+
77+
public SingleArgDouble(@ImplicitName("value") double value) {
78+
v = value;
79+
}
80+
}
81+
3282
static class SingleArgNoMode {
3383
protected int v;
3484

@@ -110,6 +160,55 @@ public void test1ArgDefaultsToPropertiesNonAnnotated() throws Exception
110160
assertEquals(137, value.v);
111161
}
112162

163+
@Test
164+
public void test1ArgDefaultsToPropertiesNonAnnotatedDecimal() throws Exception
165+
{
166+
SingleArgNotAnnotated value = MAPPER_PROPS.readValue(a2q("{'value' : 137.0 }"),
167+
SingleArgNotAnnotated.class);
168+
assertEquals(137, value.v);
169+
}
170+
171+
@Test
172+
public void test1ArgDefaultsToPropertiesByte() throws Exception
173+
{
174+
SingleArgByte value = MAPPER_PROPS.readValue(a2q("{'value' : -99 }"),
175+
SingleArgByte.class);
176+
assertEquals(-99, value.v);
177+
}
178+
179+
@Test
180+
public void test1ArgDefaultsToPropertiesShort() throws Exception
181+
{
182+
SingleArgShort value = MAPPER_PROPS.readValue(a2q("{'value' : 137 }"),
183+
SingleArgShort.class);
184+
assertEquals(137, value.v);
185+
}
186+
187+
@Test
188+
public void test1ArgDefaultsToPropertiesLong() throws Exception
189+
{
190+
String val = Long.toString(Long.MAX_VALUE);
191+
SingleArgLong value = MAPPER_PROPS.readValue(a2q("{'value' : " + val + " }"),
192+
SingleArgLong.class);
193+
assertEquals(Long.MAX_VALUE, value.v);
194+
}
195+
196+
@Test
197+
public void test1ArgDefaultsToPropertiesFloat() throws Exception
198+
{
199+
SingleArgFloat value = MAPPER_PROPS.readValue(a2q("{'value' : 136.99 }"),
200+
SingleArgFloat.class);
201+
assertEquals(136.99f, value.v);
202+
}
203+
204+
@Test
205+
public void test1ArgDefaultsToPropertiesDouble() throws Exception
206+
{
207+
SingleArgDouble value = MAPPER_PROPS.readValue(a2q("{'value' : 999999999999999999.99 }"),
208+
SingleArgDouble.class);
209+
assertEquals(999999999999999999.99, value.v);
210+
}
211+
113212
@Test
114213
public void test1ArgDefaultsToPropertiesNoMode() throws Exception
115214
{

0 commit comments

Comments
 (0)