Skip to content

Commit 5845399

Browse files
committed
Bigger PR rewrite
1 parent 57d8d92 commit 5845399

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/main/java/tools/jackson/databind/jsontype/TypeResolverProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public TypeDeserializer findPropertyContentTypeDeserializer(DeserializationConte
197197
}
198198
// [databind#1654]: Explicit `@JsonTypeInfo(Id.NONE)` should block class-level type info
199199
if (b == NO_RESOLVER) {
200-
return new NoOpTypeDeserializer(contentType, null);
200+
return NoOpTypeDeserializer.forBaseType(ctxt, contentType);
201201
}
202202
Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByTypeId(config,
203203
accessor, contentType);

src/main/java/tools/jackson/databind/jsontype/impl/NoOpTypeDeserializer.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import tools.jackson.databind.*;
77
import tools.jackson.databind.jsontype.TypeDeserializer;
88
import tools.jackson.databind.jsontype.TypeIdResolver;
9+
import tools.jackson.databind.util.ClassUtil;
910

1011
/**
1112
* Special {@link TypeDeserializer} implementation used to explicitly
@@ -22,23 +23,27 @@
2223
public class NoOpTypeDeserializer extends TypeDeserializer
2324
{
2425
private final JavaType _baseType;
25-
private final ValueDeserializer<Object> _deserializer;
26+
private final BeanProperty _property;
2627

27-
public NoOpTypeDeserializer(JavaType baseType, ValueDeserializer<Object> deser) {
28+
// Dynamically constructed deserializer
29+
private volatile ValueDeserializer<Object> _deserializer;
30+
31+
private NoOpTypeDeserializer(JavaType baseType, BeanProperty prop) {
2832
_baseType = baseType;
29-
_deserializer = deser;
33+
_property = prop;
3034
}
3135

32-
public NoOpTypeDeserializer withDeserializer(ValueDeserializer<Object> deser) {
33-
if (_deserializer == deser) {
34-
return this;
35-
}
36-
return new NoOpTypeDeserializer(_baseType, deser);
36+
public static NoOpTypeDeserializer forBaseType(DeserializationContext ctxt,
37+
JavaType baseType) {
38+
return new NoOpTypeDeserializer(baseType, null);
3739
}
3840

3941
@Override
4042
public TypeDeserializer forProperty(BeanProperty prop) {
41-
return this;
43+
if (_property == prop) {
44+
return this;
45+
}
46+
return new NoOpTypeDeserializer(_baseType, prop);
4247
}
4348

4449
@Override
@@ -63,34 +68,30 @@ public Class<?> getDefaultImpl() {
6368
}
6469

6570
@Override
66-
public Object deserializeTypedFromObject(JsonParser p,
67-
DeserializationContext ctxt) throws JacksonException
71+
public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt)
72+
throws JacksonException
6873
{
69-
// Just deserialize without type info
7074
return _deserialize(p, ctxt);
7175
}
7276

7377
@Override
74-
public Object deserializeTypedFromArray(JsonParser p,
75-
DeserializationContext ctxt) throws JacksonException
78+
public Object deserializeTypedFromArray(JsonParser p, DeserializationContext ctxt)
79+
throws JacksonException
7680
{
77-
// Just deserialize without type info
7881
return _deserialize(p, ctxt);
7982
}
8083

8184
@Override
82-
public Object deserializeTypedFromScalar(JsonParser p,
83-
DeserializationContext ctxt) throws JacksonException
85+
public Object deserializeTypedFromScalar(JsonParser p, DeserializationContext ctxt)
86+
throws JacksonException
8487
{
85-
// Just deserialize without type info
8688
return _deserialize(p, ctxt);
8789
}
8890

8991
@Override
90-
public Object deserializeTypedFromAny(JsonParser p,
91-
DeserializationContext ctxt) throws JacksonException
92+
public Object deserializeTypedFromAny(JsonParser p, DeserializationContext ctxt)
93+
throws JacksonException
9294
{
93-
// Just deserialize without type info
9495
return _deserialize(p, ctxt);
9596
}
9697

@@ -99,14 +100,16 @@ protected Object _deserialize(JsonParser p, DeserializationContext ctxt)
99100
{
100101
ValueDeserializer<Object> deser = _deserializer;
101102

102-
// Find deserializer for the base type (this will find custom deserializers
103-
// registered for this type, including those from @JsonDeserialize annotations)
103+
// Find deserializer for the base type, given property (if any).
104+
// This will find custom deserializers registered for this type,
105+
// including those from @JsonDeserialize annotations)
104106
if (deser == null) {
105-
deser = ctxt.findContextualValueDeserializer(_baseType, null);
107+
deser = ctxt.findContextualValueDeserializer(_baseType, _property);
106108
if (deser == null) {
107109
ctxt.reportBadDefinition(_baseType,
108-
"Cannot find deserializer for type " + _baseType);
110+
"Cannot find deserializer for type " +ClassUtil.getTypeDescription(_baseType));
109111
}
112+
_deserializer = deser;
110113
}
111114
return deser.deserialize(p, ctxt);
112115
}

src/test/java/tools/jackson/databind/jsontype/NoTypeInfo1654Test.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313

1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515

16-
class NoTypeInfo1654Test extends DatabindTestUtil {
17-
16+
class NoTypeInfo1654Test extends DatabindTestUtil
17+
{
1818
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
1919
static class Value1654 {
2020
public int x;
2121

22-
protected Value1654() {
23-
}
22+
protected Value1654() { }
2423

2524
public Value1654(int x) {
2625
this.x = x;
@@ -30,8 +29,7 @@ public Value1654(int x) {
3029
static class Value1654TypedContainer {
3130
public List<Value1654> values;
3231

33-
protected Value1654TypedContainer() {
34-
}
32+
protected Value1654TypedContainer() { }
3533

3634
public Value1654TypedContainer(Value1654... v) {
3735
values = Arrays.asList(v);

0 commit comments

Comments
 (0)