-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
@wirasto Thanks for the bug report. The JSON adapter uses the
Can you try seeing what the value of |
The value is data |
@wirasto
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 |
This would be addressed by something like #1867 |
This issue still valid. With
in my serialiser base class, it returns all data with the root key Here's a failing test of my use case: |
Expected behavior vs actual behavior
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
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
The text was updated successfully, but these errors were encountered: