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

json serializer ignores nested associations #921

Closed
quaternion opened this issue May 21, 2015 · 8 comments
Closed

json serializer ignores nested associations #921

quaternion opened this issue May 21, 2015 · 8 comments

Comments

@quaternion
Copy link

Example 0.10.x:

class NodeSerializer < ActiveModel::Serializer
  attribute :id
end

class UserSerializer < ActiveModel::Serializer
  attribute :id
  attribute :name
  belongs_to :node # <--- ignored
end

class PostSerializer < ActiveModel::Serializer
  attribute :id
  belongs_to :user
end

PostSerializer produces json without a user node:

{ id: 1, user: { id: 1, name: 'Bob' } }

Is this a bug or a feature?

@quaternion quaternion changed the title json serializer ignores a nested associations json serializer ignores nested associations May 21, 2015
@groyoh
Copy link
Member

groyoh commented May 21, 2015

If you're using the JSON adapter then it is the expected behavior. The JSON adapter only render the attributes of associations (see here)

@quaternion
Copy link
Author

Yes, you are right. I use json adapter.

@groyoh
Copy link
Member

groyoh commented May 21, 2015

If you need a workaround, you can do this:

class UserSerializer < ActiveModel::Serializer
  attribute :id
  attribute :name
  attribute :node
  def node
    serializer = ActiveModel::Serializer.serializer_for(object.node).new(object.node)
    ActiveModel::Serializer::Adapter::Json.new(serializer).as_json
  end
end

@chrisbranson
Copy link
Contributor

I ran into this today, having previously used 0.9.x where nested associations were found. Is this 'expected behaviour' in that that's the way it is supposed to work, or would a PR be welcome? A small change gets this behaviour back - see my fork Current-RMS@80de719

@joaomdmoura
Copy link
Member

@quaternion ans @groyoh said, this is indeed the expected behaviour.
This approach takes into account a more lean implementation avoiding heavy responses and also trying to help people into build a "more RESTfull" API.
I'm no saying we will definitely keep it this way, but before move forward into a PR I would like to hear more opinions from other AMS members. cc @kurko @guilleiguaran
I'm closing this for now. But lets keep the discussion 😄

@aaronlerch
Copy link
Contributor

This one caught me doing an upgrade from 0.9 -> 0.10. The frustrating part is that I couldn't find any documentation about it, though perhaps I wasn't searching for the right terms. So I ended up spending a bit of time debugging why it was silently ignoring my has_many relationships. What really threw me off was that it worked for the top-level has_many, so it seemed more like a bug or a configuration issue than an "as intended" behavior.

I get the mindset behind guiding people towards a more lean implementation, however it seems that guidance would be better put into documentation and "how to" write-ups instead of a behavior change resulting in inconsistency. It seems like, unless you just plain don't want to support nested associations for "opinionated framework" reasons, offer the API and simply guide people away from using it.

As for my opinion, I think it should be supported to use has_many in nested serializers. Not every use-case is best served by fully "lean-ing" up the API. There are considerations to make, such as mobile-first scenarios in lower bandwidth areas. In this scenario (and where the app supports caching the information for offline access) it can be preferable to send a larger payload once instead of many smaller payloads. In the case of the app I'm working on, that's a tradeoff we have made in the API to better support the use-cases.

Hope that helps add a data point to your decision-making process. Thanks!

@chibicode
Copy link

Waiting for this PR: #952 and relevant issue: #835 (comment))

By the way, to add to @groyoh's comment (#921 (comment)), for has_many relationships, you can try something like this:

class Post
  attributes :comments

  def comments
    object
      .comments
      .map { |x| ActiveModel::Serializer::Adapter::FlattenJson.new(CommentSerializer.new(x)).as_json }
  end
end

@joaomdmoura
Copy link
Member

Hey everyone there is actually a new PR in place as well, it's a different approach I have not review it yet, but it seems nice: #1073

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

6 participants