diff --git a/lib/active_model_serializers/adapter/base.rb b/lib/active_model_serializers/adapter/base.rb index 18002af4a..10701eef9 100644 --- a/lib/active_model_serializers/adapter/base.rb +++ b/lib/active_model_serializers/adapter/base.rb @@ -48,7 +48,7 @@ def root end def include_meta(json) - json[meta_key] = meta if meta + json[meta_key] = meta unless meta.blank? json end end diff --git a/lib/active_model_serializers/adapter/json_api.rb b/lib/active_model_serializers/adapter/json_api.rb index 1fbfabe0d..cf0c0c6b4 100644 --- a/lib/active_model_serializers/adapter/json_api.rb +++ b/lib/active_model_serializers/adapter/json_api.rb @@ -320,7 +320,7 @@ def resource_object_for(serializer) # :'git-ref' => 'abc123' # } meta = meta_for(serializer) - resource_object[:meta] = meta unless meta.nil? + resource_object[:meta] = meta unless meta.blank? resource_object end diff --git a/test/adapter/json_api/resource_meta_test.rb b/test/adapter/json_api/resource_meta_test.rb index 7eec4365c..e8835ae06 100644 --- a/test/adapter/json_api/resource_meta_test.rb +++ b/test/adapter/json_api/resource_meta_test.rb @@ -17,6 +17,20 @@ class MetaBlockPostSerializer < ActiveModel::Serializer end end + class MetaBlockPostBlankMetaSerializer < ActiveModel::Serializer + attributes :id + meta do + {} + end + end + + class MetaBlockPostEmptyStringSerializer < ActiveModel::Serializer + attributes :id + meta do + '' + end + end + def setup @post = Post.new(id: 1337, comments: [], author: nil) end @@ -61,6 +75,24 @@ def test_meta_object_resource_in_array } assert_equal(expected, hash) end + + def test_meta_object_blank_omitted + hash = ActiveModel::SerializableResource.new( + @post, + serializer: MetaBlockPostBlankMetaSerializer, + adapter: :json_api + ).serializable_hash + refute hash[:data].key? :meta + end + + def test_meta_object_empty_string_omitted + hash = ActiveModel::SerializableResource.new( + @post, + serializer: MetaBlockPostEmptyStringSerializer, + adapter: :json_api + ).serializable_hash + refute hash[:data].key? :meta + end end end end diff --git a/test/serializers/meta_test.rb b/test/serializers/meta_test.rb index a555adb7e..5b856762d 100644 --- a/test/serializers/meta_test.rb +++ b/test/serializers/meta_test.rb @@ -28,6 +28,38 @@ def test_meta_is_present_with_root assert_equal(expected, actual) end + def test_meta_is_not_included_when_blank + actual = ActiveModel::SerializableResource.new( + @blog, + adapter: :json, + serializer: AlternateBlogSerializer, + meta: {} + ).as_json + expected = { + blog: { + id: 1, + title: 'AMS Hints' + } + } + assert_equal(expected, actual) + end + + def test_meta_is_not_included_when_empty_string + actual = ActiveModel::SerializableResource.new( + @blog, + adapter: :json, + serializer: AlternateBlogSerializer, + meta: '' + ).as_json + expected = { + blog: { + id: 1, + title: 'AMS Hints' + } + } + assert_equal(expected, actual) + end + def test_meta_is_not_included_when_root_is_missing actual = ActiveModel::SerializableResource.new( @blog, @@ -78,6 +110,42 @@ def test_meta_key_is_used_with_json_api assert_equal(expected, actual) end + def test_meta_key_is_not_present_when_blank_object_with_json_api + actual = ActiveModel::SerializableResource.new( + @blog, + adapter: :json_api, + serializer: AlternateBlogSerializer, + meta: {}, + meta_key: 'haha_meta' + ).as_json + expected = { + data: { + id: '1', + type: 'blogs', + attributes: { title: 'AMS Hints' } + } + } + assert_equal(expected, actual) + end + + def test_meta_key_is_not_present_when_empty_string_with_json_api + actual = ActiveModel::SerializableResource.new( + @blog, + adapter: :json_api, + serializer: AlternateBlogSerializer, + meta: '', + meta_key: 'haha_meta' + ).as_json + expected = { + data: { + id: '1', + type: 'blogs', + attributes: { title: 'AMS Hints' } + } + } + assert_equal(expected, actual) + end + def test_meta_is_not_present_on_arrays_without_root actual = ActiveModel::SerializableResource.new( [@blog],