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.

Deduction deserialization - add support for pojo-hierarchies such that the absence of child fields infers a supertype

See original GitHub issue

Currently, deduction deserialization requires all values to have unique field. Because of that, there is no way to deserialize values to superclass and subclasses, when defaultImpl is different than superclass.

Example:

For json:

 "name": "Italy",
 "places": {
   "mountains": {
     "name": "The Alps"
   },
   "cites": [
     {
       "name": "Rome",
       "places": {
         "colosseum": {
           "name": "The Colosseum"
         },
         "romanForum": {
           "name": "The Roman Forum"
         }
       }
     },
     {
       "name": "Venice",
       "places": {
         "bridges": []
       }
     }
   ]
 }
}

and pojo-hierarchy:

public static class Place implements WorthSeeing {  
  public String name;  
}  
  
public static class CompositePlace extends Place implements WorthSeeing {  
  
  public Map<String, WorthSeeing> places;  
}  
  
static class ListOfPlaces extends ArrayList<WorthSeeing> implements WorthSeeing {  
}

and deduction deserialization with defaultImpl differ then supertype:

@JsonTypeInfo(use = DEDUCTION, defaultImpl = ListOfPlaces.class)  
@JsonSubTypes( {@Type(ListOfPlaces.class), @Type(CompositePlace.class), @Type(Place.class)})  
interface WorthSeeing {}

the MismatchedInputException is thrown, because the values with only name fields are not classified to Place .

Desired behaviour:

  • value contains only fields from superclass -> it’s mapped to an object of superclass
  • value contains fields from superclass and a subclass -> it’s mapped to an object of the supclass
  • value contains fields from superclass and fields that don’t exist in any of subclasses -> the UnrecognizedPropertyException is thrown

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
drekbourcommented, Jan 11, 2022

Thankyou for thinking through the scenario and code. Honouring required=true is a good addition to the deduction process and goes onto my list.

0reactions
TCke83commented, Jan 28, 2022

+1 would love to see this in the next version, can’t use the deduction now because the supertypes don’t get deducted

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing polymorphic types with Jackson based on the ...
Such an approach is presented in this article. Update. Jackson 2.12.0 gets Polymorphic subtype deduction from available fields which adds @JsonTypeInfo(use = ...
Read more >
Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >
Jackson-js: Powerful JavaScript decorators to serialize ...
it supports more advanced Object concepts such as polymorphism and Object identity;; it supports cyclic object serialization/deserialization;; it supports ...
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson to figure out the right type during deserialization. Let's ......
Read more >
Security update for jackson-databind, jackson-dataformats ...
bindItem()' for same mapping + Add 'namespace' property for ... + Jackson does not support deserializing new Java 9 unmodifiable collections ...
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