Skip to content

Commit

Permalink
Backport #511 fix for 2.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 23, 2014
1 parent 0e03ef1 commit b126e8e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Version: 2.4.3 (xx-xxx-2014)

#496: Wrong result with `new TextNode("false").asBoolean(true)`
(reported by Ivar R, ivarru@github)
#511: DeserializationFeature.FAIL_ON_INVALID_SUBTYPE does not work
(reported by sbelikov@github)
#523: MapDeserializer and friends do not report the field/key name for mapping exceptions
(reported by Ian B, tea-dragon@github)
#524: @JsonIdentityReference(alwaysAsId = true) Custom resolver is reset to SimpleObjectIdResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class TypeDeserializerBase
extends TypeDeserializer
implements java.io.Serializable
{
private static final long serialVersionUID = 278445030337366675L;
private static final long serialVersionUID = 1;

protected final TypeIdResolver _idResolver;

Expand Down Expand Up @@ -149,7 +149,7 @@ protected final JsonDeserializer<Object> _findDeserializer(DeserializationContex
synchronized (_deserializers) {
deser = _deserializers.get(typeId);
if (deser == null) {
/* As per [Issue#305], need to provide contextual info. But for
/* As per [Databind#305], need to provide contextual info. But for
* backwards compatibility, let's start by only supporting this
* for base class, not via interface. Later on we can add this
* to the interface, assuming deprecation at base class helps.
Expand All @@ -162,10 +162,10 @@ protected final JsonDeserializer<Object> _findDeserializer(DeserializationContex
}
if (type == null) {
// As per [JACKSON-614], use the default impl if no type id available:
if (_defaultImpl == null) {
deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
throw ctxt.unknownTypeException(_baseType, typeId);
}
deser = _findDefaultImplDeserializer(ctxt);
} else {
/* 16-Dec-2010, tatu: Since nominal type we get here has no (generic) type parameters,
* we actually now need to explicitly narrow from base type (which may have parameterization)
Expand Down Expand Up @@ -238,10 +238,10 @@ protected Object _deserializeWithNativeTypeId(JsonParser jp, DeserializationCont
/* 04-May-2014, tatu: Should error be obligatory, or should there be another method
* for "try to deserialize with native tpye id"?
*/
if (_defaultImpl == null) {
deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
throw ctxt.mappingException("No (native) type id found when one was expected for polymorphic type handling");
}
deser = _findDefaultImplDeserializer(ctxt);
} else {
String typeIdStr = (typeId instanceof String) ? (String) typeId : String.valueOf(typeId);
deser = _findDeserializer(ctxt, typeIdStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Unit tests related to [JACKSON-712]; specialized handling of
* Unit tests related to specialized handling of
* otherwise invalid type id embedding cases.
*/
public class TestTypedDeserializationWithDefault extends BaseMapTest
Expand Down Expand Up @@ -48,7 +48,39 @@ public static class DefaultWithNoClass { }
// and then one with no defaultImpl nor listed subtypes
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
abstract static class MysteryPolymorphic { }


// [Databind#511] types

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes(@JsonSubTypes.Type(name="sub1", value = BadSub1.class))
public static class BadItem {}

public static class BadSub1 extends BadItem {
public String a ;
}

public static class Good {
public List<GoodItem> many;
}

public static class Bad {
public List<BadItem> many;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({@JsonSubTypes.Type(name="sub1", value = GoodSub1.class),
@JsonSubTypes.Type(name="sub2", value = GoodSub2.class) })
public static class GoodItem {}

public static class GoodSub1 extends GoodItem {
public String a ;
}
public static class GoodSub2 extends GoodItem {
public String b ;

}
/*
/**********************************************************
/* Unit tests, deserialization
Expand Down Expand Up @@ -86,7 +118,7 @@ public void testDeserializationWithArrayOfSize2() throws Exception
assertEquals(Arrays.asList("a", "b"), ((MyInter) inter).blah);
}

// [Issue#148]
// [Databind#148]
public void testDefaultAsNoClass() throws Exception
{
Object ob = MAPPER.reader(DefaultWithNoClass.class).readValue("{ }");
Expand All @@ -95,7 +127,7 @@ public void testDefaultAsNoClass() throws Exception
assertNull(ob);
}

// [Issue#148]
// [Databind#148]
public void testBadTypeAsNull() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -105,4 +137,18 @@ public void testBadTypeAsNull() throws Exception
ob = mapper.readValue("{ \"whatever\":13}", MysteryPolymorphic.class);
assertNull(ob);
}

// [Databind#511]
public void testInvalidTypeId511() throws Exception {
ObjectMapper mapper = new ObjectMapper().disable(
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE,
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES
);
String json = "{\"many\":[{\"sub1\":{\"a\":\"foo\"}},{\"sub2\":{\"b\":\"bar\"}}]}" ;
Good goodResult = mapper.readValue(json, Good.class) ;
assertNotNull(goodResult) ;
Bad badResult = mapper.readValue(json, Bad.class);
assertNotNull(badResult);
}
}

0 comments on commit b126e8e

Please sign in to comment.