Skip to content

Commit

Permalink
Fix #3824 (same as #2968 but for 3.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 13, 2023
1 parent 3ecdd8e commit a5b4708
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AsDeductionTypeDeserializer(DeserializationContext ctxt,
JavaType bt, TypeIdResolver idRes, JavaType defaultImpl,
Collection<NamedType> subtypes)
{
super(bt, idRes, null, false, defaultImpl, null);
super(bt, idRes, null, false, defaultImpl, null, true);
propertyBitIndex = new HashMap<>();
subtypeFingerprints = buildFingerprints(ctxt, subtypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class AsPropertyTypeDeserializer extends AsArrayTypeDeserializer
{
protected final As _inclusion;

/**
* Indicates if the current class has a TypeResolver attached or not.
*/
protected final boolean _hasTypeResolver;

protected final String _msgForMissingId = (_property == null)
? String.format("missing type id property '%s'", _typePropertyName)
: String.format("missing type id property '%s' (for POJO property '%s')", _typePropertyName, _property.getName());
Expand All @@ -37,14 +42,24 @@ public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl,
As inclusion)
{
this(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, inclusion, true);
}

public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl,
As inclusion,
boolean hasTypeResolver)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
_inclusion = inclusion;
_hasTypeResolver = hasTypeResolver;
}

public AsPropertyTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) {
super(src, property);
_inclusion = src._inclusion;
_hasTypeResolver = src._hasTypeResolver;
}

@Override
Expand Down Expand Up @@ -166,7 +181,8 @@ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p,
// genuine, or faked for "dont fail on bad type id")
ValueDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
JavaType t = _handleMissingTypeId(ctxt, priorFailureMsg);
JavaType t = _hasTypeResolver
? _handleMissingTypeId(ctxt, priorFailureMsg) : _baseType;
if (t == null) {
// 09-Mar-2017, tatu: Is this the right thing to do?
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import tools.jackson.databind.*;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.introspect.AnnotatedClass;
import tools.jackson.databind.introspect.AnnotatedClassResolver;
import tools.jackson.databind.jsontype.*;
import tools.jackson.databind.util.ClassUtil;

Expand Down Expand Up @@ -192,7 +194,8 @@ public TypeDeserializer buildTypeDeserializer(DeserializationContext ctxt,
case PROPERTY:
case EXISTING_PROPERTY: // as per [#528] same class as PROPERTY
return new AsPropertyTypeDeserializer(baseType, idRes,
_typeProperty, _typeIdVisible, defaultImpl, _includeAs);
_typeProperty, _typeIdVisible, defaultImpl, _includeAs,
_hasTypeResolver(ctxt, baseType));
case WRAPPER_OBJECT:
return new AsWrapperTypeDeserializer(baseType, idRes,
_typeProperty, _typeIdVisible, defaultImpl);
Expand Down Expand Up @@ -372,4 +375,23 @@ protected boolean allowPrimitiveTypes(DatabindContext ctxt,
JavaType baseType) {
return false;
}


/**
* Checks whether the given class has annotations indicating some type resolver
* is applied, for example {@link com.fasterxml.jackson.annotation.JsonSubTypes}.
* Only initializes {@link #_hasTypeResolver} once if its value is null.
*
* @param ctxt Currently active context
* @param baseType the base type to check for type resolver annotations
*
* @return true if the class has type resolver annotations, false otherwise
*/
protected boolean _hasTypeResolver(DatabindContext ctxt, JavaType baseType) {
AnnotatedClass ac =
AnnotatedClassResolver.resolveWithoutSuperTypes(ctxt.getConfig(),
baseType.getRawClass());
AnnotationIntrospector ai = ctxt.getAnnotationIntrospector();
return ai.findPolymorphicTypeInfo(ctxt.getConfig(), ac) != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tools.jackson.failing;
package tools.jackson.databind.jsontype;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand Down Expand Up @@ -98,7 +98,7 @@ public void testDeserializationWrapperWithDefaultTyping() throws Exception {

final String defaultTypedJson = a2q(
"{'value':" +
"['com.fasterxml.jackson.databind.jsontype.JsonTypeInfoIgnored2968Test$BasketBall'," +
"['"+getClass().getName()+"$BasketBall'," +
"{'size':42}]}");

BallValueWrapper wrapper = mapper.readValue(defaultTypedJson, BallValueWrapper.class);
Expand All @@ -118,8 +118,7 @@ public void testDeserializationBaseClassWithDefaultTyping() throws Exception {
try {
mapper.readValue(concreteTypeJson, SimpleBall.class);
} catch (MismatchedInputException | InvalidDefinitionException e) {
verifyException(e, "Unexpected token (START_OBJECT), expected START_ARRAY: need Array value " +
"to contain `As.WRAPPER_ARRAY` type information for class");
verifyException(e, "Unexpected token", "START_OBJECT", "expected", "START_ARRAY");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tools.jackson.failing;
package tools.jackson.databind.jsontype.deftyping;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

Expand All @@ -23,6 +23,7 @@ public BasketBall(int size) {
}
}

// [databind#2968] / [databind#3824]
public void testDeserializationConcreteClassWithDefaultTyping() throws Exception {
final PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(SimpleBall.class)
Expand Down

0 comments on commit a5b4708

Please sign in to comment.