mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Fix: ResourceIdentifier.for_type_with_id can serialize unpersisted resources
This commit is contained in:
parent
5916014b48
commit
9745a2f735
@ -8,12 +8,13 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.for_type_with_id(type, id, options)
|
def self.for_type_with_id(type, id, options)
|
||||||
return nil if id.blank?
|
|
||||||
type = inflect_type(type)
|
type = inflect_type(type)
|
||||||
{
|
type = type_for(:no_class_needed, type, options)
|
||||||
id: id.to_s,
|
if id.blank?
|
||||||
type: type_for(:no_class_needed, type, options)
|
{ type: type }
|
||||||
}
|
else
|
||||||
|
{ id: id.to_s, type: type }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.raw_type_from_serializer_object(object)
|
def self.raw_type_from_serializer_object(object)
|
||||||
|
|||||||
@ -72,33 +72,33 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
def test_defined_type
|
def test_defined_type
|
||||||
actual = with_jsonapi_inflection :plural do
|
actual = with_jsonapi_inflection :plural do
|
||||||
actual_resource_identifier_object(WithDefinedTypeSerializer)
|
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
||||||
end
|
end
|
||||||
expected = { id: expected_model_id, type: 'with-defined-types' }
|
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_defined_type_not_inflected
|
def test_defined_type_not_inflected
|
||||||
actual = with_jsonapi_inflection :singular do
|
actual = with_jsonapi_inflection :singular do
|
||||||
actual_resource_identifier_object(WithDefinedTypeSerializer)
|
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
||||||
end
|
end
|
||||||
expected = { id: expected_model_id, type: 'with-defined-types' }
|
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_singular_type
|
def test_singular_type
|
||||||
actual = with_jsonapi_inflection :singular do
|
actual = with_jsonapi_inflection :singular do
|
||||||
actual_resource_identifier_object(AuthorSerializer)
|
actual_resource_identifier_object(AuthorSerializer, @model)
|
||||||
end
|
end
|
||||||
expected = { id: expected_model_id, type: 'author' }
|
expected = { id: expected_model_id(@model), type: 'author' }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_plural_type
|
def test_plural_type
|
||||||
actual = with_jsonapi_inflection :plural do
|
actual = with_jsonapi_inflection :plural do
|
||||||
actual_resource_identifier_object(AuthorSerializer)
|
actual_resource_identifier_object(AuthorSerializer, @model)
|
||||||
end
|
end
|
||||||
expected = { id: expected_model_id, type: 'authors' }
|
expected = { id: expected_model_id(@model), type: 'authors' }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -123,45 +123,69 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_object
|
def test_id_defined_on_object
|
||||||
actual = actual_resource_identifier_object(AuthorSerializer)
|
actual = actual_resource_identifier_object(AuthorSerializer, @model)
|
||||||
expected = { id: @model.id.to_s, type: expected_model_type }
|
expected = { id: @model.id.to_s, type: expected_model_type(@model) }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_blank_id
|
def test_blank_id
|
||||||
@model.id = nil
|
model = Author.new(id: nil, name: 'Steve K.')
|
||||||
actual = actual_resource_identifier_object(AuthorSerializer)
|
actual = actual_resource_identifier_object(AuthorSerializer, model)
|
||||||
expected = { type: expected_model_type }
|
expected = { type: expected_model_type(model) }
|
||||||
|
assert_equal actual, expected
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_type_with_id
|
||||||
|
id = 1
|
||||||
|
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
||||||
|
expected = { id: "1", type: 'admin-users' }
|
||||||
|
assert_equal actual, expected
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_type_with_id_given_blank_id
|
||||||
|
id = ""
|
||||||
|
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
||||||
|
expected = { type: 'admin-users' }
|
||||||
|
assert_equal actual, expected
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_for_type_with_id_inflected
|
||||||
|
id = 2
|
||||||
|
actual = with_jsonapi_inflection :singular do
|
||||||
|
ResourceIdentifier.for_type_with_id('admin_users', id, {})
|
||||||
|
end
|
||||||
|
expected = { id: "2", type: 'admin-user' }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_serializer
|
def test_id_defined_on_serializer
|
||||||
actual = actual_resource_identifier_object(WithDefinedIdSerializer)
|
actual = actual_resource_identifier_object(WithDefinedIdSerializer, @model)
|
||||||
expected = { id: 'special_id', type: expected_model_type }
|
expected = { id: 'special_id', type: expected_model_type(@model) }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_id_defined_on_fragmented
|
def test_id_defined_on_fragmented
|
||||||
actual = actual_resource_identifier_object(FragmentedSerializer)
|
actual = actual_resource_identifier_object(FragmentedSerializer, @model)
|
||||||
expected = { id: 'special_id', type: expected_model_type }
|
expected = { id: 'special_id', type: expected_model_type(@model) }
|
||||||
assert_equal actual, expected
|
assert_equal actual, expected
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def actual_resource_identifier_object(serializer_class)
|
def actual_resource_identifier_object(serializer_class, model)
|
||||||
serializer = serializer_class.new(@model)
|
serializer = serializer_class.new(model)
|
||||||
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
||||||
resource_identifier.as_json
|
resource_identifier.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def expected_model_type
|
def expected_model_type(model, inflection = ActiveModelSerializers.config.jsonapi_resource_type)
|
||||||
inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
with_jsonapi_inflection inflection do
|
||||||
@model.class.model_name.send(inflection)
|
model.class.model_name.send(inflection)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def expected_model_id
|
def expected_model_id(model)
|
||||||
@model.id.to_s
|
model.id.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user