Skip to content

Commit 7e74bb9

Browse files
committed
Add test to verify #3251 is fixed by 2.16
1 parent d6f005d commit 7e74bb9

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
<artifactId>junit-jupiter</artifactId>
100100
<scope>test</scope>
101101
</dependency>
102+
<dependency> <!-- added in 2.16 -->
103+
<groupId>org.assertj</groupId>
104+
<artifactId>assertj-core</artifactId>
105+
<scope>test</scope>
106+
</dependency>
102107
<dependency>
103108
<groupId>org.powermock</groupId>
104109
<artifactId>powermock-core</artifactId>

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,11 @@ Antti Lampinen (arlampin@github)
16571657
and is marked `Access.WRITE_ONLY`
16581658
(2.15.1)
16591659
1660+
Kevin Baes (BaesKevin@github)
1661+
* Reported #3251: Generic class with generic field of runtime type `Double` is deserialized
1662+
as `BigDecimal` when used with `@JsonTypeInfo` and `JsonTypeInfo.As.EXISTING_PROPERTY`
1663+
(2.16.0)
1664+
16601665
David Schlosnagle (schlosna@github)
16611666
* Contributed #4008: Optimize `ObjectNode` findValue(s) and findParent(s) fast paths
16621667
(2.16.0)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Project: jackson-databind
1010
(contributed by Joo-Hyuk K)
1111
#2787: Mix-ins do not work for `Enum`s
1212
(fix contributed by Joo-Hyuk K)
13+
#3251: Generic class with generic field of runtime type `Double` is deserialized
14+
as `BigDecimal` when used with `@JsonTypeInfo` and `JsonTypeInfo.As.EXISTING_PROPERTY`
15+
(reported by Kevin B)
1316
#3647: `@JsonIgnoreProperties` not working with `@JsonValue`
1417
(reported by @ThatSneakyRaccoon)
1518
(fix contributed by Joo-Hyuk K)

src/test/java/com/fasterxml/jackson/databind/jsontype/ExistingPropertyTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.fasterxml.jackson.databind.BaseMapTest;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
1313

14+
import static org.assertj.core.api.Assertions.assertThat;
15+
1416
public class ExistingPropertyTest extends BaseMapTest
1517
{
1618
/**
@@ -199,6 +201,45 @@ public interface Base2785 {
199201
static class Impl2785 implements Base2785 {
200202
}
201203

204+
// [databind#3251]: Double vs BigDecimal
205+
@JsonTypeInfo(
206+
use = JsonTypeInfo.Id.NAME,
207+
property = "type_alias"
208+
)
209+
static class GenericWrapperWithNew3251<T> {
210+
private final T value;
211+
212+
@JsonCreator
213+
public GenericWrapperWithNew3251(@JsonProperty("value") T value) {
214+
this.value = value;
215+
}
216+
217+
public T getValue() {
218+
return value;
219+
}
220+
}
221+
222+
@JsonTypeInfo(
223+
use = JsonTypeInfo.Id.NAME,
224+
include = JsonTypeInfo.As.EXISTING_PROPERTY,
225+
property = "fieldType",
226+
visible = true,
227+
defaultImpl = GenericWrapperWithExisting3251.class
228+
)
229+
static class GenericWrapperWithExisting3251<T> {
230+
public String fieldType;
231+
private final T value;
232+
233+
@JsonCreator
234+
public GenericWrapperWithExisting3251(@JsonProperty("value") T value) {
235+
this.value = value;
236+
}
237+
238+
public T getValue() {
239+
return value;
240+
}
241+
}
242+
202243
// [databind#3271]
203244
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
204245
visible = true, property = "type", defaultImpl = DefaultShape3271.class)
@@ -490,4 +531,30 @@ public void testDeserializationNull() throws Exception {
490531
Shape3271 deserShape = MAPPER.readValue("{\"type\":null}", Shape3271.class);
491532
assertNull(deserShape.getType()); // error: "expected null, but was:<null>"
492533
}
534+
535+
// [databind#3251]: Double vs BigDecimal
536+
public void test3251WithNewProperty() throws Exception
537+
{
538+
GenericWrapperWithNew3251<?> wrapper = new GenericWrapperWithNew3251<>(123.5);
539+
540+
String json = MAPPER.writeValueAsString(wrapper);
541+
GenericWrapperWithNew3251<?> actualWrapper = MAPPER.readValue(json, GenericWrapperWithNew3251.class);
542+
543+
assertThat(actualWrapper).satisfies(it -> assertThat(it.getValue()).isEqualTo(123.5));
544+
assertThat(actualWrapper.getValue()).isInstanceOf(Double.class);
545+
assertThat(json).contains("\"value\":123.5");
546+
}
547+
548+
public void test3251WithExistingProperty() throws Exception
549+
{
550+
GenericWrapperWithExisting3251<?> wrapper = new GenericWrapperWithExisting3251<>(123.5);
551+
552+
String json = MAPPER.writeValueAsString(wrapper);
553+
GenericWrapperWithExisting3251<?> actualWrapper = MAPPER.readValue(json, GenericWrapperWithExisting3251.class);
554+
555+
assertThat(actualWrapper).satisfies(it -> assertThat(it.getValue()).isEqualTo(123.5));
556+
assertThat(actualWrapper.getValue()).isInstanceOf(Double.class);
557+
assertThat(json).contains("\"value\":123.5");
558+
}
559+
493560
}

0 commit comments

Comments
 (0)