Skip to content

Exception on HAL serialization with object mapper configured to sort map entries by keys #1515

@frank256

Description

@frank256

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 an HttpMessageNotWritableException due to a JsonMappingException with the cause being that org.springframework.hateoas.mediatype.hal.HalLinkRelation cannot be cast to java.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 implementing SortedMap (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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions