Apply key transforms to keys referenced in values

This commit is contained in:
Ben Mills
2016-03-31 14:24:31 -06:00
parent d30aa4c44f
commit 3498647d1a
30 changed files with 579 additions and 223 deletions

View File

@@ -129,7 +129,7 @@ module ActiveModelSerializers
assert_equal({
data: {
id: '1',
type: 'virtual_values',
type: 'virtual-values',
relationships: {
maker: { data: { id: 1 } },
reviews: { data: [{ id: 1 }, { id: 2 }] }

View File

@@ -63,7 +63,7 @@ module ActiveModelSerializers
expected = {
data: {
id: '1',
type: 'virtual_values',
type: 'virtual-values',
relationships: {
maker: { data: { id: 1 } },
reviews: { data: [{ id: 1 }, { id: 2 }] }

View File

@@ -216,7 +216,7 @@ module ActiveModelSerializers
expected = {
related: {
data: [{
type: 'spam_unrelated_links',
type: 'spam-unrelated-links',
id: '456'
}]
}
@@ -366,12 +366,12 @@ module ActiveModelSerializers
adapter: :json_api,
include: '*').serializable_hash
expected = [
type: 'nested_posts', id: '2',
type: 'nested-posts', id: '2',
relationships: {
nested_posts: {
:"nested-posts" => {
data: [
{ type: 'nested_posts', id: '1' },
{ type: 'nested_posts', id: '2' }
{ type: 'nested-posts', id: '1' },
{ type: 'nested-posts', id: '2' }
]
}
}

View File

@@ -80,10 +80,10 @@ module ActiveModelSerializers
}
},
author: 'http://example.com/link_authors/1337',
link_authors: 'http://example.com/link_authors',
:"link-authors" => 'http://example.com/link_authors',
resource: 'http://example.com/resource',
posts: 'http://example.com/link_authors/1337/posts',
yet_another: 'http://example.com/resource/1337'
:"yet-another" => 'http://example.com/resource/1337'
}
assert_equal(expected, hash[:data][:links])
end

View File

@@ -25,7 +25,6 @@ module ActiveModelSerializers
context = Minitest::Mock.new
context.expect(:request_url, original_url)
context.expect(:query_parameters, query_parameters)
context.expect(:key_transform, nil)
@options = {}
@options[:serialization_context] = context
end

View File

@@ -151,11 +151,8 @@ module ActiveModelSerializers
private
def test_relationship(expected, params = {})
options = params.fetch(:options, {})
links = params.fetch(:links, {})
meta = params[:meta]
parent_serializer = AuthorSerializer.new(@author)
relationship = Relationship.new(parent_serializer, @serializer, options, links, meta)
relationship = Relationship.new(parent_serializer, @serializer, nil, params)
assert_equal(expected, relationship.as_json)
end
end

View File

@@ -22,7 +22,7 @@ module ActiveModelSerializers
end
def test_defined_type
test_type(WithDefinedTypeSerializer, 'with_defined_type')
test_type(WithDefinedTypeSerializer, 'with-defined-type')
end
def test_singular_type
@@ -58,7 +58,7 @@ module ActiveModelSerializers
def test_type(serializer_class, expected_type)
serializer = serializer_class.new(@model)
resource_identifier = ResourceIdentifier.new(serializer)
resource_identifier = ResourceIdentifier.new(serializer, nil)
expected = {
id: @model.id.to_s,
type: expected_type
@@ -69,7 +69,7 @@ module ActiveModelSerializers
def test_id(serializer_class, id)
serializer = serializer_class.new(@model)
resource_identifier = ResourceIdentifier.new(serializer)
resource_identifier = ResourceIdentifier.new(serializer, nil)
inflection = ActiveModelSerializers.config.jsonapi_resource_type
type = @model.class.model_name.send(inflection)
expected = {

View File

@@ -54,7 +54,7 @@ module ActiveModel
adapter: :json_api
).serializable_hash
expected = {
comments_count: @post.comments.count
:"comments-count" => @post.comments.count
}
assert_equal(expected, hash[:data][:meta])
end
@@ -69,8 +69,8 @@ module ActiveModel
).serializable_hash
expected = {
:data => [
{ :id => '1337', :type => 'posts', :meta => { :comments_count => 0 } },
{ :id => '1339', :type => 'posts', :meta => { :comments_count => 1 } }
{ :id => '1337', :type => 'posts', :meta => { :"comments-count" => 0 } },
{ :id => '1339', :type => 'posts', :meta => { :"comments-count" => 1 } }
]
}
assert_equal(expected, hash)

View File

@@ -36,13 +36,13 @@ module ActiveModelSerializers
belongs_to :author
end
def mock_request(key_transform = nil)
def mock_request(transform = nil)
context = Minitest::Mock.new
context.expect(:request_url, URI)
context.expect(:query_parameters, {})
context.expect(:key_transform, key_transform)
context.expect(:url_helpers, Rails.application.routes.url_helpers)
@options = {}
@options[:key_transform] = transform if transform
@options[:serialization_context] = context
end
@@ -64,7 +64,7 @@ module ActiveModelSerializers
@comment2.post = @post
end
def test_success_document_key_transform_default
def test_success_document_transform_default
mock_request
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
@@ -98,7 +98,7 @@ module ActiveModelSerializers
}, result)
end
def test_success_document_key_transform_global_config
def test_success_document_transform_global_config
mock_request
result = with_config(key_transform: :camel_lower) do
serializer = PostSerializer.new(@post)
@@ -134,7 +134,7 @@ module ActiveModelSerializers
}, result)
end
def test_success_doc_key_transform_serialization_ctx_overrides_global
def test_success_doc_transform_serialization_ctx_overrides_global
mock_request(:camel)
result = with_config(key_transform: :camel_lower) do
serializer = PostSerializer.new(@post)
@@ -144,7 +144,7 @@ module ActiveModelSerializers
assert_equal({
Data: {
Id: '1337',
Type: 'posts',
Type: 'Posts',
Attributes: {
Title: 'Title 1',
Body: 'Body 1',
@@ -152,12 +152,12 @@ module ActiveModelSerializers
},
Relationships: {
Author: {
Data: { Id: '1', Type: 'authors' }
Data: { Id: '1', Type: 'Authors' }
},
Comments: {
Data: [
{ Id: '7', Type: 'comments' },
{ Id: '12', Type: 'comments' }
{ Id: '7', Type: 'Comments' },
{ Id: '12', Type: 'Comments' }
] }
},
Links: {
@@ -170,8 +170,8 @@ module ActiveModelSerializers
}, result)
end
def test_success_document_key_transform_dashed
mock_request(:dashed)
def test_success_document_transform_dash
mock_request(:dash)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
@@ -204,7 +204,7 @@ module ActiveModelSerializers
}, result)
end
def test_success_document_key_transform_unaltered
def test_success_document_transform_unaltered
mock_request(:unaltered)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
@@ -238,7 +238,7 @@ module ActiveModelSerializers
}, result)
end
def test_success_document_key_transform_undefined
def test_success_document_transform_undefined
mock_request(:zoot)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
@@ -247,7 +247,7 @@ module ActiveModelSerializers
end
end
def test_success_document_key_transform_camel
def test_success_document_transform_camel
mock_request(:camel)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
@@ -255,7 +255,7 @@ module ActiveModelSerializers
assert_equal({
Data: {
Id: '1337',
Type: 'posts',
Type: 'Posts',
Attributes: {
Title: 'Title 1',
Body: 'Body 1',
@@ -263,12 +263,12 @@ module ActiveModelSerializers
},
Relationships: {
Author: {
Data: { Id: '1', Type: 'authors' }
Data: { Id: '1', Type: 'Authors' }
},
Comments: {
Data: [
{ Id: '7', Type: 'comments' },
{ Id: '12', Type: 'comments' }
{ Id: '7', Type: 'Comments' },
{ Id: '12', Type: 'Comments' }
] }
},
Links: {
@@ -281,7 +281,7 @@ module ActiveModelSerializers
}, result)
end
def test_success_document_key_transform_camel_lower
def test_success_document_transform_camel_lower
mock_request(:camel_lower)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
@@ -315,7 +315,7 @@ module ActiveModelSerializers
}, result)
end
def test_error_document_key_transform_default
def test_error_document_transform_default
mock_request
resource = ModelWithErrors.new
resource.errors.add(:published_at, 'must be in the future')
@@ -327,7 +327,7 @@ module ActiveModelSerializers
{ :errors =>
[
{
:source => { :pointer => '/data/attributes/published_at' },
:source => { :pointer => '/data/attributes/published-at' },
:detail => 'must be in the future' },
{
:source => { :pointer => '/data/attributes/title' },
@@ -338,7 +338,7 @@ module ActiveModelSerializers
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_global_config
def test_error_document_transform_global_config
mock_request
result = with_config(key_transform: :camel) do
resource = ModelWithErrors.new
@@ -352,11 +352,11 @@ module ActiveModelSerializers
{ :Errors =>
[
{
:Source => { :Pointer => '/data/attributes/published_at' },
:Source => { :Pointer => '/data/attributes/PublishedAt' },
:Detail => 'must be in the future'
},
{
:Source => { :Pointer => '/data/attributes/title' },
:Source => { :Pointer => '/data/attributes/Title' },
:Detail => 'must be longer'
}
]
@@ -364,7 +364,7 @@ module ActiveModelSerializers
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_serialization_ctx_overrides_global
def test_error_document_transform_serialization_ctx_overrides_global
mock_request(:camel)
result = with_config(key_transform: :camel_lower) do
resource = ModelWithErrors.new
@@ -378,11 +378,11 @@ module ActiveModelSerializers
{ :Errors =>
[
{
:Source => { :Pointer => '/data/attributes/published_at' },
:Source => { :Pointer => '/data/attributes/PublishedAt' },
:Detail => 'must be in the future'
},
{
:Source => { :Pointer => '/data/attributes/title' },
:Source => { :Pointer => '/data/attributes/Title' },
:Detail => 'must be longer'
}
]
@@ -390,8 +390,8 @@ module ActiveModelSerializers
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_dashed
mock_request(:dashed)
def test_error_document_transform_dash
mock_request(:dash)
resource = ModelWithErrors.new
resource.errors.add(:published_at, 'must be in the future')
@@ -405,7 +405,7 @@ module ActiveModelSerializers
{ :errors =>
[
{
:source => { :pointer => '/data/attributes/published_at' },
:source => { :pointer => '/data/attributes/published-at' },
:detail => 'must be in the future'
},
{
@@ -417,7 +417,7 @@ module ActiveModelSerializers
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_unaltered
def test_error_document_transform_unaltered
mock_request(:unaltered)
resource = ModelWithErrors.new
@@ -438,7 +438,7 @@ module ActiveModelSerializers
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_undefined
def test_error_document_transform_undefined
mock_request(:krazy)
resource = ModelWithErrors.new
@@ -453,7 +453,7 @@ module ActiveModelSerializers
end
end
def test_error_document_key_transform_camel
def test_error_document_transform_camel
mock_request(:camel)
resource = ModelWithErrors.new
@@ -467,14 +467,14 @@ module ActiveModelSerializers
expected_errors_object =
{ :Errors =>
[
{ :Source => { :Pointer => '/data/attributes/published_at' }, :Detail => 'must be in the future' },
{ :Source => { :Pointer => '/data/attributes/title' }, :Detail => 'must be longer' }
{ :Source => { :Pointer => '/data/attributes/PublishedAt' }, :Detail => 'must be in the future' },
{ :Source => { :Pointer => '/data/attributes/Title' }, :Detail => 'must be longer' }
]
}
assert_equal expected_errors_object, result
end
def test_error_document_key_transform_camel_lower
def test_error_document_transform_camel_lower
mock_request(:camel_lower)
resource = ModelWithErrors.new
@@ -488,7 +488,7 @@ module ActiveModelSerializers
expected_errors_object =
{ :errors =>
[
{ :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' },
{ :source => { :pointer => '/data/attributes/publishedAt' }, :detail => 'must be in the future' },
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' }
]
}