Using the :json adapter, is there some way to exclude the namespace part from the root key?
I used version 0.9 before and my JSON representation of a User at api/v1/users/1 was something like:
{ "user": { "id": 1, "first_name": "Foo", "last_name": "Bar" } }
I recently upgraded to version 0.10 and now I get the namespace included in the root key:
{ "api/v1/user": { "id": 1,"first_name": "Foo", "last_name": "Bar" } }
I want the root key to be like before, that is "user" instead of "api/v1/user".
According to https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#json
The root key can't be overridden :-(
I think @beauby was doing some work on this.
You should be able to change it by controlling Serializer#json_key. See Adapter::Base#root
def root
serializer.json_key.to_sym if serializer.json_key
end
where Serializer#json_key is
# Used by adapter as resource root.
def json_key
root || _type || object.class.model_name.to_s.underscore
end
where root is self.root = instance_options[:root]
So, looks like you can make a pr to update the docs :)
also, arguably, root should be self.root ||= instance_options[:root] but really you can just use type: 'user' (or users) in the serializer
@NullVoxPopuli I read that, but I also read this at https://github.com/rails-api/active_model_serializers/blob/master/docs/general/rendering.md#overriding-the-root-key:
Overriding the resource root only applies when using the JSON adapter.
Is that documentation obsolete?
So, looks like you can make a pr to update the docs :)
@bf4 What docs are you suggesting to be updated? The one that @NullVoxPopuli linked to?
I have now two working solutions:
type in the serializer as @bf4 suggested.render json: @user, root: 'user'The second alternative is obviously only suitable when changing the root temporary.
I think I'll go the first alternative and programmatically determine the root key through a module or ApplicationSerializer. That has the advantage to work with the :json_api adapter as well.
Whatever would have helped you before you made the issue :) probably need to update those docs
B mobile phone
On May 31, 2016, at 7:20 AM, Hannes Elvemyr [email protected] wrote:
So, looks like you can make a pr to update the docs :)
@bf4 What docs are you suggesting to be updated? The one that @NullVoxPopuli linked to?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
The root key can't be overridden, and will be derived from the resource being serialized.
needs to be corrected in https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#json. It should be updated to make known, in brief, it _is_ possible to override the root key in the ways mentioned in this issue and link to the other guide sections:
root in render calls - Detailed in the Rendering Guidetype in the serializer - Detailed in the Serializers Guideref: #1486
Great work! Then I think we can consider this issue as resolved, right?
@ehannes yeah, thanks for your input. 👍
Most helpful comment
needs to be corrected in https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#json. It should be updated to make known, in brief, it _is_ possible to override the root key in the ways mentioned in this issue and link to the other guide sections:
rootinrendercalls - Detailed in the Rendering Guidetypein the serializer - Detailed in the Serializers Guideref: #1486