-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 adapter does not handle nested associations #835
Comments
I actually thought about it yesterday!
It could be implemented as an option but Imho it isn't the way to go. |
+1 dealing with the very same issue at the moment with an inherited app - i agree that our request is humongous, but it's cached on the mobile client as i've been told i was expecting that I could define a nested includes and pass the object to the serializer and it would use it, but as far as i can tell now, it stops at level one, even though the active record object has everything loaded already |
I think in general this is not a good convention because of the bigger payload, as you said. Maybe as an option, but I don't think allowing for all this configuration is a good idea, especially when it runs counter to a convention. |
John, when you say that it runs counter to a convention, why do you mean by that? If people want to follow a convention, they could possibly go with JSONAPI instead. The issue here is more of a backward compatibility issue. We could use the same option as JSONAPI:
|
@frahugo is right in this case.
|
@joaomdmoura is this something that's already handled by the |
Here's a link to reference my above quote http://jsonapi.org/format/#document-structure-compound-documents |
@frahugo I think this should be handled by the |
This adapter, according to the documentation, does the following:
Am I misunderstanding the issue you're having? Do you want this to also be handled by the |
@frahugo What @joshsmith and I meant was that it is not usual and not a convention to choose big payloads including ever object related to the resource you are retrieving. Indeed the JsonApi adapter already enables you to retrieve included resources, but it's just one level deep. There is a work-around to this that would be define a method at your serializer. |
A work-around would be awesome. We have existing apps that expect a JSON structure as in 0.9.x. So as long as we can provide the same JSON and that caching works, that would be awesome.
|
@frahugo the work around would be something like bellow: Instead of use class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :comments
def comments
comments = []
object.comments.each do |comment|
comments << { content: comment.content, user: comment.user }
end
end
end |
Would it support caching? Caching is the main reason why we want to go with the latest code. |
Hell yeah! 😄 |
Sorry to re-open (even if was never closed) this discussion, but I agree with @frahugo. That said, @joaomdmoura solution could work, but is not DRY in my opinion. First-level nested association are serialised using their own serialisers, if found, with this solution I'm actually defining how to serialise comments inside the posts serialiser... It would be great to have this as an option in the serialiser class, something like:
so that if I have Looking forward to hear your thoughts :) |
However, for the time being, I created a new adapter called JsonCustom.
to
and it actually seems to be working. No option, it ALWAYS call recursively. That would be the best approach |
Line 29 also need to change into
|
Hey @iMacTia, first of all thank you for contributing 😄 |
Just submitted PR #952 on the master. Great timing for an rc2 with at least my commit and PR #936 (I REALLY need this), what do you think, @joaomdmoura ;)? |
Tks @iMacTia! Have just checked the PR. |
Needed this too. unless options[:root_key] == false
{ root => @result }
else
@result
end |
+1 for nested associations. |
+1 for option to include nested associations |
+1 for supporting nested associations deeper than 1 level |
👍 I both agree that it's a bad idea for the default, but the option to configure it to allow nested associations would be very useful |
+1 Another scenario here is when you have a recursive tree structure. Looking at #968 you can actually make this work with dot-notation, though @joaomdmoura I'm not sure if this is officially supported or something that just happens to work? render json: tree, include: ['children', 'children.children', 'children.children.children'] |
+1 |
Yeap @richmolj it's officially supported, but I think it only work with json-API adapter. |
Is there anything specific to to get this working as mentioned by @richmolj? It's not working for me using the latest on master and with the json-api adapter. render json: tree, include: ['children', 'children.children', 'children.children.children'] Do you need to define the nested relationship in the serializer? |
@feedthebayer Nops, all you would need the the usual relationship declaration, if it does not work, could you open a new issue if more info, it might be a little details because it is supposed to work. |
hey, I think #1127 resolves this. |
So #1158 and #1127 solve this problem. If you want to serialize all defined relationships (which might be dangerous if you have cycles in your data graph), you would just provide the Closing this for now, feel free to reopen if needed. |
Sorry to comment on this old thread, but this feature |
@nicolas-besnard It's actually documented in https://github.com/rails-api/active_model_serializers/blob/db6083af2fb9932f9e8591e1d964f1787aacdb37/docs/general/adapters.md#included Do you think you can improve the docs so that you would have found it? |
Well, firstly, it's on the |
@nicolas-besnard Looking forward a PR :) |
Does this actually work with the JSON API adapter ? |
@dan-corneanu Please open a new issue. (And clarify what 'this' and 'actually' refer to) |
It only supports the associations in the model being serialized. For instance if you serialize Blog, it will serialize the posts, but not the comments of each post.
This line https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json.rb#L14 should probably be changed to get a
serializable_hash
instead of just attributes.This was discovered while trying existing 0.9.x code on the master branch. Is this something that should be supported? If so, I can submit a pull request for it.
The text was updated successfully, but these errors were encountered: