Generic class and @JsonUnwrapped causes failure to deserialize JSON in some cases #22857
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: duplicate
A duplicate of another issue
I have a generic class which wraps another generic class which itself wraps a couple of other classes. I want the JSON from all these to be "flattened", so I use the
@JsonUnwrapped
annotation. I can then GET and POST the flattened JSON and it all works fine. But if I then introduce some abstract (generic) REST controller, so that the method which receives the POSTed data is in a generic class, Spring fails to deserialize the JSON correctly.For clarity, here's some code that explains the situation...
First, I have some DTOs representing information about a user:
I then have a generic class that I use to combine this "basic" and "extended" information:
I then have a generic class which wraps the generic class above and adds more information to it (e.g an ID):
In the end, what I work with is an object of this type:
SavedItem<Integer, ItemInfo<UserBasicInfo, UserExtendedInfo>>
If I then have a controller which receives an item of that type, as shown below, it all works OK:
However, if I declare that method instead in an abstract generic base class, as shown below, it doesn't quite work:
What happens is that the
basicInfo
andextendedInfo
items are left null as the JSON fails to be deserialized correctly.The issue does not occur if:
UserRestController
, explicitly implements the method(even if it just overrides it and then calls the super class's implementation),Or
ItemInfo<Basic, Extended>
instead ofSavedItem<Id, ItemInfo<Basic, Extended>>
Or
@JsonUnwrapped
(meaning the JSON has a couple of extra levels of nesting within it).The reason I think this might be an issue with Spring rather than Jackson is because if I put a breakpoint in
AbstractJackson2HttpMessageConverter.readJavaType()
(line 239) I can see that when the Jackson object mapper is called, thejavaType
argument which is passed to it doesn't have any of the generic information in theItemInfo
class. Whereas when that same line of code is hit when the REST controller method is implemented in the concrete class, then the full generic type information is correctly passed into Jackson. However that's just a guess, so if you think this is actually a Jackson issue let me know and I'll log an issue with them.A git repo where the issue can be replicated is available here. Details of how to replicate it are provided in the readme in that repo.
The version information is:
The text was updated successfully, but these errors were encountered: