Skip to content

Commit

Permalink
Fix #2775
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 14, 2020
1 parent 62329c3 commit 78f609e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project: jackson-databind

2.12.0-rc2 (not yet released)

#2775: Disabling `FAIL_ON_INVALID_SUBTYPE` breaks polymorphic deserialization of Enums
(reported by holgerknoche@github)
#2878: Revert change initially made to fix #2805: change in signature
of `ObjectMapper.treeToValue()` regarding exceptions
#2880: Revert removal of 2.7-deprecated `PropertyNamingStrategy` constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public boolean hasTypePropertyName(String n) {
}

public boolean hasDefaultType() {
return _typeDeserializer.getDefaultImpl() != null;
return _typeDeserializer.hasDefaultImpl();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public abstract class TypeDeserializer
*/
public abstract Class<?> getDefaultImpl();

/**
* @since 2.12
*/
public boolean hasDefaultImpl() { return getDefaultImpl() != null; }

/*
/**********************************************************
/* Type deserialization methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ct
return _deserializeWithNativeTypeId(p, ctxt, typeId);
}
}

// but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
JsonToken t = p.currentToken();
if (t == JsonToken.START_OBJECT) {
Expand Down Expand Up @@ -115,7 +115,7 @@ public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ct

@SuppressWarnings("resource")
protected Object _deserializeTypedForId(JsonParser p, DeserializationContext ctxt,
TokenBuffer tb, String typeId) throws IOException {
TokenBuffer tb, String typeId) throws IOException {
JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
if (_typeIdVisible) { // need to merge id back in JSON input?
if (tb == null) {
Expand All @@ -142,8 +142,9 @@ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p,
DeserializationContext ctxt, TokenBuffer tb) throws IOException
{
// May have default implementation to use
JsonDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
// 13-Oct-2020, tatu: As per [databind#2775], need to be careful to
// avoid ending up using "nullifying" deserializer
if (!hasDefaultImpl()) {
// or, perhaps we just bumped into a "natural" value (boolean/int/double/String)?
Object result = TypeDeserializer.deserializeIfNatural(p, ctxt, _baseType);
if (result != null) {
Expand All @@ -161,6 +162,11 @@ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p,
}
}
}
}
// ... and here we will check for default implementation handling (either
// genuine, or faked for "dont fail on bad type id")
JsonDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
String msg = String.format("missing type id property '%s'",
_typePropertyName);
// even better, may know POJO property polymorphic value would be assigned to
Expand Down Expand Up @@ -190,9 +196,8 @@ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p,
*/
@Override
public Object deserializeTypedFromAny(JsonParser p, DeserializationContext ctxt) throws IOException {
/* Sometimes, however, we get an array wrapper; specifically
* when an array or list has been serialized with type information.
*/
// Sometimes, however, we get an array wrapper; specifically
// when an array or list has been serialized with type information.
if (p.hasToken(JsonToken.START_ARRAY)) {
return super.deserializeTypedFromArray(p, ctxt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ protected TypeDeserializerBase(TypeDeserializerBase src, BeanProperty property)
@Override
public TypeIdResolver getTypeIdResolver() { return _idResolver; }

@Override
@Override
public Class<?> getDefaultImpl() {
return ClassUtil.rawClass(_defaultImpl);
}

@Override
public boolean hasDefaultImpl() {
return (_defaultImpl != null);
}

/**
* @since 2.9
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

Expand Down Expand Up @@ -89,10 +90,14 @@ public String findImplicitPropertyName(final AnnotatedMember member) {
}

@Override
public boolean hasCreatorAnnotation(Annotated a) {
final AnnotatedConstructor ctor = (AnnotatedConstructor) a;
return (ctor.getParameterCount() > 0)
&& (ctor.getParameter(0).getAnnotation(Field1756.class) != null);
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated ann) {
final AnnotatedConstructor ctor = (AnnotatedConstructor) ann;
if (ctor.getParameterCount() > 0) {
if (ctor.getParameter(0).getAnnotation(Field1756.class) != null) {
return JsonCreator.Mode.PROPERTIES;
}
}
return null;
}
}

Expand Down

0 comments on commit 78f609e

Please sign in to comment.