|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 11 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 14 | + |
| 15 | +// [databind#3355] Deserialization fails depending on the order of deserialized |
| 16 | +// objects with "Cannot construct instance (although at least one Creator exists)" |
| 17 | +public class CreatorWithIgnoreProperties3355Test |
| 18 | + extends DatabindTestUtil |
| 19 | +{ |
| 20 | + static class Common3355 { |
| 21 | + private final String property; |
| 22 | + private final ContainerFail3355 container; |
| 23 | + |
| 24 | + @JsonCreator |
| 25 | + public Common3355(@JsonProperty("property") final String property, |
| 26 | + @JsonProperty("container") final ContainerFail3355 container) { |
| 27 | + this.property = property; |
| 28 | + this.container = container; |
| 29 | + } |
| 30 | + |
| 31 | + public String getProperty() { |
| 32 | + return property; |
| 33 | + } |
| 34 | + |
| 35 | + public ContainerFail3355 getContainer() { |
| 36 | + return container; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static class ContainerFail3355 { |
| 41 | + private final Common3355 common; |
| 42 | + |
| 43 | + @JsonCreator |
| 44 | + public ContainerFail3355(@JsonProperty("common") final Common3355 common) { |
| 45 | + this.common = common; |
| 46 | + } |
| 47 | + |
| 48 | + @JsonIgnoreProperties("container") |
| 49 | + public Common3355 getCommon() { |
| 50 | + return common; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 55 | + |
| 56 | + @JacksonTestFailureExpected |
| 57 | + @Test |
| 58 | + public void testDeserFailing() throws Exception |
| 59 | + { |
| 60 | + final String objectJson = "{ \"property\": \"valueOne\" }"; |
| 61 | + final String containersJson = "{ \"common\": { \"property\": \"valueTwo\" } }"; |
| 62 | + |
| 63 | + // If we deserialize inner object first, outer object FAILS |
| 64 | + Common3355 object = MAPPER.readValue(objectJson, Common3355.class); |
| 65 | + ContainerFail3355 container = MAPPER.readValue(containersJson, ContainerFail3355.class); |
| 66 | + |
| 67 | + assertNotNull(object); |
| 68 | + assertNotNull(container); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testDeserPassing() throws Exception |
| 73 | + { |
| 74 | + final String objectJson = "{ \"property\": \"valueOne\" }"; |
| 75 | + final String containersJson = "{ \"common\": { \"property\": \"valueTwo\" } }"; |
| 76 | + |
| 77 | + // If we deserialize outer object first, it WORKS |
| 78 | + final ContainerFail3355 container = MAPPER.readValue(containersJson, ContainerFail3355.class); |
| 79 | + final Common3355 object = MAPPER.readValue(objectJson, Common3355.class); |
| 80 | + |
| 81 | + assertNotNull(object); |
| 82 | + assertNotNull(container); |
| 83 | + } |
| 84 | +} |
0 commit comments