mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #2214 from jmeredith16/undeterminable-json-root
Fail if collection type cannot be inferred with json adapter (#2210)
This commit is contained in:
commit
54d40c79e8
@ -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:
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user