active_model_serializers 0.8.3 → 0.10.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +17 -0
- data/.rubocop.yml +105 -0
- data/.simplecov +110 -0
- data/.travis.yml +50 -24
- data/CHANGELOG.md +650 -6
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +69 -1
- data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
- data/README.md +195 -545
- data/Rakefile +64 -8
- data/active_model_serializers.gemspec +62 -23
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +43 -38
- data/lib/active_model/serializable_resource.rb +11 -0
- data/lib/active_model/serializer/adapter/attributes.rb +15 -0
- data/lib/active_model/serializer/adapter/base.rb +18 -0
- data/lib/active_model/serializer/adapter/json.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +15 -0
- data/lib/active_model/serializer/adapter/null.rb +15 -0
- data/lib/active_model/serializer/adapter.rb +24 -0
- data/lib/active_model/serializer/array_serializer.rb +12 -0
- data/lib/active_model/serializer/association.rb +71 -0
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +11 -0
- data/lib/active_model/serializer/collection_serializer.rb +88 -0
- data/lib/active_model/serializer/concerns/caching.rb +300 -0
- data/lib/active_model/serializer/error_serializer.rb +14 -0
- data/lib/active_model/serializer/errors_serializer.rb +32 -0
- data/lib/active_model/serializer/field.rb +90 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +7 -0
- data/lib/active_model/serializer/lazy_association.rb +96 -0
- data/lib/active_model/serializer/link.rb +21 -0
- data/lib/active_model/serializer/lint.rb +150 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +210 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +343 -442
- data/lib/active_model_serializers/adapter/attributes.rb +13 -0
- data/lib/active_model_serializers/adapter/base.rb +83 -0
- data/lib/active_model_serializers/adapter/json.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +88 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +104 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +66 -0
- data/lib/active_model_serializers/adapter/json_api.rb +533 -0
- data/lib/active_model_serializers/adapter/null.rb +9 -0
- data/lib/active_model_serializers/adapter.rb +98 -0
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deprecate.rb +54 -0
- data/lib/active_model_serializers/deserialization.rb +15 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/logging.rb +122 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +130 -0
- data/lib/active_model_serializers/railtie.rb +50 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
- data/lib/active_model_serializers/serializable_resource.rb +82 -0
- data/lib/active_model_serializers/serialization_context.rb +39 -0
- data/lib/active_model_serializers/test/schema.rb +138 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +47 -81
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +16 -0
- data/lib/grape/formatters/active_model_serializers.rb +32 -0
- data/lib/grape/helpers/active_model_serializers.rb +17 -0
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +62 -0
- data/test/action_controller/explicit_serializer_test.rb +135 -0
- data/test/action_controller/json/include_test.rb +246 -0
- data/test/action_controller/json_api/deserialization_test.rb +112 -0
- data/test/action_controller/json_api/errors_test.rb +40 -0
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +202 -0
- data/test/action_controller/json_api/pagination_test.rb +124 -0
- data/test/action_controller/json_api/transform_test.rb +189 -0
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +235 -0
- data/test/action_controller/serialization_test.rb +478 -0
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/json_pointer_test.rb +22 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +142 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +68 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
- data/test/active_model_serializers/test/schema_test.rb +131 -0
- data/test/active_model_serializers/test/serializer_test.rb +62 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +45 -0
- data/test/adapter/json/collection_test.rb +104 -0
- data/test/adapter/json/has_many_test.rb +53 -0
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +155 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/errors_test.rb +76 -0
- data/test/adapter/json_api/fields_test.rb +96 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
- data/test/adapter/json_api/has_many_test.rb +173 -0
- data/test/adapter/json_api/has_one_test.rb +80 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +213 -0
- data/test/adapter/json_api/json_api_test.rb +33 -0
- data/test/adapter/json_api/linked_test.rb +413 -0
- data/test/adapter/json_api/links_test.rb +110 -0
- data/test/adapter/json_api/pagination_links_test.rb +206 -0
- data/test/adapter/json_api/parse_test.rb +137 -0
- data/test/adapter/json_api/relationship_test.rb +397 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
- data/test/adapter/json_api/transform_test.rb +512 -0
- data/test/adapter/json_api/type_test.rb +193 -0
- data/test/adapter/json_test.rb +46 -0
- data/test/adapter/null_test.rb +22 -0
- data/test/adapter/polymorphic_test.rb +218 -0
- data/test/adapter_test.rb +67 -0
- data/test/array_serializer_test.rb +20 -73
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +119 -0
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +45 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +83 -0
- data/test/benchmark/fixtures.rb +219 -0
- data/test/cache_test.rb +651 -0
- data/test/collection_serializer_test.rb +127 -0
- data/test/fixtures/active_record.rb +113 -0
- data/test/fixtures/poro.rb +225 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +75 -0
- data/test/grape_test.rb +196 -0
- data/test/lint_test.rb +49 -0
- data/test/logger_test.rb +20 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +79 -0
- data/test/serializers/association_macros_test.rb +37 -0
- data/test/serializers/associations_test.rb +518 -0
- data/test/serializers/attribute_test.rb +153 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +202 -0
- data/test/serializers/options_test.rb +32 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/reflection_test.rb +479 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/serializers/serializer_for_test.rb +136 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +84 -0
- data/test/support/rails5_shims.rb +53 -0
- data/test/support/rails_app.rb +38 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +79 -0
- data/test/test_helper.rb +59 -21
- metadata +529 -43
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class ErrorsTest < ActionController::TestCase
|
7
|
+
def test_active_model_with_multiple_errors
|
8
|
+
get :render_resource_with_errors
|
9
|
+
|
10
|
+
expected_errors_object = {
|
11
|
+
errors: [
|
12
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
13
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
14
|
+
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
15
|
+
]
|
16
|
+
}.to_json
|
17
|
+
assert_equal json_response_body.to_json, expected_errors_object
|
18
|
+
end
|
19
|
+
|
20
|
+
def json_response_body
|
21
|
+
JSON.load(@response.body)
|
22
|
+
end
|
23
|
+
|
24
|
+
class ErrorsTestController < ActionController::Base
|
25
|
+
def render_resource_with_errors
|
26
|
+
resource = Profile.new(name: 'Name 1',
|
27
|
+
description: 'Description 1',
|
28
|
+
comments: 'Comments 1')
|
29
|
+
resource.errors.add(:name, 'cannot be nil')
|
30
|
+
resource.errors.add(:name, 'must be longer')
|
31
|
+
resource.errors.add(:id, 'must be a uuid')
|
32
|
+
render json: resource, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
tests ErrorsTestController
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class FieldsTest < ActionController::TestCase
|
7
|
+
class FieldsTestController < ActionController::Base
|
8
|
+
class AuthorWithName < Author
|
9
|
+
attributes :first_name, :last_name
|
10
|
+
end
|
11
|
+
class AuthorWithNameSerializer < AuthorSerializer
|
12
|
+
type 'authors'
|
13
|
+
end
|
14
|
+
class PostWithPublishAt < Post
|
15
|
+
attributes :publish_at
|
16
|
+
end
|
17
|
+
class PostWithPublishAtSerializer < ActiveModel::Serializer
|
18
|
+
type 'posts'
|
19
|
+
attributes :title, :body, :publish_at
|
20
|
+
belongs_to :author
|
21
|
+
has_many :comments
|
22
|
+
end
|
23
|
+
|
24
|
+
def setup_post
|
25
|
+
ActionController::Base.cache_store.clear
|
26
|
+
@author = AuthorWithName.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
27
|
+
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
|
28
|
+
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
|
29
|
+
@post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
30
|
+
author: @author, comments: [@comment1, @comment2],
|
31
|
+
publish_at: '2020-03-16T03:55:25.291Z')
|
32
|
+
@comment1.post = @post
|
33
|
+
@comment2.post = @post
|
34
|
+
end
|
35
|
+
|
36
|
+
def render_fields_works_on_relationships
|
37
|
+
setup_post
|
38
|
+
render json: @post, serializer: PostWithPublishAtSerializer, adapter: :json_api, fields: { posts: [:author] }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
tests FieldsTestController
|
43
|
+
|
44
|
+
test 'fields works on relationships' do
|
45
|
+
get :render_fields_works_on_relationships
|
46
|
+
response = JSON.parse(@response.body)
|
47
|
+
expected = {
|
48
|
+
'data' => {
|
49
|
+
'id' => '1337',
|
50
|
+
'type' => 'posts',
|
51
|
+
'relationships' => {
|
52
|
+
'author' => {
|
53
|
+
'data' => {
|
54
|
+
'id' => '1',
|
55
|
+
'type' => 'authors'
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
assert_equal expected, response
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class LinkedTest < ActionDispatch::IntegrationTest
|
7
|
+
class LinkedTestController < ActionController::Base
|
8
|
+
def setup_post
|
9
|
+
ActionController::Base.cache_store.clear
|
10
|
+
@role1 = Role.new(id: 1, name: 'admin')
|
11
|
+
@role2 = Role.new(id: 2, name: 'colab')
|
12
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
13
|
+
@author.posts = []
|
14
|
+
@author.bio = nil
|
15
|
+
@author.roles = [@role1, @role2]
|
16
|
+
@role1.author = @author
|
17
|
+
@role2.author = @author
|
18
|
+
@author2 = Author.new(id: 2, name: 'Anonymous')
|
19
|
+
@author2.posts = []
|
20
|
+
@author2.bio = nil
|
21
|
+
@author2.roles = []
|
22
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
23
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
24
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
25
|
+
@post.comments = [@first_comment, @second_comment]
|
26
|
+
@post.author = @author
|
27
|
+
@first_comment.post = @post
|
28
|
+
@first_comment.author = @author2
|
29
|
+
@second_comment.post = @post
|
30
|
+
@second_comment.author = nil
|
31
|
+
@post2 = Post.new(id: 2, title: 'Another Post', body: 'Body')
|
32
|
+
@post2.author = @author
|
33
|
+
@post2.comments = []
|
34
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
35
|
+
@post.blog = @blog
|
36
|
+
@post2.blog = @blog
|
37
|
+
end
|
38
|
+
|
39
|
+
def render_resource_without_include
|
40
|
+
setup_post
|
41
|
+
render json: @post
|
42
|
+
end
|
43
|
+
|
44
|
+
def render_resource_with_include
|
45
|
+
setup_post
|
46
|
+
render json: @post, adapter: :json_api, include: [:author]
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_resource_with_include_of_custom_key_by_original
|
50
|
+
setup_post
|
51
|
+
render json: @post, adapter: :json_api, include: [:reviews], serializer: PostWithCustomKeysSerializer
|
52
|
+
end
|
53
|
+
|
54
|
+
def render_resource_with_nested_include
|
55
|
+
setup_post
|
56
|
+
render json: @post, adapter: :json_api, include: [comments: [:author]]
|
57
|
+
end
|
58
|
+
|
59
|
+
def render_resource_with_nested_has_many_include_wildcard
|
60
|
+
setup_post
|
61
|
+
render json: @post, adapter: :json_api, include: 'author.*'
|
62
|
+
end
|
63
|
+
|
64
|
+
def render_resource_with_missing_nested_has_many_include
|
65
|
+
setup_post
|
66
|
+
@post.author = @author2 # author2 has no roles.
|
67
|
+
render json: @post, adapter: :json_api, include: [author: [:roles]]
|
68
|
+
end
|
69
|
+
|
70
|
+
def render_collection_with_missing_nested_has_many_include
|
71
|
+
setup_post
|
72
|
+
@post.author = @author2
|
73
|
+
render json: [@post, @post2], adapter: :json_api, include: [author: [:roles]]
|
74
|
+
end
|
75
|
+
|
76
|
+
def render_collection_without_include
|
77
|
+
setup_post
|
78
|
+
render json: [@post], adapter: :json_api
|
79
|
+
end
|
80
|
+
|
81
|
+
def render_collection_with_include
|
82
|
+
setup_post
|
83
|
+
render json: [@post], adapter: :json_api, include: 'author, comments'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
setup do
|
88
|
+
@routes = Rails.application.routes.draw do
|
89
|
+
ActiveSupport::Deprecation.silence do
|
90
|
+
match ':action', to: LinkedTestController, via: [:get, :post]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_render_resource_without_include
|
96
|
+
get '/render_resource_without_include'
|
97
|
+
response = JSON.parse(@response.body)
|
98
|
+
refute response.key? 'included'
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_render_resource_with_include
|
102
|
+
get '/render_resource_with_include'
|
103
|
+
response = JSON.parse(@response.body)
|
104
|
+
assert response.key? 'included'
|
105
|
+
assert_equal 1, response['included'].size
|
106
|
+
assert_equal 'Steve K.', response['included'].first['attributes']['name']
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_render_resource_with_nested_has_many_include
|
110
|
+
get '/render_resource_with_nested_has_many_include_wildcard'
|
111
|
+
response = JSON.parse(@response.body)
|
112
|
+
expected_linked = [
|
113
|
+
{
|
114
|
+
'id' => '1',
|
115
|
+
'type' => 'authors',
|
116
|
+
'attributes' => {
|
117
|
+
'name' => 'Steve K.'
|
118
|
+
},
|
119
|
+
'relationships' => {
|
120
|
+
'posts' => { 'data' => [] },
|
121
|
+
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
|
122
|
+
'bio' => { 'data' => nil }
|
123
|
+
}
|
124
|
+
}, {
|
125
|
+
'id' => '1',
|
126
|
+
'type' => 'roles',
|
127
|
+
'attributes' => {
|
128
|
+
'name' => 'admin',
|
129
|
+
'description' => nil,
|
130
|
+
'slug' => 'admin-1'
|
131
|
+
},
|
132
|
+
'relationships' => {
|
133
|
+
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
|
134
|
+
}
|
135
|
+
}, {
|
136
|
+
'id' => '2',
|
137
|
+
'type' => 'roles',
|
138
|
+
'attributes' => {
|
139
|
+
'name' => 'colab',
|
140
|
+
'description' => nil,
|
141
|
+
'slug' => 'colab-2'
|
142
|
+
},
|
143
|
+
'relationships' => {
|
144
|
+
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
|
145
|
+
}
|
146
|
+
}
|
147
|
+
]
|
148
|
+
assert_equal expected_linked, response['included']
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_render_resource_with_include_of_custom_key_by_original
|
152
|
+
get '/render_resource_with_include_of_custom_key_by_original'
|
153
|
+
response = JSON.parse(@response.body)
|
154
|
+
assert response.key? 'included'
|
155
|
+
|
156
|
+
relationships = response['data']['relationships']
|
157
|
+
|
158
|
+
assert_includes relationships, 'reviews'
|
159
|
+
assert_includes relationships, 'writer'
|
160
|
+
assert_includes relationships, 'site'
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_render_resource_with_nested_include
|
164
|
+
get '/render_resource_with_nested_include'
|
165
|
+
response = JSON.parse(@response.body)
|
166
|
+
assert response.key? 'included'
|
167
|
+
assert_equal 3, response['included'].size
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_render_collection_without_include
|
171
|
+
get '/render_collection_without_include'
|
172
|
+
response = JSON.parse(@response.body)
|
173
|
+
refute response.key? 'included'
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_render_collection_with_include
|
177
|
+
get '/render_collection_with_include'
|
178
|
+
response = JSON.parse(@response.body)
|
179
|
+
assert response.key? 'included'
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
183
|
+
get '/render_resource_with_missing_nested_has_many_include'
|
184
|
+
response = JSON.parse(@response.body)
|
185
|
+
assert response.key? 'included'
|
186
|
+
refute include_type?(response['included'], 'roles')
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_render_collection_with_missing_nested_has_many_include
|
190
|
+
get '/render_collection_with_missing_nested_has_many_include'
|
191
|
+
response = JSON.parse(@response.body)
|
192
|
+
assert response.key? 'included'
|
193
|
+
assert include_type?(response['included'], 'roles')
|
194
|
+
end
|
195
|
+
|
196
|
+
def include_type?(collection, value)
|
197
|
+
collection.detect { |i| i['type'] == value }
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'will_paginate/array'
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/hooks'
|
5
|
+
::Kaminari::Hooks.init
|
6
|
+
|
7
|
+
module ActionController
|
8
|
+
module Serialization
|
9
|
+
class JsonApi
|
10
|
+
class PaginationTest < ActionController::TestCase
|
11
|
+
KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'.freeze
|
12
|
+
WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'.freeze
|
13
|
+
|
14
|
+
class PaginationTestController < ActionController::Base
|
15
|
+
def setup
|
16
|
+
@array = [
|
17
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
18
|
+
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
19
|
+
Profile.new(name: 'Name 3', description: 'Description 3', comments: 'Comments 3')
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
def using_kaminari
|
24
|
+
setup
|
25
|
+
Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
|
26
|
+
end
|
27
|
+
|
28
|
+
def using_will_paginate
|
29
|
+
setup
|
30
|
+
@array.paginate(page: params[:page][:number], per_page: params[:page][:size])
|
31
|
+
end
|
32
|
+
|
33
|
+
def render_pagination_using_kaminari
|
34
|
+
render json: using_kaminari, adapter: :json_api
|
35
|
+
end
|
36
|
+
|
37
|
+
def render_pagination_using_will_paginate
|
38
|
+
render json: using_will_paginate, adapter: :json_api
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_array_without_pagination_links
|
42
|
+
setup
|
43
|
+
render json: @array, adapter: :json_api
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
tests PaginationTestController
|
48
|
+
|
49
|
+
def test_render_pagination_links_with_will_paginate
|
50
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
51
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
52
|
+
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
53
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
54
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
55
|
+
|
56
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
|
57
|
+
response = JSON.parse(@response.body)
|
58
|
+
assert_equal expected_links, response['links']
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_render_only_first_last_and_next_pagination_links
|
62
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
63
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
64
|
+
'prev' => nil,
|
65
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
66
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
67
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
|
68
|
+
response = JSON.parse(@response.body)
|
69
|
+
assert_equal expected_links, response['links']
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_render_pagination_links_with_kaminari
|
73
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
74
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
75
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
76
|
+
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
77
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
78
|
+
get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
|
79
|
+
response = JSON.parse(@response.body)
|
80
|
+
assert_equal expected_links, response['links']
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_render_only_prev_first_and_last_pagination_links
|
84
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
85
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
86
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
87
|
+
'next' => nil,
|
88
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
89
|
+
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
|
90
|
+
response = JSON.parse(@response.body)
|
91
|
+
assert_equal expected_links, response['links']
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_render_only_first_last_and_next_pagination_links_with_additional_params
|
95
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
96
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
97
|
+
'prev' => nil,
|
98
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
99
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
100
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
|
101
|
+
response = JSON.parse(@response.body)
|
102
|
+
assert_equal expected_links, response['links']
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_render_only_prev_first_and_last_pagination_links_with_additional_params
|
106
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
107
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
108
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional",
|
109
|
+
'next' => nil,
|
110
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional" }
|
111
|
+
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
|
112
|
+
response = JSON.parse(@response.body)
|
113
|
+
assert_equal expected_links, response['links']
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_array_without_pagination_links
|
117
|
+
get :render_array_without_pagination_links, params: { page: { number: 2, size: 1 } }
|
118
|
+
response = JSON.parse(@response.body)
|
119
|
+
refute response.key? 'links'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,189 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class KeyTransformTest < ActionController::TestCase
|
7
|
+
class KeyTransformTestController < ActionController::Base
|
8
|
+
class Post < ::Model
|
9
|
+
attributes :title, :body, :publish_at
|
10
|
+
associations :author, :top_comments
|
11
|
+
end
|
12
|
+
class Author < ::Model
|
13
|
+
attributes :first_name, :last_name
|
14
|
+
end
|
15
|
+
class TopComment < ::Model
|
16
|
+
attributes :body
|
17
|
+
associations :author, :post
|
18
|
+
end
|
19
|
+
class PostSerializer < ActiveModel::Serializer
|
20
|
+
type 'posts'
|
21
|
+
attributes :title, :body, :publish_at
|
22
|
+
belongs_to :author
|
23
|
+
has_many :top_comments
|
24
|
+
|
25
|
+
link(:post_authors) { 'https://example.com/post_authors' }
|
26
|
+
|
27
|
+
meta do
|
28
|
+
{
|
29
|
+
rating: 5,
|
30
|
+
favorite_count: 10
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class AuthorSerializer < ActiveModel::Serializer
|
36
|
+
type 'authors'
|
37
|
+
attributes :first_name, :last_name
|
38
|
+
end
|
39
|
+
|
40
|
+
class TopCommentSerializer < ActiveModel::Serializer
|
41
|
+
type 'top_comments'
|
42
|
+
attributes :body
|
43
|
+
belongs_to :author
|
44
|
+
end
|
45
|
+
|
46
|
+
def setup_post
|
47
|
+
ActionController::Base.cache_store.clear
|
48
|
+
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
49
|
+
@comment1 = TopComment.new(id: 7, body: 'cool', author: @author)
|
50
|
+
@comment2 = TopComment.new(id: 12, body: 'awesome', author: @author)
|
51
|
+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
52
|
+
author: @author, top_comments: [@comment1, @comment2],
|
53
|
+
publish_at: '2020-03-16T03:55:25.291Z')
|
54
|
+
@comment1.post = @post
|
55
|
+
@comment2.post = @post
|
56
|
+
end
|
57
|
+
|
58
|
+
def render_resource_with_transform
|
59
|
+
setup_post
|
60
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
61
|
+
key_transform: :camel
|
62
|
+
end
|
63
|
+
|
64
|
+
def render_resource_with_transform_nil
|
65
|
+
setup_post
|
66
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
67
|
+
key_transform: nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def render_resource_with_transform_with_global_config
|
71
|
+
old_transform = ActiveModelSerializers.config.key_transform
|
72
|
+
setup_post
|
73
|
+
ActiveModelSerializers.config.key_transform = :camel_lower
|
74
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api
|
75
|
+
ensure
|
76
|
+
ActiveModelSerializers.config.key_transform = old_transform
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
tests KeyTransformTestController
|
81
|
+
|
82
|
+
def test_render_resource_with_transform
|
83
|
+
get :render_resource_with_transform
|
84
|
+
response = JSON.parse(@response.body)
|
85
|
+
expected = {
|
86
|
+
'Data' => {
|
87
|
+
'Id' => '1337',
|
88
|
+
'Type' => 'Posts',
|
89
|
+
'Attributes' => {
|
90
|
+
'Title' => 'Title 1',
|
91
|
+
'Body' => 'Body 1',
|
92
|
+
'PublishAt' => '2020-03-16T03:55:25.291Z'
|
93
|
+
},
|
94
|
+
'Relationships' => {
|
95
|
+
'Author' => {
|
96
|
+
'Data' => {
|
97
|
+
'Id' => '1',
|
98
|
+
'Type' => 'Authors'
|
99
|
+
}
|
100
|
+
},
|
101
|
+
'TopComments' => {
|
102
|
+
'Data' => [
|
103
|
+
{ 'Id' => '7', 'Type' => 'TopComments' },
|
104
|
+
{ 'Id' => '12', 'Type' => 'TopComments' }
|
105
|
+
]
|
106
|
+
}
|
107
|
+
},
|
108
|
+
'Links' => {
|
109
|
+
'PostAuthors' => 'https://example.com/post_authors'
|
110
|
+
},
|
111
|
+
'Meta' => { 'Rating' => 5, 'FavoriteCount' => 10 }
|
112
|
+
}
|
113
|
+
}
|
114
|
+
assert_equal expected, response
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_render_resource_with_transform_nil
|
118
|
+
get :render_resource_with_transform_nil
|
119
|
+
response = JSON.parse(@response.body)
|
120
|
+
expected = {
|
121
|
+
'data' => {
|
122
|
+
'id' => '1337',
|
123
|
+
'type' => 'posts',
|
124
|
+
'attributes' => {
|
125
|
+
'title' => 'Title 1',
|
126
|
+
'body' => 'Body 1',
|
127
|
+
'publish-at' => '2020-03-16T03:55:25.291Z'
|
128
|
+
},
|
129
|
+
'relationships' => {
|
130
|
+
'author' => {
|
131
|
+
'data' => {
|
132
|
+
'id' => '1',
|
133
|
+
'type' => 'authors'
|
134
|
+
}
|
135
|
+
},
|
136
|
+
'top-comments' => {
|
137
|
+
'data' => [
|
138
|
+
{ 'id' => '7', 'type' => 'top-comments' },
|
139
|
+
{ 'id' => '12', 'type' => 'top-comments' }
|
140
|
+
]
|
141
|
+
}
|
142
|
+
},
|
143
|
+
'links' => {
|
144
|
+
'post-authors' => 'https://example.com/post_authors'
|
145
|
+
},
|
146
|
+
'meta' => { 'rating' => 5, 'favorite-count' => 10 }
|
147
|
+
}
|
148
|
+
}
|
149
|
+
assert_equal expected, response
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_render_resource_with_transform_with_global_config
|
153
|
+
get :render_resource_with_transform_with_global_config
|
154
|
+
response = JSON.parse(@response.body)
|
155
|
+
expected = {
|
156
|
+
'data' => {
|
157
|
+
'id' => '1337',
|
158
|
+
'type' => 'posts',
|
159
|
+
'attributes' => {
|
160
|
+
'title' => 'Title 1',
|
161
|
+
'body' => 'Body 1',
|
162
|
+
'publishAt' => '2020-03-16T03:55:25.291Z'
|
163
|
+
},
|
164
|
+
'relationships' => {
|
165
|
+
'author' => {
|
166
|
+
'data' => {
|
167
|
+
'id' => '1',
|
168
|
+
'type' => 'authors'
|
169
|
+
}
|
170
|
+
},
|
171
|
+
'topComments' => {
|
172
|
+
'data' => [
|
173
|
+
{ 'id' => '7', 'type' => 'topComments' },
|
174
|
+
{ 'id' => '12', 'type' => 'topComments' }
|
175
|
+
]
|
176
|
+
}
|
177
|
+
},
|
178
|
+
'links' => {
|
179
|
+
'postAuthors' => 'https://example.com/post_authors'
|
180
|
+
},
|
181
|
+
'meta' => { 'rating' => 5, 'favoriteCount' => 10 }
|
182
|
+
}
|
183
|
+
}
|
184
|
+
assert_equal expected, response
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class LookupProcTest < ActionController::TestCase
|
6
|
+
module Api
|
7
|
+
module V3
|
8
|
+
class PostCustomSerializer < ActiveModel::Serializer
|
9
|
+
attributes :title, :body
|
10
|
+
|
11
|
+
belongs_to :author
|
12
|
+
end
|
13
|
+
|
14
|
+
class AuthorCustomSerializer < ActiveModel::Serializer
|
15
|
+
attributes :name
|
16
|
+
end
|
17
|
+
|
18
|
+
class LookupProcTestController < ActionController::Base
|
19
|
+
def implicit_namespaced_serializer
|
20
|
+
author = Author.new(name: 'Bob')
|
21
|
+
post = Post.new(title: 'New Post', body: 'Body', author: author)
|
22
|
+
|
23
|
+
render json: post
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
tests Api::V3::LookupProcTestController
|
30
|
+
|
31
|
+
test 'implicitly uses namespaced serializer' do
|
32
|
+
controller_namespace = lambda do |resource_class, _parent_serializer_class, namespace|
|
33
|
+
"#{namespace}::#{resource_class}CustomSerializer" if namespace
|
34
|
+
end
|
35
|
+
|
36
|
+
with_prepended_lookup(controller_namespace) do
|
37
|
+
get :implicit_namespaced_serializer
|
38
|
+
|
39
|
+
assert_serializer Api::V3::PostCustomSerializer
|
40
|
+
|
41
|
+
expected = { 'title' => 'New Post', 'body' => 'Body', 'author' => { 'name' => 'Bob' } }
|
42
|
+
actual = JSON.parse(@response.body)
|
43
|
+
|
44
|
+
assert_equal expected, actual
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|