Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should there be support for recursive includes? #14

Open
NullVoxPopuli opened this issue Feb 28, 2017 · 3 comments
Open

Should there be support for recursive includes? #14

NullVoxPopuli opened this issue Feb 28, 2017 · 3 comments

Comments

@NullVoxPopuli
Copy link

NullVoxPopuli commented Feb 28, 2017

something like: json-api/json-api#940

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

@beauby
Copy link
Member

beauby commented Mar 1, 2017

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.

@NullVoxPopuli
Copy link
Author

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

@patodevilla
Copy link

patodevilla commented Dec 1, 2018

What is the syntax to get this to work with rails?

# @example 'posts.**' # => Include related posts, and all the included

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants