active_model_serializers 0.10.0 → 0.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +9 -1
- data/CHANGELOG.md +81 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +24 -24
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +20 -24
- data/docs/ARCHITECTURE.md +6 -7
- data/docs/README.md +2 -0
- data/docs/general/adapters.md +4 -2
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/rendering.md +42 -3
- data/docs/general/serializers.md +97 -8
- data/docs/howto/add_pagination_links.md +4 -5
- data/docs/howto/add_relationship_links.md +137 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +9 -2
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +64 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +76 -37
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +38 -38
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/adapter/json_api.rb +44 -43
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/key_transform.rb +4 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +4 -2
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers.rb +7 -0
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/test/action_controller/adapter_selector_test.rb +4 -4
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +6 -7
- data/test/action_controller/json_api/fields_test.rb +57 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +3 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +226 -0
- data/test/action_controller/serialization_test.rb +10 -7
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/key_transform_test.rb +286 -252
- data/test/active_model_serializers/model_test.rb +17 -4
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +4 -3
- data/test/adapter/json_api/has_many_test.rb +39 -18
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +255 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +16 -5
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +143 -49
- data/test/collection_serializer_test.rb +3 -3
- data/test/fixtures/poro.rb +52 -48
- data/test/generators/serializer_generator_test.rb +22 -5
- data/test/grape_test.rb +152 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +107 -32
- data/test/serializers/attribute_test.rb +2 -2
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +6 -4
- data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +0 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +1 -0
- metadata +85 -34
- data/.rubocop_todo.yml +0 -167
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'support/isolated_unit'
|
2
|
+
require 'minitest/mock'
|
3
|
+
require 'action_dispatch'
|
4
|
+
require 'action_controller'
|
5
|
+
|
6
|
+
class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
7
|
+
include ActiveSupport::Testing::Isolation
|
8
|
+
|
9
|
+
class TestController < ActionController::Base
|
10
|
+
class << self
|
11
|
+
attr_accessor :last_request_parameters
|
12
|
+
end
|
13
|
+
|
14
|
+
def render_with_jsonapi_renderer
|
15
|
+
author = Author.new(params[:data][:attributes])
|
16
|
+
render jsonapi: author
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse
|
20
|
+
self.class.last_request_parameters = request.request_parameters
|
21
|
+
head :ok
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown
|
26
|
+
TestController.last_request_parameters = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def assert_parses(expected, actual, headers = {})
|
30
|
+
post '/parse', params: actual, headers: headers
|
31
|
+
assert_response :ok
|
32
|
+
assert_equal(expected, TestController.last_request_parameters)
|
33
|
+
end
|
34
|
+
|
35
|
+
class WithoutRenderer < JsonApiRendererTest
|
36
|
+
setup do
|
37
|
+
require 'rails'
|
38
|
+
require 'active_record'
|
39
|
+
require 'support/rails5_shims'
|
40
|
+
require 'active_model_serializers'
|
41
|
+
require 'fixtures/poro'
|
42
|
+
|
43
|
+
make_basic_app
|
44
|
+
|
45
|
+
Rails.application.routes.draw do
|
46
|
+
ActiveSupport::Deprecation.silence do
|
47
|
+
match ':action', to: TestController, via: [:get, :post]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_jsonapi_parser_not_registered
|
53
|
+
parsers = if Rails::VERSION::MAJOR >= 5
|
54
|
+
ActionDispatch::Request.parameter_parsers
|
55
|
+
else
|
56
|
+
ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
57
|
+
end
|
58
|
+
assert_nil parsers[Mime[:jsonapi]]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_jsonapi_renderer_not_registered
|
62
|
+
expected = {
|
63
|
+
'data' => {
|
64
|
+
'attributes' => {
|
65
|
+
'name' => 'Johnny Rico'
|
66
|
+
},
|
67
|
+
'type' => 'users'
|
68
|
+
}
|
69
|
+
}
|
70
|
+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
71
|
+
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
72
|
+
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
73
|
+
assert expected, response.body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_jsonapi_parser
|
77
|
+
assert_parses(
|
78
|
+
{},
|
79
|
+
'',
|
80
|
+
'CONTENT_TYPE' => 'application/vnd.api+json'
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class WithRenderer < JsonApiRendererTest
|
86
|
+
setup do
|
87
|
+
require 'rails'
|
88
|
+
require 'active_record'
|
89
|
+
require 'support/rails5_shims'
|
90
|
+
require 'active_model_serializers'
|
91
|
+
require 'fixtures/poro'
|
92
|
+
require 'active_model_serializers/register_jsonapi_renderer'
|
93
|
+
|
94
|
+
make_basic_app
|
95
|
+
|
96
|
+
Rails.application.routes.draw do
|
97
|
+
ActiveSupport::Deprecation.silence do
|
98
|
+
match ':action', to: TestController, via: [:get, :post]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_jsonapi_parser_registered
|
104
|
+
if Rails::VERSION::MAJOR >= 5
|
105
|
+
parsers = ActionDispatch::Request.parameter_parsers
|
106
|
+
assert_equal Proc, parsers[:jsonapi].class
|
107
|
+
else
|
108
|
+
parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
109
|
+
assert_equal Proc, parsers[Mime[:jsonapi]].class
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_jsonapi_renderer_registered
|
114
|
+
expected = {
|
115
|
+
'data' => {
|
116
|
+
'attributes' => {
|
117
|
+
'name' => 'Johnny Rico'
|
118
|
+
},
|
119
|
+
'type' => 'users'
|
120
|
+
}
|
121
|
+
}
|
122
|
+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
123
|
+
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
124
|
+
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
125
|
+
assert expected, response.body
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_jsonapi_parser
|
129
|
+
assert_parses(
|
130
|
+
{
|
131
|
+
'data' => {
|
132
|
+
'attributes' => {
|
133
|
+
'name' => 'John Doe'
|
134
|
+
},
|
135
|
+
'type' => 'users'
|
136
|
+
}
|
137
|
+
},
|
138
|
+
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
|
139
|
+
'CONTENT_TYPE' => 'application/vnd.api+json'
|
140
|
+
)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -5,13 +5,19 @@ require 'minitest/mock'
|
|
5
5
|
class SerializationContextTest < ActiveSupport::TestCase
|
6
6
|
include ActiveSupport::Testing::Isolation
|
7
7
|
|
8
|
-
def create_request
|
9
|
-
request = Minitest::Mock.new
|
10
|
-
request.expect(:original_url, 'original_url')
|
11
|
-
request.expect(:query_parameters, 'query_parameters')
|
12
|
-
end
|
13
|
-
|
14
8
|
class WithRails < SerializationContextTest
|
9
|
+
def create_request
|
10
|
+
request = ActionDispatch::Request.new({})
|
11
|
+
def request.original_url
|
12
|
+
'http://example.com/articles?page=2'
|
13
|
+
end
|
14
|
+
|
15
|
+
def request.query_parameters
|
16
|
+
{ 'page' => 2 }
|
17
|
+
end
|
18
|
+
request
|
19
|
+
end
|
20
|
+
|
15
21
|
setup do
|
16
22
|
require 'rails'
|
17
23
|
require 'active_model_serializers'
|
@@ -20,8 +26,8 @@ class SerializationContextTest < ActiveSupport::TestCase
|
|
20
26
|
end
|
21
27
|
|
22
28
|
test 'create context with request url and query parameters' do
|
23
|
-
assert_equal @context.request_url, '
|
24
|
-
assert_equal @context.query_parameters, '
|
29
|
+
assert_equal @context.request_url, 'http://example.com/articles'
|
30
|
+
assert_equal @context.query_parameters, 'page' => 2
|
25
31
|
end
|
26
32
|
|
27
33
|
test 'url_helpers is set up for Rails url_helpers' do
|
@@ -36,14 +42,21 @@ class SerializationContextTest < ActiveSupport::TestCase
|
|
36
42
|
end
|
37
43
|
|
38
44
|
class WithoutRails < SerializationContextTest
|
45
|
+
def create_request
|
46
|
+
{
|
47
|
+
request_url: 'http://example.com/articles',
|
48
|
+
query_parameters: { 'page' => 2 }
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
39
52
|
setup do
|
40
53
|
require 'active_model_serializers/serialization_context'
|
41
54
|
@context = ActiveModelSerializers::SerializationContext.new(create_request)
|
42
55
|
end
|
43
56
|
|
44
57
|
test 'create context with request url and query parameters' do
|
45
|
-
assert_equal @context.request_url, '
|
46
|
-
assert_equal @context.query_parameters, '
|
58
|
+
assert_equal @context.request_url, 'http://example.com/articles'
|
59
|
+
assert_equal @context.query_parameters, 'page' => 2
|
47
60
|
end
|
48
61
|
|
49
62
|
test 'url_helpers is a module when Rails is not present' do
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class AttributesTest < ActiveSupport::TestCase
|
6
|
+
class Person
|
7
|
+
include ActiveModel::Model
|
8
|
+
include ActiveModel::Serialization
|
9
|
+
|
10
|
+
attr_accessor :first_name, :last_name
|
11
|
+
end
|
12
|
+
|
13
|
+
class PersonSerializer < ActiveModel::Serializer
|
14
|
+
attributes :first_name, :last_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup
|
18
|
+
ActionController::Base.cache_store.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_serializable_hash
|
22
|
+
person = Person.new(first_name: 'Arthur', last_name: 'Dent')
|
23
|
+
serializer = PersonSerializer.new(person)
|
24
|
+
adapter = ActiveModelSerializers::Adapter::Attributes.new(serializer)
|
25
|
+
|
26
|
+
assert_equal({ first_name: 'Arthur', last_name: 'Dent' },
|
27
|
+
adapter.serializable_hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_serializable_hash_with_transform_key_casing
|
31
|
+
person = Person.new(first_name: 'Arthur', last_name: 'Dent')
|
32
|
+
serializer = PersonSerializer.new(person)
|
33
|
+
adapter = ActiveModelSerializers::Adapter::Attributes.new(
|
34
|
+
serializer,
|
35
|
+
key_transform: :camel_lower
|
36
|
+
)
|
37
|
+
|
38
|
+
assert_equal({ firstName: 'Arthur', lastName: 'Dent' },
|
39
|
+
adapter.serializable_hash)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -84,6 +84,20 @@ module ActiveModelSerializers
|
|
84
84
|
|
85
85
|
assert_equal(expected, actual)
|
86
86
|
end
|
87
|
+
|
88
|
+
def test_fields_with_no_associations_include_option
|
89
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
90
|
+
[@first_post, @second_post], adapter: :json, fields: [:id], include: []
|
91
|
+
).as_json
|
92
|
+
|
93
|
+
expected = { posts: [{
|
94
|
+
id: 1
|
95
|
+
}, {
|
96
|
+
id: 2
|
97
|
+
}] }
|
98
|
+
|
99
|
+
assert_equal(expected, actual)
|
100
|
+
end
|
87
101
|
end
|
88
102
|
end
|
89
103
|
end
|
@@ -15,7 +15,7 @@ module ActiveModelSerializers
|
|
15
15
|
@adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options)
|
16
16
|
end
|
17
17
|
|
18
|
-
Post
|
18
|
+
class Post < ::Model; end
|
19
19
|
class PostSerializer < ActiveModel::Serializer
|
20
20
|
attributes :id, :title, :body, :publish_at
|
21
21
|
end
|
@@ -28,8 +28,8 @@ module ActiveModelSerializers
|
|
28
28
|
def test_transform_default
|
29
29
|
mock_request
|
30
30
|
assert_equal({
|
31
|
-
|
32
|
-
|
31
|
+
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
32
|
+
}, @adapter.serializable_hash)
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_transform_global_config
|
@@ -38,8 +38,8 @@ module ActiveModelSerializers
|
|
38
38
|
@adapter.serializable_hash
|
39
39
|
end
|
40
40
|
assert_equal({
|
41
|
-
|
42
|
-
|
41
|
+
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
42
|
+
}, result)
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_transform_serialization_ctx_overrides_global_config
|
@@ -48,8 +48,8 @@ module ActiveModelSerializers
|
|
48
48
|
@adapter.serializable_hash
|
49
49
|
end
|
50
50
|
assert_equal({
|
51
|
-
|
52
|
-
|
51
|
+
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
52
|
+
}, result)
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_transform_undefined
|
@@ -63,29 +63,29 @@ module ActiveModelSerializers
|
|
63
63
|
def test_transform_dash
|
64
64
|
mock_request(:dash)
|
65
65
|
assert_equal({
|
66
|
-
|
67
|
-
|
66
|
+
blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
|
67
|
+
}, @adapter.serializable_hash)
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_transform_unaltered
|
71
71
|
mock_request(:unaltered)
|
72
72
|
assert_equal({
|
73
|
-
|
74
|
-
|
73
|
+
blog: { id: 1, special_attribute: 'neat', articles: nil }
|
74
|
+
}, @adapter.serializable_hash)
|
75
75
|
end
|
76
76
|
|
77
77
|
def test_transform_camel
|
78
78
|
mock_request(:camel)
|
79
79
|
assert_equal({
|
80
|
-
|
81
|
-
|
80
|
+
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
|
81
|
+
}, @adapter.serializable_hash)
|
82
82
|
end
|
83
83
|
|
84
84
|
def test_transform_camel_lower
|
85
85
|
mock_request(:camel_lower)
|
86
86
|
assert_equal({
|
87
|
-
|
88
|
-
|
87
|
+
blog: { id: 1, specialAttribute: 'neat', articles: nil }
|
88
|
+
}, @adapter.serializable_hash)
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
@@ -58,9 +58,10 @@ module ActiveModelSerializers
|
|
58
58
|
|
59
59
|
def test_limiting_fields
|
60
60
|
actual = ActiveModelSerializers::SerializableResource.new(
|
61
|
-
[@first_post, @second_post],
|
62
|
-
|
63
|
-
|
61
|
+
[@first_post, @second_post],
|
62
|
+
adapter: :json_api,
|
63
|
+
fields: { posts: %w(title comments blog author) }
|
64
|
+
).serializable_hash
|
64
65
|
expected = [
|
65
66
|
{
|
66
67
|
id: '1',
|
@@ -12,8 +12,8 @@ module ActiveModelSerializers
|
|
12
12
|
|
13
13
|
def test_active_model_with_error
|
14
14
|
options = {
|
15
|
-
|
16
|
-
|
15
|
+
serializer: ActiveModel::Serializer::ErrorSerializer,
|
16
|
+
adapter: :json_api
|
17
17
|
}
|
18
18
|
|
19
19
|
@resource.errors.add(:name, 'cannot be nil')
|
@@ -22,22 +22,21 @@ module ActiveModelSerializers
|
|
22
22
|
assert_equal serializable_resource.serializer_instance.attributes, {}
|
23
23
|
assert_equal serializable_resource.serializer_instance.object, @resource
|
24
24
|
|
25
|
-
expected_errors_object =
|
26
|
-
|
27
|
-
|
28
|
-
{
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
]
|
25
|
+
expected_errors_object = {
|
26
|
+
errors: [
|
27
|
+
{
|
28
|
+
source: { pointer: '/data/attributes/name' },
|
29
|
+
detail: 'cannot be nil'
|
30
|
+
}
|
31
|
+
]
|
33
32
|
}
|
34
33
|
assert_equal serializable_resource.as_json, expected_errors_object
|
35
34
|
end
|
36
35
|
|
37
36
|
def test_active_model_with_multiple_errors
|
38
37
|
options = {
|
39
|
-
|
40
|
-
|
38
|
+
serializer: ActiveModel::Serializer::ErrorSerializer,
|
39
|
+
adapter: :json_api
|
41
40
|
}
|
42
41
|
|
43
42
|
@resource.errors.add(:name, 'cannot be nil')
|
@@ -48,13 +47,12 @@ module ActiveModelSerializers
|
|
48
47
|
assert_equal serializable_resource.serializer_instance.attributes, {}
|
49
48
|
assert_equal serializable_resource.serializer_instance.object, @resource
|
50
49
|
|
51
|
-
expected_errors_object =
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
]
|
50
|
+
expected_errors_object = {
|
51
|
+
errors: [
|
52
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
53
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
54
|
+
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
55
|
+
]
|
58
56
|
}
|
59
57
|
assert_equal serializable_resource.as_json, expected_errors_object
|
60
58
|
end
|
@@ -4,7 +4,10 @@ module ActiveModelSerializers
|
|
4
4
|
module Adapter
|
5
5
|
class JsonApi
|
6
6
|
class FieldsTest < ActiveSupport::TestCase
|
7
|
-
Post
|
7
|
+
class Post < ::Model; end
|
8
|
+
class Author < ::Model; end
|
9
|
+
class Comment < ::Model; end
|
10
|
+
|
8
11
|
class PostSerializer < ActiveModel::Serializer
|
9
12
|
type 'posts'
|
10
13
|
attributes :title, :body
|
@@ -12,13 +15,11 @@ module ActiveModelSerializers
|
|
12
15
|
has_many :comments
|
13
16
|
end
|
14
17
|
|
15
|
-
Author = Class.new(::Model)
|
16
18
|
class AuthorSerializer < ActiveModel::Serializer
|
17
19
|
type 'authors'
|
18
20
|
attributes :name, :birthday
|
19
21
|
end
|
20
22
|
|
21
|
-
Comment = Class.new(::Model)
|
22
23
|
class CommentSerializer < ActiveModel::Serializer
|
23
24
|
type 'comments'
|
24
25
|
attributes :body
|
@@ -40,6 +40,27 @@ module ActiveModelSerializers
|
|
40
40
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
41
41
|
end
|
42
42
|
|
43
|
+
test 'relationships can be whitelisted via fields' do
|
44
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, fields: { posts: [:author] })
|
45
|
+
result = @adapter.serializable_hash
|
46
|
+
expected = {
|
47
|
+
data: {
|
48
|
+
id: '1',
|
49
|
+
type: 'posts',
|
50
|
+
relationships: {
|
51
|
+
author: {
|
52
|
+
data: {
|
53
|
+
id: '1',
|
54
|
+
type: 'authors'
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
assert_equal expected, result
|
62
|
+
end
|
63
|
+
|
43
64
|
def test_includes_linked_comments
|
44
65
|
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
|
45
66
|
expected = [{
|
@@ -112,14 +133,14 @@ module ActiveModelSerializers
|
|
112
133
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
113
134
|
|
114
135
|
assert_equal({
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
136
|
+
data: {
|
137
|
+
id: '1',
|
138
|
+
type: 'posts',
|
139
|
+
relationships: {
|
140
|
+
tags: { data: [@tag.as_json] }
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}, adapter.serializable_hash)
|
123
144
|
end
|
124
145
|
|
125
146
|
def test_has_many_with_virtual_value
|
@@ -127,16 +148,16 @@ module ActiveModelSerializers
|
|
127
148
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
128
149
|
|
129
150
|
assert_equal({
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
151
|
+
data: {
|
152
|
+
id: '1',
|
153
|
+
type: 'virtual-values',
|
154
|
+
relationships: {
|
155
|
+
maker: { data: { type: 'makers', id: '1' } },
|
156
|
+
reviews: { data: [{ type: 'reviews', id: '1' },
|
157
|
+
{ type: 'reviews', id: '2' }] }
|
158
|
+
}
|
159
|
+
}
|
160
|
+
}, adapter.serializable_hash)
|
140
161
|
end
|
141
162
|
end
|
142
163
|
end
|