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

Add docs for include in Json adapter. #1243

Open
beauby opened this issue Oct 6, 2015 · 24 comments
Open

Add docs for include in Json adapter. #1243

beauby opened this issue Oct 6, 2015 · 24 comments

Comments

@beauby
Copy link
Contributor

beauby commented Oct 6, 2015

The include option with Json adapter works the same as with the JsonApi adapter, but it's not written anywhere in the docs.

@vasilakisfil
Copy link
Contributor

That's interesting! Do you know if fields or something relevant is also working with JSON adapter? If not is there any interest to send a pull request for that ? Cause I really need it and I am going to implement it anyway..

@beauby
Copy link
Contributor Author

beauby commented Oct 6, 2015

@vasilakisfil Currently you can use the fields option with Json/Attributes adapters, but you can only filter the primary resources (render json: @posts, fields: 'id, title, body').

@beauby
Copy link
Contributor Author

beauby commented Oct 6, 2015

I am not sure extending the Json adapter to fit the broader JSON API style fields option (where you specify the attributes for each type) is a good thing. The goal is to keep Json/Attributes adapters very simple.

@vasilakisfil
Copy link
Contributor

given that's an adapter I don't think it's an issue. Plus there should be
an adapter that opts for compatibility with 0.9.x and 0.8.x (supporting
only, except etc options). Otherwise the way to go is create an external
plugin adapter just like ember? is there support for that?
On Oct 6, 2015 5:03 PM, "Lucas Hosseini" notifications@github.com wrote:

I am not sure extending the Json adapter to fit the broader JSON API style
fields option (where you specify the attributes for each type) is a good
thing. The goal is to keep Json/Attributes adapters very simple.


Reply to this email directly or view it on GitHub
#1243 (comment)
.

@beauby
Copy link
Contributor Author

beauby commented Oct 6, 2015

Oh yeah, AMS is deisgned in such a way that you can create your own adapter and/or extend an existing one. I agree the migration from 0.8/0.9 should be made as easy as possible, I'm just not sure what's the best way to make that happen.

@NullVoxPopuli
Copy link
Contributor

maybe for the initial 0.10 release, we could copy the old adapters? maybe name them json_08, json_09 etc

@NullVoxPopuli
Copy link
Contributor

and then in the next release after 0.10, remove them?

@beauby
Copy link
Contributor Author

beauby commented Oct 6, 2015

First we have to make sure it is actually possible to build such legacy adapters.

@phcoliveira
Copy link
Contributor

@beauby, is there any reason to use the option include when rendering an object? My point is, if that option must be included when of rendering - render json: @posts, include: 'comments' e.g. -, I expect the serializer to exclude the comments relationship when used without the include option.

I have a use case where a user can have many sessions, in order to be able to sign them all out in case something bad happens. I have something like this:

class SessionsController
# ...
render json: @session, include: 'user'
# ...
end

class SessionSerializer
# ...

# I am not asking for the "UserSerializer" to include the "sessions" relationship.
# Nevertheless, it does. The "UserSerializer", for its own logical reasons, also
# declares its own relationship with "sessions" and other models.
# I should explicitly use the "include" option here if I wanted the other relationships
# to be included.
has_one :user, serializer: UserSerializer # include: ['other', 'relationships']
# ...
end

In my use case, I don't want the relationships of the user model to be included, but they do.

@beauby
Copy link
Contributor Author

beauby commented Oct 9, 2015

@phcoliveira Could you open a separate issue with the definition of your serializers and your render calls and a description of the unexpected behavior?

@bdmac
Copy link
Contributor

bdmac commented Oct 19, 2015

The include option does not seem to work for me w/the JSON adapter in RC3 at least. I cannot get it to include associations 2 levels down; just the top level seems to get included. Take the following for example:

admin -< businesses - industry

The business objects get nested appropriately in an array under admin but they do not include their industry. The key is not even present at all.

@beauby
Copy link
Contributor Author

beauby commented Oct 19, 2015

@bdmac Did you specify the include: '**' option on your render call?

@bdmac
Copy link
Contributor

bdmac commented Oct 19, 2015

Yes I tried that and also the more specific flavored of include.


Sent from Mailbox

On Mon, Oct 19, 2015 at 9:25 AM, Lucas Hosseini notifications@github.com
wrote:

@bdmac Did you specify the include: '**' option on your render call?

Reply to this email directly or view it on GitHub:
#1243 (comment)

@beauby
Copy link
Contributor Author

beauby commented Oct 19, 2015

@bdmac Could you open a separate issue in which you provide your models, serializers and render calls?

@bf4
Copy link
Member

bf4 commented Dec 9, 2015

@beauby This is a great issue. As the domain master of includes, I'd love if you added some docs of how it behaves and what data structures are inputs and outputs.

@mepatterson
Copy link

I'm seeing what might be the same issue. I can't really get any use out of the include feature at all for the problem I'm trying to solve. I'm using the simple JSON adapter.

I have an Organization:

class OrganizationSerializer < ActiveModel::Serializer
  attributes :id, :name, :created_at
  has_many :apps, class_name: 'Client::Application', serializer: ClientAppSerializer
  has_many :services
end

and a ClientAppSerializer:

class ClientAppSerializer < ActiveModel::Serializer
  attributes :id, :name, :created_at
  belongs_to :organization
end

and a ServiceSerializer:

class ServiceSerializer < ActiveModel::Serializer
  attributes :id, :name, :created_at, :updated_at
  belongs_to :organization
end

Pretty simple, right?

Let's say I want to serialize starting at the App level:

render json: @app, serializer: ClientAppSerializer

I'll get the serialized data for the App, and the serialized data for the Organization, but the Organization won't have any Services array.

So I try this...

render json: @app, serializer: ClientAppSerializer, include: 'organization.services'

... but nothing changes in my serialized output. No services.

Am I missing something fundamental here?

@beauby
Copy link
Contributor Author

beauby commented Jan 9, 2016

@mepatterson Could you open a separate issue adding the current output you're getting?

@mepatterson
Copy link

Done. #1421

@d3crypt3d
Copy link

Hello, guys.

Can you tell me, please, whether it's possible to use this option inside the serializer class itself

class FooSerializer < ActiveModel::Serializer
  attributes :foo, :bar, :baz

  # something like this
  include: '*'
end

rather than inside controller?

@bf4
Copy link
Member

bf4 commented Jan 10, 2016

@d3crypt3d short answer is no. It's an adapter option. Please open a new issue to make a proposal if you'd like to discuss further, or better, join us on the slack chat. See https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md

@charlie-wasp
Copy link
Contributor

@bf4 sorry for bothering, but is it issue relevant either? Quick check showed that include option is not working anymore for JSON adapter

@bf4
Copy link
Member

bf4 commented Mar 13, 2017

@charlie-wasp I had thought it did... we have tests... but it's been a while since I worked on that area...

@charlie-wasp
Copy link
Contributor

charlie-wasp commented Mar 13, 2017

@bf4 sorry, my bad, I misunderstood how include option works. Yes, include works with the JSON adapter and even with the attributes adapter. I tested it on the gem version 0.10.2. However in docs include option is mentioned only in the JSON API section

@bf4
Copy link
Member

bf4 commented Mar 14, 2017

@charlie-wasp Would love a PR to improve :)

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

No branches or pull requests

10 participants