|
| 1 | +package tools.jackson.databind.jsontype; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.concurrent.atomic.AtomicReference; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 9 | + |
| 10 | +import tools.jackson.core.JacksonException; |
| 11 | +import tools.jackson.core.JsonGenerator; |
| 12 | +import tools.jackson.core.JsonParser; |
| 13 | + |
| 14 | +import tools.jackson.databind.*; |
| 15 | +import tools.jackson.databind.annotation.JsonDeserialize; |
| 16 | +import tools.jackson.databind.annotation.JsonSerialize; |
| 17 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test for Reference types (Optional, AtomicReference) with |
| 24 | + * {@code @JsonTypeInfo(use = Id.NONE)} override. |
| 25 | + */ |
| 26 | +class NoTypeInfo1654ReferenceTest extends DatabindTestUtil |
| 27 | +{ |
| 28 | + // [databind#1654] |
| 29 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) |
| 30 | + static class Value1654 { |
| 31 | + public int x; |
| 32 | + |
| 33 | + protected Value1654() { } |
| 34 | + |
| 35 | + public Value1654(int x) { |
| 36 | + this.x = x; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static class Value1654TypedOptionalContainer { |
| 41 | + public Optional<Value1654> value; |
| 42 | + |
| 43 | + protected Value1654TypedOptionalContainer() { } |
| 44 | + |
| 45 | + public Value1654TypedOptionalContainer(Value1654 v) { |
| 46 | + value = Optional.ofNullable(v); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + static class Value1654UntypedOptionalContainer { |
| 51 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NONE) |
| 52 | + public Optional<Value1654> value; |
| 53 | + |
| 54 | + protected Value1654UntypedOptionalContainer() { } |
| 55 | + |
| 56 | + public Value1654UntypedOptionalContainer(Value1654 v) { |
| 57 | + value = Optional.ofNullable(v); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static class Value1654UsingCustomSerDeserUntypedOptionalContainer { |
| 62 | + @JsonDeserialize(contentUsing = Value1654Deserializer.class) |
| 63 | + @JsonSerialize(contentUsing = Value1654Serializer.class) |
| 64 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NONE) |
| 65 | + public Optional<Value1654> value; |
| 66 | + |
| 67 | + protected Value1654UsingCustomSerDeserUntypedOptionalContainer() { } |
| 68 | + |
| 69 | + public Value1654UsingCustomSerDeserUntypedOptionalContainer(Value1654 v) { |
| 70 | + value = Optional.ofNullable(v); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + static class Value1654TypedAtomicRefContainer { |
| 75 | + public AtomicReference<Value1654> value; |
| 76 | + |
| 77 | + protected Value1654TypedAtomicRefContainer() { } |
| 78 | + |
| 79 | + public Value1654TypedAtomicRefContainer(Value1654 v) { |
| 80 | + value = new AtomicReference<>(v); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + static class Value1654UsingCustomSerDeserUntypedAtomicRefContainer { |
| 85 | + @JsonDeserialize(contentUsing = Value1654Deserializer.class) |
| 86 | + @JsonSerialize(contentUsing = Value1654Serializer.class) |
| 87 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NONE) |
| 88 | + public AtomicReference<Value1654> value; |
| 89 | + |
| 90 | + protected Value1654UsingCustomSerDeserUntypedAtomicRefContainer() { } |
| 91 | + |
| 92 | + public Value1654UsingCustomSerDeserUntypedAtomicRefContainer(Value1654 v) { |
| 93 | + value = new AtomicReference<>(v); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + static class Value1654Deserializer extends ValueDeserializer<Value1654> { |
| 98 | + @Override |
| 99 | + public Value1654 deserialize(JsonParser p, DeserializationContext ctxt) { |
| 100 | + JsonNode n = ctxt.readTree(p); |
| 101 | + if (!n.has("v")) { |
| 102 | + ctxt.reportInputMismatch(Value1654.class, "Bad JSON input (no 'v'): " + n); |
| 103 | + } |
| 104 | + return new Value1654(n.path("v").intValue()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + static class Value1654Serializer extends ValueSerializer<Value1654> { |
| 109 | + @Override |
| 110 | + public void serialize(Value1654 value, JsonGenerator gen, SerializationContext ctxt) |
| 111 | + throws JacksonException { |
| 112 | + gen.writeStartObject(value); |
| 113 | + gen.writeNumberProperty("v", value.x); |
| 114 | + gen.writeEndObject(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /* |
| 119 | + /********************************************************************** |
| 120 | + /* Test methods |
| 121 | + /********************************************************************** |
| 122 | + */ |
| 123 | + |
| 124 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 125 | + |
| 126 | + // [databind#1654]: no override, default polymorphic type id for Optional |
| 127 | + @Test |
| 128 | + void withoutNoTypeElementOverrideSerAndDeserOptional() throws Exception { |
| 129 | + Value1654TypedOptionalContainer cont = new Value1654TypedOptionalContainer(new Value1654(42)); |
| 130 | + String json = MAPPER.writeValueAsString(cont); |
| 131 | + String typeId = Value1654.class.getName(); |
| 132 | + typeId = "'@type':'" + typeId.substring(typeId.lastIndexOf('.') + 1) + "'"; |
| 133 | + assertEquals(a2q("{'value':{"+typeId+",'x':42}}"), json); |
| 134 | + |
| 135 | + Value1654TypedOptionalContainer result = MAPPER.readValue(json, Value1654TypedOptionalContainer.class); |
| 136 | + assertTrue(result.value.isPresent()); |
| 137 | + assertEquals(42, result.value.get().x); |
| 138 | + } |
| 139 | + |
| 140 | + // [databind#1654]: override, no polymorphic type id for Optional, serialization |
| 141 | + @Test |
| 142 | + void withNoTypeInfoDefaultSerOptional() throws Exception { |
| 143 | + Value1654UntypedOptionalContainer cont = new Value1654UntypedOptionalContainer(new Value1654(42)); |
| 144 | + assertEquals(a2q("{'value':{'x':42}}"), MAPPER.writeValueAsString(cont)); |
| 145 | + } |
| 146 | + |
| 147 | + // [databind#1654]: override, no polymorphic type id for Optional, deserialization |
| 148 | + @Test |
| 149 | + void withNoTypeInfoDefaultDeserOptional() throws Exception { |
| 150 | + String noTypeJson = a2q("{'value':{'x':42}}"); |
| 151 | + Value1654UntypedOptionalContainer result = MAPPER.readValue(noTypeJson, |
| 152 | + Value1654UntypedOptionalContainer.class); |
| 153 | + assertTrue(result.value.isPresent()); |
| 154 | + assertEquals(42, result.value.get().x); |
| 155 | + } |
| 156 | + |
| 157 | + // [databind#1654]: override, no polymorphic type id for Optional, custom serialization |
| 158 | + @Test |
| 159 | + void withNoTypeInfoOverrideSerOptional() throws Exception { |
| 160 | + Value1654UsingCustomSerDeserUntypedOptionalContainer cont = |
| 161 | + new Value1654UsingCustomSerDeserUntypedOptionalContainer(new Value1654(42)); |
| 162 | + assertEquals(a2q("{'value':{'v':42}}"), MAPPER.writeValueAsString(cont)); |
| 163 | + } |
| 164 | + |
| 165 | + // [databind#1654]: override, no polymorphic type id for Optional, custom deserialization |
| 166 | + @Test |
| 167 | + void withNoTypeInfoOverrideDeserOptional() throws Exception { |
| 168 | + String noTypeJson = a2q("{'value':{'v':42}}"); |
| 169 | + Value1654UsingCustomSerDeserUntypedOptionalContainer result = MAPPER.readValue(noTypeJson, |
| 170 | + Value1654UsingCustomSerDeserUntypedOptionalContainer.class); |
| 171 | + assertTrue(result.value.isPresent()); |
| 172 | + assertEquals(42, result.value.get().x); |
| 173 | + } |
| 174 | + |
| 175 | + // [databind#1654]: no override, default polymorphic type id for AtomicReference |
| 176 | + @Test |
| 177 | + void withoutNoTypeElementOverrideSerAndDeserAtomicRef() throws Exception { |
| 178 | + Value1654TypedAtomicRefContainer cont = new Value1654TypedAtomicRefContainer(new Value1654(42)); |
| 179 | + String json = MAPPER.writeValueAsString(cont); |
| 180 | + String typeId = Value1654.class.getName(); |
| 181 | + typeId = "'@type':'" + typeId.substring(typeId.lastIndexOf('.') + 1) + "'"; |
| 182 | + assertEquals(a2q("{'value':{"+typeId+",'x':42}}"), json); |
| 183 | + |
| 184 | + Value1654TypedAtomicRefContainer result = MAPPER.readValue(json, Value1654TypedAtomicRefContainer.class); |
| 185 | + assertEquals(42, result.value.get().x); |
| 186 | + } |
| 187 | + |
| 188 | + // [databind#1654]: override, no polymorphic type id for AtomicReference, custom serialization |
| 189 | + @Test |
| 190 | + void withNoTypeInfoOverrideSerAtomicRef() throws Exception { |
| 191 | + Value1654UsingCustomSerDeserUntypedAtomicRefContainer cont = |
| 192 | + new Value1654UsingCustomSerDeserUntypedAtomicRefContainer(new Value1654(42)); |
| 193 | + assertEquals(a2q("{'value':{'v':42}}"), MAPPER.writeValueAsString(cont)); |
| 194 | + } |
| 195 | + |
| 196 | + // [databind#1654]: override, no polymorphic type id for AtomicReference, custom deserialization |
| 197 | + @Test |
| 198 | + void withNoTypeInfoOverrideDeserAtomicRef() throws Exception { |
| 199 | + String noTypeJson = a2q("{'value':{'v':42}}"); |
| 200 | + Value1654UsingCustomSerDeserUntypedAtomicRefContainer result = MAPPER.readValue(noTypeJson, |
| 201 | + Value1654UsingCustomSerDeserUntypedAtomicRefContainer.class); |
| 202 | + assertEquals(42, result.value.get().x); |
| 203 | + } |
| 204 | +} |
0 commit comments