Skip to content

Commit

Permalink
Add test to verify #3251 is fixed by 2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 3, 2023
1 parent d6f005d commit 7e74bb9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- added in 2.16 -->
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,11 @@ Antti Lampinen (arlampin@github)
and is marked `Access.WRITE_ONLY`
(2.15.1)
Kevin Baes (BaesKevin@github)
* Reported #3251: Generic class with generic field of runtime type `Double` is deserialized
as `BigDecimal` when used with `@JsonTypeInfo` and `JsonTypeInfo.As.EXISTING_PROPERTY`
(2.16.0)
David Schlosnagle (schlosna@github)
* Contributed #4008: Optimize `ObjectNode` findValue(s) and findParent(s) fast paths
(2.16.0)
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Project: jackson-databind
(contributed by Joo-Hyuk K)
#2787: Mix-ins do not work for `Enum`s
(fix contributed by Joo-Hyuk K)
#3251: Generic class with generic field of runtime type `Double` is deserialized
as `BigDecimal` when used with `@JsonTypeInfo` and `JsonTypeInfo.As.EXISTING_PROPERTY`
(reported by Kevin B)
#3647: `@JsonIgnoreProperties` not working with `@JsonValue`
(reported by @ThatSneakyRaccoon)
(fix contributed by Joo-Hyuk K)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.assertj.core.api.Assertions.assertThat;

public class ExistingPropertyTest extends BaseMapTest
{
/**
Expand Down Expand Up @@ -199,6 +201,45 @@ public interface Base2785 {
static class Impl2785 implements Base2785 {
}

// [databind#3251]: Double vs BigDecimal
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type_alias"
)
static class GenericWrapperWithNew3251<T> {
private final T value;

@JsonCreator
public GenericWrapperWithNew3251(@JsonProperty("value") T value) {
this.value = value;
}

public T getValue() {
return value;
}
}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "fieldType",
visible = true,
defaultImpl = GenericWrapperWithExisting3251.class
)
static class GenericWrapperWithExisting3251<T> {
public String fieldType;
private final T value;

@JsonCreator
public GenericWrapperWithExisting3251(@JsonProperty("value") T value) {
this.value = value;
}

public T getValue() {
return value;
}
}

// [databind#3271]
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
visible = true, property = "type", defaultImpl = DefaultShape3271.class)
Expand Down Expand Up @@ -490,4 +531,30 @@ public void testDeserializationNull() throws Exception {
Shape3271 deserShape = MAPPER.readValue("{\"type\":null}", Shape3271.class);
assertNull(deserShape.getType()); // error: "expected null, but was:<null>"
}

// [databind#3251]: Double vs BigDecimal
public void test3251WithNewProperty() throws Exception
{
GenericWrapperWithNew3251<?> wrapper = new GenericWrapperWithNew3251<>(123.5);

String json = MAPPER.writeValueAsString(wrapper);
GenericWrapperWithNew3251<?> actualWrapper = MAPPER.readValue(json, GenericWrapperWithNew3251.class);

assertThat(actualWrapper).satisfies(it -> assertThat(it.getValue()).isEqualTo(123.5));
assertThat(actualWrapper.getValue()).isInstanceOf(Double.class);
assertThat(json).contains("\"value\":123.5");
}

public void test3251WithExistingProperty() throws Exception
{
GenericWrapperWithExisting3251<?> wrapper = new GenericWrapperWithExisting3251<>(123.5);

String json = MAPPER.writeValueAsString(wrapper);
GenericWrapperWithExisting3251<?> actualWrapper = MAPPER.readValue(json, GenericWrapperWithExisting3251.class);

assertThat(actualWrapper).satisfies(it -> assertThat(it.getValue()).isEqualTo(123.5));
assertThat(actualWrapper.getValue()).isInstanceOf(Double.class);
assertThat(json).contains("\"value\":123.5");
}

}

0 comments on commit 7e74bb9

Please sign in to comment.