|
| 1 | +package com.fasterxml.jackson.databind.jsontype.jdk; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class EnumTyping4733Test extends DatabindTestUtil |
| 14 | +{ |
| 15 | + // Baseline case that already worked |
| 16 | + @JsonTypeInfo(use = Id.CLASS) |
| 17 | + @JsonSubTypes({ |
| 18 | + @JsonSubTypes.Type(value = A_CLASS.class), |
| 19 | + }) |
| 20 | + interface InterClass { |
| 21 | + default void yes() {} |
| 22 | + } |
| 23 | + |
| 24 | + enum A_CLASS implements InterClass { |
| 25 | + A1, |
| 26 | + A2 { |
| 27 | + @Override |
| 28 | + public void yes() { } |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + // Failed before fix for [databind#4733] |
| 33 | + @JsonTypeInfo(use = Id.MINIMAL_CLASS) |
| 34 | + @JsonSubTypes({ |
| 35 | + @JsonSubTypes.Type(value = A_MIN_CLASS.class), |
| 36 | + }) |
| 37 | + interface InterMinimalClass { |
| 38 | + default void yes() {} |
| 39 | + } |
| 40 | + |
| 41 | + enum A_MIN_CLASS implements InterMinimalClass { |
| 42 | + A1, |
| 43 | + A2 { |
| 44 | + @Override |
| 45 | + public void yes() { } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + // Failed before fix for [databind#4733] |
| 50 | + @JsonTypeInfo(use = Id.NAME) |
| 51 | + @JsonSubTypes({ |
| 52 | + @JsonSubTypes.Type(value = A_NAME.class), |
| 53 | + }) |
| 54 | + interface InterName { |
| 55 | + default void yes() {} |
| 56 | + } |
| 57 | + |
| 58 | + enum A_NAME implements InterName { |
| 59 | + A1, |
| 60 | + A2 { |
| 61 | + @Override |
| 62 | + public void yes() { } |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + // Failed before fix for [databind#4733] |
| 67 | + @JsonTypeInfo(use = Id.SIMPLE_NAME) |
| 68 | + @JsonSubTypes({ |
| 69 | + @JsonSubTypes.Type(value = A_SIMPLE_NAME.class), |
| 70 | + }) |
| 71 | + interface InterSimpleName { |
| 72 | + default void yes() {} |
| 73 | + } |
| 74 | + |
| 75 | + enum A_SIMPLE_NAME implements InterSimpleName { |
| 76 | + A1, |
| 77 | + A2 { |
| 78 | + @Override |
| 79 | + public void yes() { } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testIssue4733Class() throws Exception |
| 87 | + { |
| 88 | + String json1 = MAPPER.writeValueAsString(A_CLASS.A1); |
| 89 | + String json2 = MAPPER.writeValueAsString(A_CLASS.A2); |
| 90 | + |
| 91 | + assertEquals(A_CLASS.A1, MAPPER.readValue(json1, A_CLASS.class)); |
| 92 | + assertEquals(A_CLASS.A2, MAPPER.readValue(json2, A_CLASS.class)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testIssue4733MinimalClass() throws Exception |
| 97 | + { |
| 98 | + String json1 = MAPPER.writeValueAsString(A_MIN_CLASS.A1); |
| 99 | + String json2 = MAPPER.writeValueAsString(A_MIN_CLASS.A2); |
| 100 | + assertEquals(A_MIN_CLASS.A1, MAPPER.readValue(json1, A_MIN_CLASS.class), |
| 101 | + "JSON: "+json1); |
| 102 | + assertEquals(A_MIN_CLASS.A2, MAPPER.readValue(json2, A_MIN_CLASS.class), |
| 103 | + "JSON: "+json2); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testIssue4733Name() throws Exception |
| 108 | + { |
| 109 | + String json1 = MAPPER.writeValueAsString(A_NAME.A1); |
| 110 | + String json2 = MAPPER.writeValueAsString(A_NAME.A2); |
| 111 | + assertEquals(A_NAME.A1, MAPPER.readValue(json1, A_NAME.class), |
| 112 | + "JSON: "+json1); |
| 113 | + assertEquals(A_NAME.A2, MAPPER.readValue(json2, A_NAME.class), |
| 114 | + "JSON: "+json2); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testIssue4733SimpleName() throws Exception |
| 119 | + { |
| 120 | + String json1 = MAPPER.writeValueAsString(A_SIMPLE_NAME.A1); |
| 121 | + String json2 = MAPPER.writeValueAsString(A_SIMPLE_NAME.A2); |
| 122 | + assertEquals(A_SIMPLE_NAME.A1, MAPPER.readValue(json1, A_SIMPLE_NAME.class), |
| 123 | + "JSON: "+json1); |
| 124 | + assertEquals(A_SIMPLE_NAME.A2, MAPPER.readValue(json2, A_SIMPLE_NAME.class), |
| 125 | + "JSON: "+json2); |
| 126 | + } |
| 127 | +} |
0 commit comments