-
Notifications
You must be signed in to change notification settings - Fork 472
Exception on HAL serialization with object mapper configured to sort map entries by keys #1515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you provide an example that's reproducing the error? I think we should be able to work around this Jackson limitation by mapping the |
Thanks for the fast reply! I cannot attach code from the original project, so I tried to prepare a very minimal demo using Spring Data Rest. Of course, there would not be any meaningful sorting taking place here. But you can see the exception. The original project also makes heavy use of custom RepresentationModels, RepresentationModelProcessors and other HATEOAS functionality not related to Spring Data Rest. spring-hateoas-hal-map-sort-demo.zip edit: See |
Actually, it should be reproducible using an |
…ing is enabled. Prior to this commit, the map of links to be rendered would key by LinkRelation. Unfortunately, Jackson requires `Map` keys to implement `Comparable` in case `SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS` is enabled. We now use the link relation's value as key right aways as Strings are comparable out of the box. Switched to use Spring's MultiValueMap to avoid having to deal with the value list initialization ourselves.
This is in place for 1.4, 1.3 and 1.2 snapshots. Feel free to give it a spin. |
Thank you very much! Tried the 1.2.6 snapshot today. Unfortunately, there is still an exception of the same type thrown when rendering embedded collections (via line 338 of Jackson2HalModule). Sorry, I did not catch this one when analyzing it at first. Stack trace:
|
@odrotbohm Ping, since I'm not sure whether you see this. : ) |
I get notified by GitHub, thanks 🙃. |
We now also eagerly map the keys for the _embedded map in a HAL response to Strings to make sure they work properly when sorting of map keys is activated for the Jackson ObjectMapper.
We now also eagerly map the keys for the _embedded map in a HAL response to Strings to make sure they work properly when sorting of map keys is activated for the Jackson ObjectMapper.
We now also eagerly map the keys for the _embedded map in a HAL response to Strings to make sure they work properly when sorting of map keys is activated for the Jackson ObjectMapper.
Fixes pushed for that. Would be cool if you could take the snapshots for another spin. |
Tried the new snapshot. Everything seems to work now. |
This concerns JSON HAL serialization using Spring Boot 2.4.4 / Spring HATEOAS 1.2.5.
When using an object mapper which has the feature
SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS
enabled (which is, unfortunately, mandatory for me in a legacy project), I will get anHttpMessageNotWritableException
due to aJsonMappingException
with the cause being thatorg.springframework.hateoas.mediatype.hal.HalLinkRelation
cannot be cast tojava.lang.Comparable
.This is due to the Jackson MapSerializer being used to serialize the links, which expects the map keys to be comparable if the feature is enabled. However, HalLinkRelation does not implement
Comparable
nor is the map (I understand the links are already sorted?) that is passed into it implementingSortedMap
(this is checked).I tried to customize the object mapper with a custom map serializer that adds the links to a temporary TreeMap so that they are not sorted again by the MapSerializer, however, as the map serializer is constructed directly within the Jackson2HalModule (lines 202-5), I currently do not see how I can intervene here.
I'd consider this more or less a bug (?), however, if you have a hint for a workaround, I'd also appreciate that very much!
Disabling the feature is, unfortunately, not an option for me. It worked with Spring HATEOAS before 1.0.0.
The text was updated successfully, but these errors were encountered: