Skip to content

Commit

Permalink
Fix #3280
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 28, 2021
1 parent 216de27 commit 70ba54f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Project: jackson-databind
=== Releases ===
------------------------------------------------------------------------

(not yet released)

#3280: Can not deserialize json to enum value with Object-/Array-valued input,
`@JsonCreator`
(reported by peteryuanpan@github)

2.12.5 (27-Aug-2021)

#3220: (regression) Factory method generic type resolution does not use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ protected Object deserializeEnumUsingPropertyBased(final JsonParser p, final Des
continue;
}
// 26-Nov-2020, tatu: ... what should we do here tho?
p.skipChildren();
}
return creator.build(ctxt, buffer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.fasterxml.jackson.databind.deser.creators;

import java.math.BigDecimal;
import java.util.*;
import java.util.Collection;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down Expand Up @@ -185,13 +189,33 @@ public String toString() {
}
}

// [databind#3280]
static enum Enum3280 {
x("x"),
y("y"),
z("z");
private final String value;
Enum3280(String value) {
this.value = value;
}
@JsonCreator
public static Enum3280 getByValue(@JsonProperty("b") String value) {
for (Enum3280 e : Enum3280.values()) {
if (e.value.equals(value)) {
return e;
}
}
return null;
}
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

protected final ObjectMapper MAPPER = new ObjectMapper();
protected final ObjectMapper MAPPER = newJsonMapper();

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

// for [databind#3280]
public void testPropertyCreatorEnum3280() throws Exception
{
final ObjectReader r = MAPPER.readerFor(Enum3280.class);
assertEquals(Enum3280.x, r.readValue("{\"b\":\"x\"}"));
assertEquals(Enum3280.x, r.readValue("{\"a\":\"1\", \"b\":\"x\"}"));
assertEquals(Enum3280.y, r.readValue("{\"b\":\"y\", \"a\":{}}"));
assertEquals(Enum3280.y, r.readValue("{\"b\":\"y\", \"a\":{}}"));
assertEquals(Enum3280.x, r.readValue("{\"a\":[], \"b\":\"x\"}"));
assertEquals(Enum3280.x, r.readValue("{\"a\":{}, \"b\":\"x\"}"));
}
}

0 comments on commit 70ba54f

Please sign in to comment.