|
| 1 | +package com.fasterxml.jackson.databind.objectid; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.*; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 13 | + |
| 14 | +import static org.junit.Assert.fail; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +public class ObjectIdSubTypes4610Test extends DatabindTestUtil |
| 18 | +{ |
| 19 | + // Unused @JsonIdentityInfo |
| 20 | + @JsonIdentityInfo(generator = ObjectIdGenerators.StringIdGenerator.class) |
| 21 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) |
| 22 | + @JsonSubTypes({ |
| 23 | + @JsonSubTypes.Type(value = EnumTypeDefinition.class, name = "enum"), |
| 24 | + @JsonSubTypes.Type(value = NumberTypeDefinition.class, name = "number") |
| 25 | + }) |
| 26 | + interface TypeDefinition { |
| 27 | + } |
| 28 | + |
| 29 | + static class EnumTypeDefinition implements TypeDefinition { |
| 30 | + public List<String> values; |
| 31 | + } |
| 32 | + |
| 33 | + static class NumberTypeDefinition implements TypeDefinition { |
| 34 | + } |
| 35 | + |
| 36 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void shouldHandleTypeDefinitionJson() throws Exception { |
| 40 | + String input = "{\"@type\": \"number\"}"; |
| 41 | + |
| 42 | + TypeDefinition model = MAPPER.readerFor(TypeDefinition.class) |
| 43 | + .without(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS) |
| 44 | + .readValue(input); |
| 45 | + |
| 46 | + Assertions.assertInstanceOf(NumberTypeDefinition.class, model); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testRoundTrip() throws Exception { |
| 51 | + // Ser |
| 52 | + String JSON = MAPPER.writeValueAsString(new NumberTypeDefinition()); |
| 53 | + assertTrue(JSON.contains("@id")); |
| 54 | + |
| 55 | + // Deser |
| 56 | + TypeDefinition model = MAPPER.readerFor(TypeDefinition.class) |
| 57 | + .with(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS) |
| 58 | + .readValue(JSON); |
| 59 | + Assertions.assertInstanceOf(NumberTypeDefinition.class, model); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void shouldHandleTypeDefinitionJsonFail() throws Exception { |
| 64 | + String JSON = "{\"@type\": \"number\"}"; |
| 65 | + |
| 66 | + try { |
| 67 | + /*TypeDefinition model =*/ MAPPER.readerFor(TypeDefinition.class) |
| 68 | + .with(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS) |
| 69 | + .readValue(JSON); |
| 70 | + fail("Should not pass"); |
| 71 | + } catch (Exception e) { |
| 72 | + assertTrue(e.getMessage().startsWith("No Object Id found for an instance of")); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments