active_model_serializers 0.10.0 → 0.10.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +17 -5
- data/CHANGELOG.md +112 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +21 -24
- data/appveyor.yml +9 -3
- data/docs/README.md +2 -1
- 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/logging.md +7 -0
- data/docs/general/rendering.md +44 -20
- data/docs/general/serializers.md +100 -11
- data/docs/howto/add_pagination_links.md +15 -16
- data/docs/howto/add_relationship_links.md +140 -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 +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- 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} +1 -1
- 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 +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- 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/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +108 -28
- 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/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +15 -0
- data/lib/generators/rails/resource_override.rb +1 -1
- 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 +14 -5
- 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 +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -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 +11 -3
- 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 +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- 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 +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -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 +263 -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 +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +25 -12
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -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 +132 -36
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +104 -38
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- 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,161 @@
|
|
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
|
+
permitted_params = params.permit(data: [:id, :type, attributes: [:name]])
|
16
|
+
permitted_params = permitted_params.to_h.with_indifferent_access
|
17
|
+
attributes =
|
18
|
+
if permitted_params[:data]
|
19
|
+
permitted_params[:data][:attributes].merge(id: permitted_params[:data][:id])
|
20
|
+
else
|
21
|
+
# Rails returns empty params when no mime type can be negotiated.
|
22
|
+
# (Until https://github.com/rails/rails/pull/26632 is reviewed.)
|
23
|
+
permitted_params
|
24
|
+
end
|
25
|
+
author = Author.new(attributes)
|
26
|
+
render jsonapi: author
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse
|
30
|
+
self.class.last_request_parameters = request.request_parameters
|
31
|
+
head :ok
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def teardown
|
36
|
+
TestController.last_request_parameters = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
def assert_parses(expected, actual, headers = {})
|
40
|
+
post '/parse', params: actual, headers: headers
|
41
|
+
assert_response :ok
|
42
|
+
assert_equal(expected, TestController.last_request_parameters)
|
43
|
+
end
|
44
|
+
|
45
|
+
def define_author_model_and_serializer
|
46
|
+
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
|
47
|
+
attributes :id, :name
|
48
|
+
end)
|
49
|
+
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
|
50
|
+
type 'users'
|
51
|
+
attribute :id
|
52
|
+
attribute :name
|
53
|
+
end)
|
54
|
+
end
|
55
|
+
|
56
|
+
class WithoutRenderer < JsonApiRendererTest
|
57
|
+
setup do
|
58
|
+
require 'rails'
|
59
|
+
require 'active_record'
|
60
|
+
require 'support/rails5_shims'
|
61
|
+
require 'active_model_serializers'
|
62
|
+
require 'fixtures/poro'
|
63
|
+
|
64
|
+
make_basic_app
|
65
|
+
|
66
|
+
Rails.application.routes.draw do
|
67
|
+
ActiveSupport::Deprecation.silence do
|
68
|
+
match ':action', to: TestController, via: [:get, :post]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
define_author_model_and_serializer
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_jsonapi_parser_not_registered
|
75
|
+
parsers = if Rails::VERSION::MAJOR >= 5
|
76
|
+
ActionDispatch::Request.parameter_parsers
|
77
|
+
else
|
78
|
+
ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
79
|
+
end
|
80
|
+
assert_nil parsers[Mime[:jsonapi]]
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_jsonapi_renderer_not_registered
|
84
|
+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
85
|
+
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
86
|
+
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
87
|
+
assert_equal '', response.body
|
88
|
+
assert_equal 500, response.status
|
89
|
+
assert_equal ActionView::MissingTemplate, request.env['action_dispatch.exception'].class
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_jsonapi_parser
|
93
|
+
assert_parses(
|
94
|
+
{},
|
95
|
+
'',
|
96
|
+
'CONTENT_TYPE' => 'application/vnd.api+json'
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class WithRenderer < JsonApiRendererTest
|
102
|
+
setup do
|
103
|
+
require 'rails'
|
104
|
+
require 'active_record'
|
105
|
+
require 'support/rails5_shims'
|
106
|
+
require 'active_model_serializers'
|
107
|
+
require 'fixtures/poro'
|
108
|
+
require 'active_model_serializers/register_jsonapi_renderer'
|
109
|
+
|
110
|
+
make_basic_app
|
111
|
+
|
112
|
+
Rails.application.routes.draw do
|
113
|
+
ActiveSupport::Deprecation.silence do
|
114
|
+
match ':action', to: TestController, via: [:get, :post]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
define_author_model_and_serializer
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_jsonapi_parser_registered
|
121
|
+
if Rails::VERSION::MAJOR >= 5
|
122
|
+
parsers = ActionDispatch::Request.parameter_parsers
|
123
|
+
assert_equal Proc, parsers[:jsonapi].class
|
124
|
+
else
|
125
|
+
parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
126
|
+
assert_equal Proc, parsers[Mime[:jsonapi]].class
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_jsonapi_renderer_registered
|
131
|
+
expected = {
|
132
|
+
'data' => {
|
133
|
+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
|
134
|
+
'type' => 'users',
|
135
|
+
'attributes' => { 'name' => 'Johnny Rico' }
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
140
|
+
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
141
|
+
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
142
|
+
assert_equal expected.to_json, response.body
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_jsonapi_parser
|
146
|
+
assert_parses(
|
147
|
+
{
|
148
|
+
'data' => {
|
149
|
+
'attributes' => {
|
150
|
+
'name' => 'John Doe'
|
151
|
+
},
|
152
|
+
'type' => 'users',
|
153
|
+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
|
154
|
+
}
|
155
|
+
},
|
156
|
+
'{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}',
|
157
|
+
'CONTENT_TYPE' => 'application/vnd.api+json'
|
158
|
+
)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
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
|
@@ -115,7 +115,8 @@ module ActiveModelSerializers
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def test_that_raises_with_a_invalid_json_body
|
118
|
-
message
|
118
|
+
# message changes from JSON gem 2.0.2 to 2.2.0
|
119
|
+
message = /A JSON text must at least contain two octets!|unexpected token at ''/
|
119
120
|
|
120
121
|
get :invalid_json_body
|
121
122
|
|
@@ -123,7 +124,7 @@ module ActiveModelSerializers
|
|
123
124
|
assert_response_schema('custom/show.json')
|
124
125
|
end
|
125
126
|
|
126
|
-
|
127
|
+
assert_match(message, error.message)
|
127
128
|
end
|
128
129
|
end
|
129
130
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class AttributesTest < ActiveSupport::TestCase
|
6
|
+
class Person < ActiveModelSerializers::Model
|
7
|
+
attributes :first_name, :last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
class PersonSerializer < ActiveModel::Serializer
|
11
|
+
attributes :first_name, :last_name
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
ActionController::Base.cache_store.clear
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_serializable_hash
|
19
|
+
person = Person.new(first_name: 'Arthur', last_name: 'Dent')
|
20
|
+
serializer = PersonSerializer.new(person)
|
21
|
+
adapter = ActiveModelSerializers::Adapter::Attributes.new(serializer)
|
22
|
+
|
23
|
+
assert_equal({ first_name: 'Arthur', last_name: 'Dent' },
|
24
|
+
adapter.serializable_hash)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_serializable_hash_with_transform_key_casing
|
28
|
+
person = Person.new(first_name: 'Arthur', last_name: 'Dent')
|
29
|
+
serializer = PersonSerializer.new(person)
|
30
|
+
adapter = ActiveModelSerializers::Adapter::Attributes.new(
|
31
|
+
serializer,
|
32
|
+
key_transform: :camel_lower
|
33
|
+
)
|
34
|
+
|
35
|
+
assert_equal({ firstName: 'Arthur', lastName: 'Dent' },
|
36
|
+
adapter.serializable_hash)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
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
|
@@ -4,6 +4,10 @@ module ActiveModelSerializers
|
|
4
4
|
module Adapter
|
5
5
|
class Json
|
6
6
|
class HasManyTestTest < ActiveSupport::TestCase
|
7
|
+
class ModelWithoutSerializer < ::Model
|
8
|
+
attributes :id, :name
|
9
|
+
end
|
10
|
+
|
7
11
|
def setup
|
8
12
|
ActionController::Base.cache_store.clear
|
9
13
|
@author = Author.new(id: 1, name: 'Steve K.')
|
@@ -16,7 +20,7 @@ module ActiveModelSerializers
|
|
16
20
|
@second_comment.post = @post
|
17
21
|
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
18
22
|
@post.blog = @blog
|
19
|
-
@tag =
|
23
|
+
@tag = ModelWithoutSerializer.new(id: 1, name: '#hash_tag')
|
20
24
|
@post.tags = [@tag]
|
21
25
|
end
|
22
26
|
|
@@ -30,7 +34,11 @@ module ActiveModelSerializers
|
|
30
34
|
end
|
31
35
|
|
32
36
|
def test_has_many_with_no_serializer
|
33
|
-
|
37
|
+
post_serializer_class = Class.new(ActiveModel::Serializer) do
|
38
|
+
attributes :id
|
39
|
+
has_many :tags
|
40
|
+
end
|
41
|
+
serializer = post_serializer_class.new(@post)
|
34
42
|
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
35
43
|
assert_equal({
|
36
44
|
id: 42,
|
@@ -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,18 @@ module ActiveModelSerializers
|
|
4
4
|
module Adapter
|
5
5
|
class JsonApi
|
6
6
|
class FieldsTest < ActiveSupport::TestCase
|
7
|
-
Post
|
7
|
+
class Post < ::Model
|
8
|
+
attributes :title, :body
|
9
|
+
associations :author, :comments
|
10
|
+
end
|
11
|
+
class Author < ::Model
|
12
|
+
attributes :name, :birthday
|
13
|
+
end
|
14
|
+
class Comment < ::Model
|
15
|
+
attributes :body
|
16
|
+
associations :author, :post
|
17
|
+
end
|
18
|
+
|
8
19
|
class PostSerializer < ActiveModel::Serializer
|
9
20
|
type 'posts'
|
10
21
|
attributes :title, :body
|
@@ -12,13 +23,11 @@ module ActiveModelSerializers
|
|
12
23
|
has_many :comments
|
13
24
|
end
|
14
25
|
|
15
|
-
Author = Class.new(::Model)
|
16
26
|
class AuthorSerializer < ActiveModel::Serializer
|
17
27
|
type 'authors'
|
18
28
|
attributes :name, :birthday
|
19
29
|
end
|
20
30
|
|
21
|
-
Comment = Class.new(::Model)
|
22
31
|
class CommentSerializer < ActiveModel::Serializer
|
23
32
|
type 'comments'
|
24
33
|
attributes :body
|
@@ -4,6 +4,10 @@ module ActiveModelSerializers
|
|
4
4
|
module Adapter
|
5
5
|
class JsonApi
|
6
6
|
class HasManyTest < ActiveSupport::TestCase
|
7
|
+
class ModelWithoutSerializer < ::Model
|
8
|
+
attributes :id, :name
|
9
|
+
end
|
10
|
+
|
7
11
|
def setup
|
8
12
|
ActionController::Base.cache_store.clear
|
9
13
|
@author = Author.new(id: 1, name: 'Steve K.')
|
@@ -26,7 +30,7 @@ module ActiveModelSerializers
|
|
26
30
|
@blog.articles = [@post]
|
27
31
|
@post.blog = @blog
|
28
32
|
@post_without_comments.blog = nil
|
29
|
-
@tag =
|
33
|
+
@tag = ModelWithoutSerializer.new(id: 1, name: '#hash_tag')
|
30
34
|
@post.tags = [@tag]
|
31
35
|
@serializer = PostSerializer.new(@post)
|
32
36
|
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
@@ -40,6 +44,27 @@ module ActiveModelSerializers
|
|
40
44
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
41
45
|
end
|
42
46
|
|
47
|
+
test 'relationships can be whitelisted via fields' do
|
48
|
+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, fields: { posts: [:author] })
|
49
|
+
result = @adapter.serializable_hash
|
50
|
+
expected = {
|
51
|
+
data: {
|
52
|
+
id: '1',
|
53
|
+
type: 'posts',
|
54
|
+
relationships: {
|
55
|
+
author: {
|
56
|
+
data: {
|
57
|
+
id: '1',
|
58
|
+
type: 'authors'
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
assert_equal expected, result
|
66
|
+
end
|
67
|
+
|
43
68
|
def test_includes_linked_comments
|
44
69
|
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
|
45
70
|
expected = [{
|
@@ -108,18 +133,22 @@ module ActiveModelSerializers
|
|
108
133
|
end
|
109
134
|
|
110
135
|
def test_has_many_with_no_serializer
|
111
|
-
|
136
|
+
post_serializer_class = Class.new(ActiveModel::Serializer) do
|
137
|
+
attributes :id
|
138
|
+
has_many :tags
|
139
|
+
end
|
140
|
+
serializer = post_serializer_class.new(@post)
|
112
141
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
113
142
|
|
114
143
|
assert_equal({
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
144
|
+
data: {
|
145
|
+
id: '1',
|
146
|
+
type: 'posts',
|
147
|
+
relationships: {
|
148
|
+
tags: { data: [@tag.as_json] }
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}, adapter.serializable_hash)
|
123
152
|
end
|
124
153
|
|
125
154
|
def test_has_many_with_virtual_value
|
@@ -127,16 +156,16 @@ module ActiveModelSerializers
|
|
127
156
|
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
128
157
|
|
129
158
|
assert_equal({
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
159
|
+
data: {
|
160
|
+
id: '1',
|
161
|
+
type: 'virtual-values',
|
162
|
+
relationships: {
|
163
|
+
maker: { data: { type: 'makers', id: '1' } },
|
164
|
+
reviews: { data: [{ type: 'reviews', id: '1' },
|
165
|
+
{ type: 'reviews', id: '2' }] }
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}, adapter.serializable_hash)
|
140
169
|
end
|
141
170
|
end
|
142
171
|
end
|