|
11 | 11 | import com.fasterxml.jackson.databind.BaseMapTest;
|
12 | 12 | import com.fasterxml.jackson.databind.ObjectMapper;
|
13 | 13 |
|
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
14 | 16 | public class ExistingPropertyTest extends BaseMapTest
|
15 | 17 | {
|
16 | 18 | /**
|
@@ -199,6 +201,45 @@ public interface Base2785 {
|
199 | 201 | static class Impl2785 implements Base2785 {
|
200 | 202 | }
|
201 | 203 |
|
| 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 | + |
202 | 243 | // [databind#3271]
|
203 | 244 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
|
204 | 245 | visible = true, property = "type", defaultImpl = DefaultShape3271.class)
|
@@ -490,4 +531,30 @@ public void testDeserializationNull() throws Exception {
|
490 | 531 | Shape3271 deserShape = MAPPER.readValue("{\"type\":null}", Shape3271.class);
|
491 | 532 | assertNull(deserShape.getType()); // error: "expected null, but was:<null>"
|
492 | 533 | }
|
| 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 | + |
493 | 560 | }
|
0 commit comments