Skip to content

Improve handling of @JsonTypeInfo, defaultImpl to anchor definition #1358

Open
@cowtowncoder

Description

@cowtowncoder

So: currently it is possible to declare a valid @JsonTypeInfo in basetype, where defaultImpl specifies a subtype that is valid within context of declaration; but use it in a way that triggers an exception for potential conflict.

This happens when user requests deserialization into a subtype of original type, but one that is not castable to the type of defaultImpl. For example, something like:

@JsonTypeInfo(.... defaultImpl=DefaultImpl.class)
public class Base { }

public class Concrete extends Base { }

public class DefaultImpl extends Base { }

where deserialization using:

Base result = mapper.readValue("{ }", Base.class);

succeeds, but attempts to do:

Concrete result = mapper.readValue("{ }", Concrete.class);

will fail, because DefaultImpl is not assignable from Concrete.

While code is not exactly wrong in that there is potential problem -- if default implementation ends up required to be used, there would be a cast exception -- it is not necessarily something that will happen, and in fact user may be able to either guarantee it will not, or, even if not it may be better to indicate problem only when it does occur as opposed to merely possibly happening.

The technical problem is most likely due to inheritance of @JsonTypeInfo annotation, so that code has no way of knowing where exactly declaration is bound. If binding was known, base type comparison could use Base regardless of expected type, and we could indicate failure only when it actually happens.

It is not 100% certain that this problem can be resolved, but it would be good to try to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssues to be only tackled for Jackson 3.x, not 2.x

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions