Skip to content

Commit 37e50bd

Browse files
committed
Merge branch '2.10' into 2.11
2 parents f87c8a7 + f4d2408 commit 37e50bd

File tree

5 files changed

+53
-61
lines changed

5 files changed

+53
-61
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Project: jackson-databind
5252
#2602: ByteBufferSerializer produces unexpected results with a duplicated ByteBuffer
5353
and a position > 0
5454
(reported by Eduard T)
55+
#2605: Failure to deserializer polymorphic subtypes of base type `Enum`
56+
(reported by uewle@github)
5557
#2610: `EXTERNAL_PROPERTY` doesn't work with `@JsonIgnoreProperties`
5658
(reported, fix suggested by Alexander S)
5759

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

+9
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,15 @@ public JsonDeserializer<?> createEnumDeserializer(DeserializationContext ctxt,
14601460
JsonDeserializer<?> deser = _findCustomEnumDeserializer(enumClass, config, beanDesc);
14611461

14621462
if (deser == null) {
1463+
// 12-Feb-2020, tatu: while we can't really create real deserializer for `Enum.class`,
1464+
// it is necessary to allow it in one specific case: see [databind#2605] for details
1465+
// but basically it can be used as polymorphic base.
1466+
// We could check `type.getTypeHandler()` to look for that case but seems like we
1467+
// may as well simply create placeholder (AbstractDeserializer) regardless
1468+
if (enumClass == Enum.class) {
1469+
return AbstractDeserializer.constructForNonPOJO(beanDesc);
1470+
}
1471+
14631472
ValueInstantiator valueInstantiator = _constructDefaultValueInstantiator(ctxt, beanDesc);
14641473
SettableBeanProperty[] creatorProps = (valueInstantiator == null) ? null
14651474
: valueInstantiator.getFromObjectArguments(ctxt.getConfig());

src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,11 @@ protected JsonDeserializer<?> _createDeserializer2(DeserializationContext ctxt,
363363
throws JsonMappingException
364364
{
365365
final DeserializationConfig config = ctxt.getConfig();
366-
// If not, let's see which factory method to use:
367-
if (type.isEnumType()) {
366+
// If not, let's see which factory method to use
367+
368+
// 12-Feb-20202, tatu: Need to ensure that not only all Enum implementations get
369+
// there, but also `Enum` -- latter wrt [databind#2605], polymorphic usage
370+
if (ClassUtil.isEnumType(type.getRawClass())) { // type.isEnumType()) {
368371
return factory.createEnumDeserializer(ctxt, type, beanDesc);
369372
}
370373
if (type.isContainerType()) {

src/test/java/com/fasterxml/jackson/databind/jsontype/TestEnumTyping.java

+37-14
Original file line numberDiff line numberDiff line change
@@ -46,59 +46,82 @@ public void setValue(Object o) {
4646
value = o;
4747
}
4848
}
49-
49+
50+
// for [databind#2605]
51+
static class EnumContaintingClass<ENUM_TYPE extends Enum<ENUM_TYPE>> {
52+
@JsonTypeInfo(
53+
use = JsonTypeInfo.Id.CLASS,
54+
include = JsonTypeInfo.As.PROPERTY,
55+
property = "@class"
56+
)
57+
public ENUM_TYPE selected;
58+
59+
protected EnumContaintingClass() { }
60+
61+
public EnumContaintingClass(ENUM_TYPE selected) {
62+
this.selected = selected;
63+
}
64+
}
65+
5066
/*
5167
/**********************************************************
5268
/* Unit tests
5369
/**********************************************************
5470
*/
5571

72+
private final ObjectMapper MAPPER = newJsonMapper();
73+
5674
public void testTagList() throws Exception
5775
{
58-
ObjectMapper m = new ObjectMapper();
5976
TagList list = new TagList();
6077
list.add(Tag.A);
6178
list.add(Tag.B);
62-
String json = m.writeValueAsString(list);
79+
String json = MAPPER.writeValueAsString(list);
6380

64-
TagList result = m.readValue(json, TagList.class);
81+
TagList result = MAPPER.readValue(json, TagList.class);
6582
assertEquals(2, result.size());
6683
assertSame(Tag.A, result.get(0));
6784
assertSame(Tag.B, result.get(1));
6885
}
6986

7087
public void testEnumInterface() throws Exception
7188
{
72-
ObjectMapper m = new ObjectMapper();
73-
String json = m.writeValueAsString(Tag.B);
74-
EnumInterface result = m.readValue(json, EnumInterface.class);
89+
String json = MAPPER.writeValueAsString(Tag.B);
90+
EnumInterface result = MAPPER.readValue(json, EnumInterface.class);
7591
assertSame(Tag.B, result);
7692
}
7793

7894
public void testEnumInterfaceList() throws Exception
7995
{
80-
ObjectMapper m = new ObjectMapper();
8196
EnumInterfaceList list = new EnumInterfaceList();
8297
list.add(Tag.A);
8398
list.add(Tag.B);
84-
String json = m.writeValueAsString(list);
99+
String json = MAPPER.writeValueAsString(list);
85100

86-
EnumInterfaceList result = m.readValue(json, EnumInterfaceList.class);
101+
EnumInterfaceList result = MAPPER.readValue(json, EnumInterfaceList.class);
87102
assertEquals(2, result.size());
88103
assertSame(Tag.A, result.get(0));
89104
assertSame(Tag.B, result.get(1));
90105
}
91106

92107
public void testUntypedEnum() throws Exception
93108
{
94-
ObjectMapper mapper = new ObjectMapper();
95-
String str = mapper.writeValueAsString(new UntypedEnumBean(TestEnum.B));
96-
UntypedEnumBean result = mapper.readValue(str, UntypedEnumBean.class);
109+
String str = MAPPER.writeValueAsString(new UntypedEnumBean(TestEnum.B));
110+
UntypedEnumBean result = MAPPER.readValue(str, UntypedEnumBean.class);
97111
assertNotNull(result);
98112
assertNotNull(result.value);
99113
Object ob = result.value;
100114
assertSame(TestEnum.class, ob.getClass());
101115
assertEquals(TestEnum.B, result.value);
102116
}
103-
117+
118+
// for [databind#2605]
119+
public void testRoundtrip() throws Exception
120+
{
121+
EnumContaintingClass<TestEnum> input = new EnumContaintingClass<TestEnum>(TestEnum.B);
122+
String json = MAPPER.writeValueAsString(input);
123+
// Object o = MAPPER.readerFor(EnumContaintingClass.class).readValue(json);
124+
Object o = MAPPER.readValue(json, EnumContaintingClass.class);
125+
assertNotNull(o);
126+
}
104127
}

src/test/java/com/fasterxml/jackson/failing/EnumPolymorphic2605Test.java

-45
This file was deleted.

0 commit comments

Comments
 (0)