jsonapi-renderer icon indicating copy to clipboard operation
jsonapi-renderer copied to clipboard

Should there be support for recursive includes?

Open NullVoxPopuli opened this issue 8 years ago • 3 comments

something like: https://github.com/json-api/json-api/issues/940

basically, if you have a tree structure, where your relationships are parent/children, you can never get all the children.

NullVoxPopuli avatar Feb 28 '17 19:02 NullVoxPopuli

I'm up for supporting this if/when it gets into the spec. Note that in your case, you can always pre-compute the depth of the tree and build an adequate include directive.

beauby avatar Mar 01 '17 14:03 beauby

I actually ended up doing this monkeypatch:

# To support recursive include directives
# e.g.: 'children[*]'
module ActiveModelSerializers
  module Adapter
    class JsonApi
      def process_relationships(serializer, include_slice)
        if (key = include_slice.to_hash.keys.first.to_s).end_with?('[*]')
          rel = key.remove('[*]')
          new_rel = "#{rel}.#{rel}[*]"

          include_slice = JSONAPI::IncludeDirective.new(
            new_rel,
            allow_wildcard: true
          )
        end

        serializer.associations(include_slice).each do |association|
          process_relationship(association.serializer, include_slice[association.key])
        end
      end
    end
  end
end

NullVoxPopuli avatar Mar 01 '17 15:03 NullVoxPopuli

What is the syntax to get this to work with rails? https://github.com/jsonapi-rb/jsonapi-renderer/blob/daa95d830a6f71a39304c8db20c8acd89e34b7a7/lib/jsonapi/include_directive.rb#L12

patodevilla avatar Dec 01 '18 22:12 patodevilla