Skip to content
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

Specify root key fail when collection is empty #2087

Open
ghost opened this issue Mar 24, 2017 · 8 comments
Open

Specify root key fail when collection is empty #2087

ghost opened this issue Mar 24, 2017 · 8 comments

Comments

@ghost
Copy link

ghost commented Mar 24, 2017

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

@bf4
Copy link
Member

bf4 commented Mar 28, 2017

@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?

@ghost
Copy link
Author

ghost commented Mar 29, 2017

The value is data

@bf4
Copy link
Member

bf4 commented Mar 29, 2017

@wirasto

The value is data

A little help here.. can you describe the data?

@ghost
Copy link
Author

ghost commented Mar 29, 2017

I specify the root key with 'data'. So the result value is 'data'

@bf4
Copy link
Member

bf4 commented Mar 29, 2017

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...

@bf4
Copy link
Member

bf4 commented Mar 29, 2017

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.

@bf4
Copy link
Member

bf4 commented Apr 12, 2017

This would be addressed by something like #1867

@sergioisidoro
Copy link

sergioisidoro commented Sep 23, 2022

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:
6e46e67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants