Active_model_serializers: Unable to define root / json_key for collection

Created on 25 May 2017  Â·  12Comments  Â·  Source: rails-api/active_model_serializers

Expected behavior vs actual behavior

Expected:

>> ActiveModel::Serializer::CollectionSerializer.new(Channel.all, root: "foo").as_json
=> {:foo=>[{:uuid=>"19fba400-2370-0135-38bd-6c4008a512a2", :slug=>"neutra", :title=>"neutra", :description=>nil}]}
>>

Actual

>> ActiveModel::Serializer::CollectionSerializer.new(Channel.all, root: "foo").as_json
=> [{:uuid=>"19fba400-2370-0135-38bd-6c4008a512a2", :slug=>"neutra", :title=>"neutra", :description=>nil}]
>>

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

ActiveModel::Serializer::CollectionSerializer.new($MODEL.all, root: "foo").as_json

Environment

ActiveModelSerializers Version (commit ref if not on tag): active_model_serializers (0.10.6)

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]

Also happens with ruby 2.3.1

OS Type & Version:

Integrated application and version (e.g., Rails, Grape, etc):

https://github.com/feedcast/core/blob/feature/api/Gemfile.lock

Backtrace

(e.g., provide any applicable backtraces from your application)

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

All 12 comments

@marceloboeira
which adapter are you using? What does ActiveModel::Serializer::CollectionSerializer.new(Channel.all, root: "foo", adapter: :json).as_json give you?

any particular reason you're calling the collection serializer directly?

>> ActiveModel::Serializer::CollectionSerializer.new(Channel.all, root: "foo", adapter: :json).as_json
=> [{:uuid=>"19fba400-2370-0135-38bd-6c4008a512a2", :slug=>"neutra", :title=>"neutra", :description=>nil}]

@bf4 I've tried to serialize using a ChannelsSerializer that inherits from CollectionSerializer, but it didn't work as well... ever since I was focusing on making the root work, but no success.

That's my app running. I had to create the root key manually, and use the collection to serialize an array.
https://github.com/feedcast/core/blob/feature/api/app/api/api/v1/channel.rb

Having the same errors, all my endpoint loses there root key.

@llaine I've been looking at the code for AMS to try to make sense of when the options are lost...
while I don't figure it out the solution I've been using a hack:

{ my_root: MyCollectionSerializer.new(collection).as_json }

@marceloboeira thanks for the answer, unfortunately, I'm using rails as an api with it.
And this MyCollectionSerializer.new(collection).as_json is kind of implicit, si I can't put it in the code ...
Any idea if there is a global configuration for this or?

I find it kind of strange to be reaching directly for the collection serializer like this. Any particular reason? Is your expected serialization based on docs?

In my case it's because the expected code is not working...

"Channels.all.to_json" does not use the collection serializer, neither the root...

Sent from my iPhone

On 7. Jun 2017, at 02:43, Benjamin Fleischer notifications@github.com wrote:

I find it kind of strange to be reaching directly for the collection serializer like this. Any particular reason? Is your expected serialization based on docs?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

+1 same pb for me since i migrate from 0.9 to 0.10

Sticking to my rails version and not upgrading solves the issue.

Having same issue. So my solution is to downgrade to 0.9.3.

render json: model, serializer: Api::V1::ModelSerializer
=> {"model" => attrs_hash }

As it supposed.

I think this issue has attracted various different problems that cause the root to be different from expected.

ActiveModelSerializers.config.adapter = :json
the base serializer has a non-overridable adapter: attributes
https://github.com/rails-api/active_model_serializers/blob/v0.10.10/lib/active_model/serializer.rb#L138
UserSerializer.new(user).to_json => {id: id, email: email .....}
But if you use it like this:
ActiveModelSerializers::SerializableResource.new( user, serializer: UserSerializer ).as_json => { user: {id: id, email: email .....} }
https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model_serializers/serializable_resource.rb

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iggant picture iggant  Â·  4Comments

yjukaku picture yjukaku  Â·  5Comments

axsuul picture axsuul  Â·  4Comments

AlexCppns picture AlexCppns  Â·  5Comments

Tybot204 picture Tybot204  Â·  3Comments