1
1
package com .fasterxml .jackson .databind .jsontype .deftyping ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonCreator ;
4
+ import com .fasterxml .jackson .annotation .JsonTypeInfo ;
5
+ import com .fasterxml .jackson .core .JsonProcessingException ;
6
+ import com .fasterxml .jackson .core .type .TypeReference ;
7
+ import com .fasterxml .jackson .databind .JavaType ;
8
+ import com .fasterxml .jackson .databind .jsontype .DefaultBaseTypeLimitingValidator ;
3
9
import java .util .concurrent .TimeUnit ;
4
10
5
11
import com .fasterxml .jackson .databind .BaseMapTest ;
@@ -26,6 +32,25 @@ protected static class TimeUnitBean {
26
32
public TimeUnit timeUnit ;
27
33
}
28
34
35
+ static class Foo3569 <T > {
36
+ public T item ;
37
+ }
38
+
39
+ enum Bar3569 {
40
+ ENABLED , DISABLED , HIDDEN ;
41
+
42
+ @ JsonCreator
43
+ public static Bar3569 fromValue (String value ) {
44
+ String upperVal = value .toUpperCase ();
45
+ for (Bar3569 enumValue : Bar3569 .values ()) {
46
+ if (enumValue .name ().equals (upperVal )) {
47
+ return enumValue ;
48
+ }
49
+ }
50
+ throw new IllegalArgumentException ("Bad input [" + value + "]" );
51
+ }
52
+ }
53
+
29
54
/*
30
55
/**********************************************************
31
56
/* Test methods
@@ -78,4 +103,32 @@ public void testSimpleEnumsAsField() throws Exception
78
103
EnumHolder holder = m .readValue (json , EnumHolder .class );
79
104
assertSame (TestEnum .B , holder .value );
80
105
}
106
+
107
+ /**
108
+ * [databind#3569]: Unable to deserialize enum object with default-typed
109
+ * {@link com.fasterxml.jackson.annotation.JsonTypeInfo.As#WRAPPER_ARRAY} and {@link JsonCreator} together,
110
+ *
111
+ * @since 2.16
112
+ */
113
+ public void testEnumAsWrapperArrayWithCreator () throws JsonProcessingException
114
+ {
115
+ ObjectMapper objectMapper = jsonMapperBuilder ()
116
+ .activateDefaultTyping (
117
+ new DefaultBaseTypeLimitingValidator (),
118
+ ObjectMapper .DefaultTyping .NON_FINAL_AND_ENUMS ,
119
+ JsonTypeInfo .As .WRAPPER_ARRAY )
120
+ .build ();
121
+
122
+ Foo3569 <Bar3569 > expected = new Foo3569 <>();
123
+ expected .item = Bar3569 .ENABLED ;
124
+
125
+ // First, serialize
126
+ String serialized = objectMapper .writeValueAsString (expected );
127
+
128
+ // Then, deserialize with TypeReference
129
+ assertNotNull (objectMapper .readValue (serialized , new TypeReference <Foo3569 <Bar3569 >>() {}));
130
+ // And, also try as described in [databind#3569]
131
+ JavaType javaType = objectMapper .getTypeFactory ().constructParametricType (Foo3569 .class , new Class []{Bar3569 .class });
132
+ assertNotNull (objectMapper .readValue (serialized , javaType ));
133
+ }
81
134
}
0 commit comments