Fail if collection type cannot be inferred with json adapter (#2210)

This commit is contained in:
Joe Meredith 2017-10-30 08:33:31 -04:00
parent 88367da970
commit c6a14c9eac
3 changed files with 11 additions and 3 deletions

View File

@ -17,6 +17,7 @@ Fixes:
- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4) - [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
- [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm) - [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
- [#2214](https://github.com/rails-api/active_model_serializers/pull/2214) Fail if unable to infer collection type with json adapter. (@jmeredith16)
Misc: Misc:

View File

@ -46,7 +46,10 @@ module ActiveModel
# 3. get from collection name, if a named collection # 3. get from collection name, if a named collection
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
# 4. key may be nil for empty collection and no serializer option # 4. key may be nil for empty collection and no serializer option
key && key.pluralize key &&= key.pluralize
# 5. fail if the key cannot be determined
key || fail(ArgumentError, 'Cannot infer root key from collection type. Please
specify the root or each_serializer option, or render a JSON String')
end end
# rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/CyclomaticComplexity

View File

@ -93,12 +93,16 @@ module ActiveModel
resource = [] resource = []
resource.define_singleton_method(:name) { nil } resource.define_singleton_method(:name) { nil }
serializer = collection_serializer.new(resource) serializer = collection_serializer.new(resource)
assert_nil serializer.json_key assert_raise ArgumentError do
serializer.json_key
end
end end
def test_json_key_with_resource_without_name_and_no_serializers def test_json_key_with_resource_without_name_and_no_serializers
serializer = collection_serializer.new([]) serializer = collection_serializer.new([])
assert_nil serializer.json_key assert_raise ArgumentError do
serializer.json_key
end
end end
def test_json_key_with_empty_resources_with_serializer def test_json_key_with_empty_resources_with_serializer