Skip to content

Deserialization failure with Polymorphism using JsonTypeInfo defaultImpl, subtype as target #1565

@j1234d2003

Description

@j1234d2003

Issue verified on version jackson-databind 2.8.7 on Ubuntu 14.04, Java 1.8.0_25. A sample code
CTestTypeId.java.txt
demonstrates this bug.

A class hierarchy that implements an interface annotated with a @JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="typeInfo", defaultImpl=CBaseClass.class) and a @JsonTypeIdResolve fails to deserialize a derived class that extends the defaultImpl CBaseClass with exception IllegalArgumentException stating that the defaultImpl class is not a subtype of the extending derived class thrown at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:359)

Consider interface CTestInterface annotated with
@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="typeInfo", defaultImpl=CBaseClass.class) and
@JsonTypeIdResolver(CTypeMapper.class)

Consider two classes:

  1. CBaseClass that implements CTestInterface and
  2. CDerivedClass extends CBaseClass.
    On attempt to deserialize data for a CDerivedClass using an ObjectMapper, the type-deserializer failed to be built as demonstrated by the following stack trace. Note that an instance of CBaseClass gets deserialized correctly. For my target usecase, data does not always include the "typeInfo" property and hence defaultImpl use becomes necessary.

Exception in thread "main" java.lang.IllegalArgumentException: Class com.example.CTestTypeId$CBaseClass not subtype of [simple type, class com.example.CTestTypeId$CDerivedClass]
at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:359)
at com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder.buildTypeDeserializer(StdTypeResolverBuilder.java:128)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findTypeDeserializer(BasicDeserializerFactory.java:1373)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:482)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3899)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3794)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
at com.example.CTestTypeId.main(CTestTypeId.java:177)

I am trying to migrate from jackson-databind 2.1.2 to a more recent production version. Is it possible to know a version that does not suffer from this bug as I need to migrate to a stable version very quickly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions