Spring-hateoas: Curies relation can be added to `_links` without any curie given by the curie provider

Created on 12 Jan 2021  路  3Comments  路  Source: spring-projects/spring-hateoas

If there is a : (colon) in a relation of a link, then curiedLinkPresent = true in Jackson2HalModule. This leads to that if a curie provider is configured, but no curie is given by curieProvider.getCurieInformation(Links.of(links)), the sortedLinks map suddenly has a "curies -> []".

Completed serialization then looks something like: "...,_links":{"colon:rel":{"href":"/test"},"curies"}...
This is wrong. "curies" should never be added in that case.

Introduced by:
https://github.com/spring-projects/spring-hateoas/commit/fc9d2a6777e0d3c8775d62c0d260727e7a450b91

Proposed solution, add in Jackson2HalModule:

if( !curies.isEmpty() ){
  sortedLinks.put(HalLinkRelation.CURIES, curies);
}
bug mediatypes

Most helpful comment

The spec defines link object keys to either be a registered link relation or a URI. colon:rel is indeed a valid URI, so we can and have to keep it as is even without a curie present. Definitely also a back-port candidate into 1.2.x.

All 3 comments

Shouldn't we rather reject that case if a CurieProvider is registered? Would colon:rel make sense standalone without any curie defined for colon?

Depends on what you mean by reject. curies should not be set if no curies are present to avoid invalid serialization.
This could be done by not settings curiedLinkPresent = true when no curieProvider for current rel or by some other check.

I guess it would be an edge/invalid case where one would want to show colon:rel without accompanying curie. Don't think it should be removed or anything, but it shouldn't break the serializing.

The spec defines link object keys to either be a registered link relation or a URI. colon:rel is indeed a valid URI, so we can and have to keep it as is even without a curie present. Definitely also a back-port candidate into 1.2.x.

Was this page helpful?
0 / 5 - 0 ratings