Expect: it returs attributes with root directory for json results by Serializer, for example:
UserSerializer.new(user).as_json
{ "user": { "id": "1" } }
Actual: it returns only attributew witout root
{ "id": "1" }
(e.g., detailed walkthrough, runnable script, example application)
# config/initializers/serializer.rb
require 'active_model_serializers'
ActiveModelSerializers.config.adapter = :json
# ActiveModelSerializers.config.adapter = :json_api # Not woking too
ActiveModelSerializers Version (commit ref if not on tag):
gem 'active_model_serializers', git: 'https://github.com/rails-api/active_model_serializers.git', branch: '0-10-stable'
Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
OS Type & Version:
Distributor ID: LinuxMint
Description: Linux Mint 19 Tara
Release: 19
Codename: tara
Integrated application and version (e.g., Rails, Grape, etc):
Rails 4.2.10
(e.g., provide any applicable backtraces from your application)
(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)
describe ParentSerializer do
let(:parent) { create(:parent) }
subject { described_class.new(parent).as_json['parent'] }
fit 'has some_attribute' do
expect(subject['some_attribute']).to eq(parent.some_attribute)
end
end
May make sense that the ParentSerializer is inheritance of UserSerializer which inheritance of ActiveModel::Serializer
NoMethodError: undefined method `[]' for nil:NilClass
0) ParentSerializer has some_attribute
Failure/Error: expect(subject['some_attribute']).to eq(parent.some_attribute)
NoMethodError:
undefined method `[]' for nil:NilClass
# ./spec/serializers/parent_serializer_spec.rb:7:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
PP.pp described_class.new(parent).as_json
{:id=>4574, ... :some_attribute=>false ... }
pass the adapter or the root option explicitly - also does not help, examples:
PP.pp described_class.new(parent, root: true).as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json).as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json, root: true).as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json_api).as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json_api, root: true).as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json_api, root: 'parent').as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json_api, root: 'parents').as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json, root: 'parent').as_json['parent'] # => nil
PP.pp described_class.new(parent, adapter: :json, root: 'parents').as_json['parent'] # => nil
# as same as for `:parent`, `:parents'
Any help is appreciated
@Mifrill I am currently stumbling the same issue as yours. I added the config file already.
# config/initializers/aws.rb
ActiveModelSerializers.config.adapter = :json
If I'm calling UserSerializer.new(user).as_json, the result does not include a root key. But when I use the same serializer in a controller action like so
render json: user, serializer: UserSerializer
returns with a root key.
Explanation for this is probably this : https://github.com/rails-api/active_model_serializers/issues/1965#issuecomment-257949274
I agree with @erbunao very much. @Mifrill can you please confirm the case?
@erbunao , @wasifhossain yeah, confirmed, besides, the root can be controlled by root: 'foo' as an option for render. Cool, thanks, now I see
thanks so much! @Mifrill