mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add support for wildcard includes + improve perfs on JsonApi includes.
This commit is contained in:
@@ -51,9 +51,9 @@ module ActionController
|
||||
render json: @post, include: [comments: [:author]], adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_nested_has_many_include
|
||||
def render_resource_with_nested_has_many_include_wildcard
|
||||
setup_post
|
||||
render json: @post, include: 'author.roles', adapter: :json_api
|
||||
render json: @post, include: 'author.*', adapter: :json_api
|
||||
end
|
||||
|
||||
def render_resource_with_missing_nested_has_many_include
|
||||
@@ -96,7 +96,7 @@ module ActionController
|
||||
end
|
||||
|
||||
def test_render_resource_with_nested_has_many_include
|
||||
get :render_resource_with_nested_has_many_include
|
||||
get :render_resource_with_nested_has_many_include_wildcard
|
||||
response = JSON.parse(@response.body)
|
||||
expected_linked = [
|
||||
{
|
||||
|
||||
26
test/include_tree/from_include_args_test.rb
Normal file
26
test/include_tree/from_include_args_test.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class IncludeTree
|
||||
class FromStringTest < Minitest::Test
|
||||
def test_simple_array
|
||||
input = [:comments, :author]
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_array
|
||||
input = [:comments, posts: [:author, comments: [:author]]]
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
94
test/include_tree/from_string_test.rb
Normal file
94
test/include_tree/from_string_test.rb
Normal file
@@ -0,0 +1,94 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class IncludeTree
|
||||
class FromStringTest < Minitest::Test
|
||||
def test_single_string
|
||||
input = 'author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
end
|
||||
|
||||
def test_multiple_strings
|
||||
input = 'author,comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_multiple_strings_with_space
|
||||
input = 'author, comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_string
|
||||
input = 'posts.author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
end
|
||||
|
||||
def test_multiple_nested_string
|
||||
input = 'posts.author,posts.comments.author,comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_toplevel_star_string
|
||||
input = '*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_star_string
|
||||
input = 'posts.*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_star_middle_string
|
||||
input = 'posts.*.author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
refute(actual[:posts][:comments].key?(:unspecified))
|
||||
end
|
||||
|
||||
def test_nested_star_lower_precedence_string
|
||||
input = 'posts.comments.author,posts.*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
end
|
||||
|
||||
def test_toplevel_double_star_string
|
||||
input = '**'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:posts))
|
||||
end
|
||||
|
||||
def test_nested_double_star_string
|
||||
input = 'comments, posts.**'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:comments))
|
||||
refute(actual[:comments].key?(:author))
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:posts))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,79 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Utils
|
||||
class IncludeArgsToHashTest < Minitest::Test
|
||||
def test_nil
|
||||
input = nil
|
||||
expected = {}
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_empty_string
|
||||
input = ''
|
||||
expected = {}
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_single_string
|
||||
input = 'author'
|
||||
expected = { author: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_multiple_strings
|
||||
input = 'author,comments'
|
||||
expected = { author: {}, comments: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_multiple_strings_with_space
|
||||
input = 'author, comments'
|
||||
expected = { author: {}, comments: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_nested_string
|
||||
input = 'posts.author'
|
||||
expected = { posts: { author: {} } }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_multiple_nested_string
|
||||
input = 'posts.author,posts.comments.author,comments'
|
||||
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_empty_array
|
||||
input = []
|
||||
expected = {}
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_simple_array
|
||||
input = [:comments, :author]
|
||||
expected = { author: {}, comments: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_nested_array
|
||||
input = [:comments, posts: [:author, comments: [:author]]]
|
||||
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} }
|
||||
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user