Skip to content

Commit a5b0af8

Browse files
committed
Merge branch '2.14' into 2.15
2 parents 4093e3f + 3b6034d commit a5b0af8

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project: jackson-databind
2121
(reported by @marvin-we)
2222
#3699: Allow custom `JsonNode` implementations
2323
(contributed by Philippe M)
24+
#3711: Enum polymorphism not working correctly with DEDUCTION
25+
(reported by @smilep)
2426

2527
2.14.1 (21-Nov-2022)
2628

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.databind.jsontype.impl;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
6+
import com.fasterxml.jackson.core.JsonGenerator;
7+
import com.fasterxml.jackson.core.type.WritableTypeId;
8+
import com.fasterxml.jackson.databind.BeanProperty;
9+
10+
/**
11+
* @since 2.14.2
12+
*/
13+
public class AsDeductionTypeSerializer extends TypeSerializerBase
14+
{
15+
private final static AsDeductionTypeSerializer INSTANCE = new AsDeductionTypeSerializer();
16+
17+
protected AsDeductionTypeSerializer() {
18+
super(null, null);
19+
}
20+
21+
public static AsDeductionTypeSerializer instance() {
22+
return INSTANCE;
23+
}
24+
25+
@Override
26+
public AsDeductionTypeSerializer forProperty(BeanProperty prop) {
27+
return this;
28+
}
29+
30+
// This isn't really right but there's no "none" option
31+
@Override
32+
public As getTypeInclusion() { return As.EXISTING_PROPERTY; }
33+
34+
@Override
35+
public WritableTypeId writeTypePrefix(JsonGenerator g,
36+
WritableTypeId idMetadata) throws IOException
37+
{
38+
// NOTE: We can NOT simply skip writing since we may have to
39+
// write surrounding Object or Array start/end markers. But
40+
// we are not to generate type id to write (compared to base class)
41+
42+
if (idMetadata.valueShape.isStructStart()
43+
// also: do not try to write native type id
44+
&& !g.canWriteTypeId()) {
45+
return g.writeTypePrefix(idMetadata);
46+
}
47+
return null;
48+
}
49+
50+
@Override
51+
public WritableTypeId writeTypeSuffix(JsonGenerator g,
52+
WritableTypeId idMetadata) throws IOException
53+
{
54+
return (idMetadata == null) ? null
55+
: g.writeTypeSuffix(idMetadata);
56+
}
57+
}

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,14 @@ public TypeSerializer buildTypeSerializer(SerializationConfig config,
108108
return null;
109109
}
110110
}
111-
112-
TypeIdResolver idRes = idResolver(config, baseType, subTypeValidator(config),
113-
subtypes, true, false);
114-
115111
if(_idType == JsonTypeInfo.Id.DEDUCTION) {
116112
// Deduction doesn't require a type property. We use EXISTING_PROPERTY with a name of <null> to drive this.
117-
return new AsExistingPropertyTypeSerializer(idRes, null, _typeProperty);
113+
// 04-Jan-2023, tatu: Actually as per [databind#3711] that won't quite work so:
114+
return AsDeductionTypeSerializer.instance();
118115
}
119116

117+
TypeIdResolver idRes = idResolver(config, baseType, subTypeValidator(config),
118+
subtypes, true, false);
120119
switch (_includeAs) {
121120
case WRAPPER_ARRAY:
122121
return new AsArrayTypeSerializer(idRes, null);

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import com.fasterxml.jackson.annotation.JsonSubTypes;
77
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8-
8+
import com.fasterxml.jackson.annotation.JsonValue;
99
import com.fasterxml.jackson.databind.BaseMapTest;
1010
import com.fasterxml.jackson.databind.DeserializationFeature;
1111
import com.fasterxml.jackson.databind.JavaType;
@@ -54,6 +54,15 @@ static class Box {
5454
public Feline feline;
5555
}
5656

57+
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
58+
static class Bean3711 {
59+
@JsonValue
60+
public String ser = "value";
61+
}
62+
63+
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
64+
static enum Enum3711 { A, B }
65+
5766
/*
5867
/**********************************************************
5968
/* Mock data
@@ -269,4 +278,16 @@ public void testListSerialization() throws Exception {
269278
// Then:
270279
assertEquals(arrayOfCatsJson, json);
271280
}
281+
282+
// [databind#3711]
283+
public void testWithPojoAsJsonValue() throws Exception
284+
{
285+
assertEquals(q("value"), MAPPER.writeValueAsString(new Bean3711()));
286+
}
287+
288+
// [databind#3711]
289+
public void testWithEnum() throws Exception
290+
{
291+
assertEquals(q("B"), MAPPER.writeValueAsString(Enum3711.B));
292+
}
272293
}

0 commit comments

Comments
 (0)