fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Nested includes not working as documented

Open TrevorHinesley opened this issue 7 years ago • 9 comments

Per the documentation, one should be able to specify nested relationships in the include key and only return the last relationship in dot notation. However, this is not working like that. Using the RSpec contexts:

options = {}
options[:include] = [:"actors.awards"]
MovieSerializer.new(movie, options).serializable_hash

This should only include awards, but it includes actors and awards.

Personally, I prefer this, but is it the intended behavior?

TrevorHinesley avatar Jul 19 '18 14:07 TrevorHinesley

The intended behaviour was to only serialize the last relationship - I'm on the fence as to the current behaviour - it's not precise, which I generally don't like, but I've taken advantage of the syntax

jshow avatar Jul 19 '18 15:07 jshow

ActiveModel::Serializers follows the current convention, of serializing all relationships in the dotted chain, but I understand both use cases for sure. Either way, I think it should be adjusted to match the intended behavior, or documentation should reflect the current implementation.

TrevorHinesley avatar Jul 19 '18 15:07 TrevorHinesley

now that I think of it, it would be tough to just serialize the end chain, as we need to walk through each serializer's instance - far easier to adjust the documentation, and there's something to be said for api consistency with AMS

jshow avatar Jul 19 '18 15:07 jshow

If you do not serialize all the chain - client will not be able to restore the chain in general use case

I think making docs more clear about this would be enough

ababich avatar Jul 20 '18 12:07 ababich

I think if you stop including the whole chain it's not jsonapi anymore. The spec is pretty explicit about full linkage. The example in the docs linked to above has some redundant keys that could be removed, also in the spec.

http://jsonapi.org/format/#document-compound-documents http://jsonapi.org/format/#fetching-includes

screen shot 2018-09-16 at 03 40 06

nruth avatar Sep 16 '18 02:09 nruth

I have a Trip model that has_many Ticket s. Both Trip and Ticket has_many Node s. Right now options[:include] = [:'tickets.nodes'] includes ticket nodes as well as trip nodes, while I was expecting only ticket nodes to be included. I think options[:include] = [:'tickets.nodes'] should only include ticket nodes.

karimalmur avatar Nov 12 '18 23:11 karimalmur

Other serializers implement it as it is here (e.g. https://github.com/jsonapi-rb/jsonapi-serializable) with all nodes included. I'd stick with that and update the documentation. As @nruth says, it's in the spec.

PhilT avatar Dec 30 '18 21:12 PhilT

Can this be closed per @nruth's comment?

dpikt avatar May 20 '19 19:05 dpikt

@dpikt I think documentation needs to be clarified then it should be good to go!

TrevorHinesley avatar May 23 '19 14:05 TrevorHinesley