diff --git a/lib/active_model_serializers/adapter/json_api/resource_identifier.rb b/lib/active_model_serializers/adapter/json_api/resource_identifier.rb index 3ff7118d..f7a8e2e9 100644 --- a/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +++ b/lib/active_model_serializers/adapter/json_api/resource_identifier.rb @@ -38,8 +38,11 @@ module ActiveModelSerializers end def as_json - return nil if id.blank? - { id: id, type: type } + if id.blank? + { type: type } + else + { id: id.to_s, type: type } + end end protected diff --git a/test/adapter/json_api/type_test.rb b/test/adapter/json_api/type_test.rb index 84bfe494..53c4ca75 100644 --- a/test/adapter/json_api/type_test.rb +++ b/test/adapter/json_api/type_test.rb @@ -128,6 +128,13 @@ module ActiveModelSerializers assert_equal actual, expected end + def test_blank_id + @model.id = nil + actual = actual_resource_identifier_object(AuthorSerializer) + expected = { type: expected_model_type } + assert_equal actual, expected + end + def test_id_defined_on_serializer actual = actual_resource_identifier_object(WithDefinedIdSerializer) expected = { id: 'special_id', type: expected_model_type }