active_model_serializers
active_model_serializers copied to clipboard
Specify root key fail when collection is empty
Expected behavior vs actual behavior
class Simpeg::JabatanSerializer < ActiveModel::Serializer
type :data
end
I specify the root key with 'data'. But that not work when collection is empty. The root name back to 'simpeg/jabatans'.
Steps to reproduce
class Simpeg::JabatanController < ApplicationController
def index
data = Simpeg::Jabatan.where(judul: 'nothing')
render json: data
end
end
Environment
ActiveModelSerializers Version 0.10.4 Rails Version 5.0.2 ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-linux]
Backtrace
(e.g., provide any applicable backtraces from your application)
Additonal helpful information
ActiveModel::Serializer.config.adapter = :json
@wirasto Thanks for the bug report. The JSON adapter uses the json_key method which is
# Used by adapter as resource root.
def json_key
root || _type || object.class.model_name.to_s.underscore
end
Can you try seeing what the value of Simpeg::JabatanSerializer.new(Simpeg::Jabatan.where(judul: 'nothing')).json_key is?
The value is data
@wirasto
The value is data
A little help here.. can you describe the data?
I specify the root key with 'data'. So the result value is 'data'
Oh, ok. so you're expecting
{ "data" : [] }
and getting
{ "simpeg/jabatans" : [] }
?
Is your collection actually an empty array? If so, see https://github.com/rails-api/active_model_serializers/blob/v0.10.5/lib/active_model/serializer/collection_serializer.rb#L30-L50 Given an empty array and no explicit serializer, AMS has no idea to know what 'type' of empty array it is...
I'm guessing that the issue is in step
# 3. get from collection name, if a named collection
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
where you want to lookup the serializer from the empty collection, but we just get the collection name. I suppose we could add a condition that tries to look up the serializer for the object.name, when present.
This would be addressed by something like https://github.com/rails-api/active_model_serializers/pull/1867
This issue still valid. With
def json_key
'data'
end
in my serialiser base class, it returns all data with the root key data when there is data, but returns other key when the list of objects to serialise is empty. Expected behaviour would be {"data": []}
Here's a failing test of my use case: https://github.com/rails-api/active_model_serializers/commit/6e46e673431f1d732d84a63571f2a8c46fd870d7