Skip to content

Commit 70ba54f

Browse files
committed
Fix #3280
1 parent 216de27 commit 70ba54f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
(not yet released)
8+
9+
#3280: Can not deserialize json to enum value with Object-/Array-valued input,
10+
`@JsonCreator`
11+
(reported by peteryuanpan@github)
12+
713
2.12.5 (27-Aug-2021)
814

915
#3220: (regression) Factory method generic type resolution does not use

src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ protected Object deserializeEnumUsingPropertyBased(final JsonParser p, final Des
197197
continue;
198198
}
199199
// 26-Nov-2020, tatu: ... what should we do here tho?
200+
p.skipChildren();
200201
}
201202
return creator.build(ctxt, buffer);
202203
}

src/test/java/com/fasterxml/jackson/databind/deser/creators/EnumCreatorTest.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.fasterxml.jackson.databind.deser.creators;
22

33
import java.math.BigDecimal;
4-
import java.util.*;
4+
import java.util.Collection;
5+
import java.util.EnumMap;
6+
import java.util.EnumSet;
7+
import java.util.HashMap;
8+
import java.util.List;
59

610
import com.fasterxml.jackson.annotation.JsonCreator;
711
import com.fasterxml.jackson.annotation.JsonGetter;
@@ -185,13 +189,33 @@ public String toString() {
185189
}
186190
}
187191

192+
// [databind#3280]
193+
static enum Enum3280 {
194+
x("x"),
195+
y("y"),
196+
z("z");
197+
private final String value;
198+
Enum3280(String value) {
199+
this.value = value;
200+
}
201+
@JsonCreator
202+
public static Enum3280 getByValue(@JsonProperty("b") String value) {
203+
for (Enum3280 e : Enum3280.values()) {
204+
if (e.value.equals(value)) {
205+
return e;
206+
}
207+
}
208+
return null;
209+
}
210+
}
211+
188212
/*
189213
/**********************************************************
190214
/* Test methods
191215
/**********************************************************
192216
*/
193217

194-
protected final ObjectMapper MAPPER = new ObjectMapper();
218+
protected final ObjectMapper MAPPER = newJsonMapper();
195219

196220
public void testCreatorEnums() throws Exception {
197221
EnumWithCreator value = MAPPER.readValue("\"enumA\"", EnumWithCreator.class);
@@ -307,4 +331,15 @@ public void testMultiArgEnumInCollections() throws Exception
307331
assertEquals(Enum929.B, valueList.get(2));
308332
}
309333

334+
// for [databind#3280]
335+
public void testPropertyCreatorEnum3280() throws Exception
336+
{
337+
final ObjectReader r = MAPPER.readerFor(Enum3280.class);
338+
assertEquals(Enum3280.x, r.readValue("{\"b\":\"x\"}"));
339+
assertEquals(Enum3280.x, r.readValue("{\"a\":\"1\", \"b\":\"x\"}"));
340+
assertEquals(Enum3280.y, r.readValue("{\"b\":\"y\", \"a\":{}}"));
341+
assertEquals(Enum3280.y, r.readValue("{\"b\":\"y\", \"a\":{}}"));
342+
assertEquals(Enum3280.x, r.readValue("{\"a\":[], \"b\":\"x\"}"));
343+
assertEquals(Enum3280.x, r.readValue("{\"a\":{}, \"b\":\"x\"}"));
344+
}
310345
}

0 commit comments

Comments
 (0)