question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve handling of `@JsonTypeInfo`, `defaultImpl` to anchor definition

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Apr 3, 2018

@michalmela As general practice, I add notes on issues if I work on them. So no news unfortunately.

0reactions
gm2211commented, May 29, 2018

Had deleted my comment while trying to verify that.

The test for #1565 is actually a little different: you have A(defaultImpl = B.class), B extends A, C extends B and testing that you can deserialize to C.

However, that same PR introduces a test for #1861 -> https://github.com/FasterXML/jackson-databind/commit/09e5ba14fbd519116cbcd23a9ee273bf5d98c7f3#diff-7d961794a75a46f308447cd997960153R54 which is equivalent to the example outlined in this issue.

Not sure about semantic differences either, but this fixes the practical issues we found while upgrading from 2.6.5 to 2.9.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonTypeInfo “defaultImpl” for collections - Google Groups
There is one set of definition as follows: @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", ...
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
When used on properties (fields, methods), this annotation applies to values. That means when used on collection types (Collection, Map, arrays) ...
Read more >
Index (jackson-databind 2.13.3 API) - javadoc.io
Method to use for defining mix-in annotations to use for augmenting ... costly to resolve) and mostly useless (no special handling), better handle...
Read more >
Anchoring and Adjustment Definition in Business & Finance
The anchoring and adjustment heuristic describes cases in which a person uses a specific target number or value as a starting point, known...
Read more >
java - How can I retain the type property when deserializing ...
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "dishName", defaultImpl = Food.class, visible = true).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found