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.

Polymorphic subtype deduction based on 'presence' of fields does not appear to take 'absence' of fields into account

See original GitHub issue

Describe the bug Our use case involves the following: We are calling a web service that returns a JSON response that is a composite object. The object has a member field which is itself an object whos field members change depending on the service called, therefore we implemented this object as an interface with a number of different implementations corresponding to each different service, so it is a “polymorphic” member of the response object. Therefore, we placed the @JsonTypeInfo(use = DEDUCTION) annotation a top this interface definition.

In one particular service call, this response object returns with an empty object for this polymorphic member. When this case occurs, an error similar to the following is encountered(say we have only two implementations defined in the @JsonSubTypes annotation) :

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Cannot deduce unique subtype of `com.engageft.jackson.deserialization.issue.objects.AnInterface` (2 candidates match)
 at [Source: (String)"{
   "compositeClassIntMember":11235,
   "compositeClassStringMember":"compositeClassString",
   "polyMorphicMember":{
      
   }
}"; line: 6, column: 4] (through reference chain: com.engageft.jackson.deserialization.issue.objects.CompositeClass["polyMorphicMember"])
	at com.fasterxml.jackson.databind.jsontype.impl.AsDeductionTypeDeserializer.deserializeTypedFromObject(AsDeductionTypeDeserializer.java:131)

We then thought we’d try creating an implementation of this polymorphic member without any fields specifically for this case of an empty response object in the hopes that this mechanism would find this “empty implementation” and use it, but this did not work.

After examining the code found here, it appears that the case, or situation of if/when a particular implementation contains no data members is not being accounted for.

Other than this, we find this functionality absolutely fantastic and immensely helpful! Thank you for adding it! We hope that you might find a way to account for and solve this issue. I realize this is probably an outlier of a use case, and so is probably one that is very easily overlooked.

If you think you may benefit from any further assistance from me on this matter please do not hesitate to let me know 😃 .

Version information Which Jackson version(s) was this for? jackson-databind-2.12.0

To Reproduce I’ve created a small project which demonstrates this issue and it’s located here.

Expected behavior The tests in the test project should outline the whole thing, but if they don’t please let me know.

Additional context Can’t think of any at the moment, but if I do, I’ll amend this.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
lderienzocommented, Dec 10, 2020

I added @JsonTypeInfo(... , defaultImpl = DefaultType.class) to the sample project but couldn’t get it to work as you suggested trying. I’ve pushed those changes to my sample project repo for you to look at.

I’m now actually wondering if there is a problem with defaultImpl when used with DEDUCTION. I found what might be a similar issue here.

1reaction
cowtowncodercommented, Dec 10, 2020

True, implementation only considers properties found. It might be possible to figure out a fallback case, if (but only if) such could be uniquely determined. What could help here would be a set of example definitions along with JSON to outline expected case.

Another possibility is that existing @JsonTypeInfo(... , defaultImpl = DefaultType.class) could maybe be used (I would think that’d be fallback anyway).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can Jackson polymorphic deserialization be used to serialize ...
The deduction-based polymorphism feature deduces subtypes based on the presence of properties distinct to a particular subtype.
Read more >
Deduction-Based Polymorphism in Jackson 2.12 - Baeldung
In this tutorial, we'll explore how we can use the deduction-based polymorphism feature from the Jackson library.
Read more >
A brand new website interface for an even better experience!
Polymorphic subtype deduction based on 'presence' of fields does not appear to take 'absence' of fields into account.
Read more >
Operations on Records | Request PDF - ResearchGate
In the former concatenation has no principle type while in the latter ... presence or absence of fields has been already considered, in...
Read more >
Operations on Records - Stanford CS Theory
In our system, the extension operation is not required to check whether a new field is already present in a record: its absence...
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