mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Omit meta when blank
This commit is contained in:
@@ -17,6 +17,20 @@ module ActiveModel
|
||||
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 @@ module ActiveModel
|
||||
}
|
||||
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
|
||||
|
||||
@@ -28,6 +28,38 @@ module ActiveModel
|
||||
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 @@ module ActiveModel
|
||||
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],
|
||||
|
||||
Reference in New Issue
Block a user