toedter / spring-hateoas-jsonapi

A JSON:API media type implementation for Spring HATEOAS
Apache License 2.0
106 stars 15 forks source link

Can't serialize model with null data #62

Closed atlas-tbrown closed 2 years ago

atlas-tbrown commented 2 years ago

According to the spec data is not required: A “relationship object” MUST contain at least one of the following:

But if i try to do:

return jsonApiModel() .model(pageEntityModel) .relationship("space", null, "Some link here", Links.NONE) .build();

I get: Cannot compute JSON:API resource id.::: null (through reference chain: com.toedter.spring.hateoas.jsonapi.JsonApiDocument["data"]->com.toedter.spring.hateoas.jsonapi.JsonApiData["relationships"]->java.util.HashMap["space"]

It seems the relationship serializer is trying to serialize the data even if its null, and then blowing up at: String id = JsonApiResourceIdentifier.getId(data, jsonApiConfiguration).value;

Am i doing something incorrect? I assume the fact that the relationship has an overload that takes no entity object means this is valid, yet it seems to be failing for me.

Thanks!

toedter commented 2 years ago

Thanks for reporting this. I'll take a look at it asap.

toedter commented 2 years ago

Fixed in the current snapshot. Please check if it works for you. Will be in the next release.

atlas-tbrown commented 2 years ago

Thank you for fixing that so quickly!