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,479 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
class ReflectionTest < ActiveSupport::TestCase
|
5
|
+
class Blog < ActiveModelSerializers::Model
|
6
|
+
attributes :id
|
7
|
+
end
|
8
|
+
class BlogSerializer < ActiveModel::Serializer
|
9
|
+
type 'blog'
|
10
|
+
attributes :id
|
11
|
+
end
|
12
|
+
|
13
|
+
setup do
|
14
|
+
@expected_meta = { id: 1 }
|
15
|
+
@expected_links = { self: 'no_uri_validation' }
|
16
|
+
@empty_links = {}
|
17
|
+
model_attributes = { blog: Blog.new(@expected_meta) }
|
18
|
+
@model = Class.new(ActiveModelSerializers::Model) do
|
19
|
+
attributes(*model_attributes.keys)
|
20
|
+
|
21
|
+
def self.name
|
22
|
+
'TestModel'
|
23
|
+
end
|
24
|
+
end.new(model_attributes)
|
25
|
+
@instance_options = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def evaluate_association_value(association)
|
29
|
+
association.lazy_association.eval_reflection_block
|
30
|
+
end
|
31
|
+
|
32
|
+
# TODO: Remaining tests
|
33
|
+
# test_reflection_value_block_with_scope
|
34
|
+
# test_reflection_value_uses_serializer_instance_method
|
35
|
+
# test_reflection_excluded_eh_blank_is_false
|
36
|
+
# test_reflection_excluded_eh_if
|
37
|
+
# test_reflection_excluded_eh_unless
|
38
|
+
# test_evaluate_condition_symbol_serializer_method
|
39
|
+
# test_evaluate_condition_string_serializer_method
|
40
|
+
# test_evaluate_condition_proc
|
41
|
+
# test_evaluate_condition_proc_yields_serializer
|
42
|
+
# test_evaluate_condition_other
|
43
|
+
# test_options_key
|
44
|
+
# test_options_polymorphic
|
45
|
+
# test_options_serializer
|
46
|
+
# test_options_virtual_value
|
47
|
+
# test_options_namespace
|
48
|
+
|
49
|
+
def test_reflection_value
|
50
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
51
|
+
has_one :blog
|
52
|
+
end
|
53
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
54
|
+
|
55
|
+
# Get Reflection
|
56
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
57
|
+
|
58
|
+
# Assert
|
59
|
+
assert_nil reflection.block
|
60
|
+
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
61
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
62
|
+
|
63
|
+
include_slice = :does_not_matter
|
64
|
+
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_reflection_value_block
|
68
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
69
|
+
has_one :blog do
|
70
|
+
object.blog
|
71
|
+
end
|
72
|
+
end
|
73
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
74
|
+
|
75
|
+
# Get Reflection
|
76
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
77
|
+
|
78
|
+
# Assert
|
79
|
+
assert_respond_to reflection.block, :call
|
80
|
+
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
81
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
82
|
+
|
83
|
+
include_slice = :does_not_matter
|
84
|
+
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_reflection_value_block_with_explicit_include_data_true
|
88
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
89
|
+
has_one :blog do
|
90
|
+
include_data true
|
91
|
+
object.blog
|
92
|
+
end
|
93
|
+
end
|
94
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
95
|
+
|
96
|
+
# Get Reflection
|
97
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
98
|
+
|
99
|
+
# Assert
|
100
|
+
assert_respond_to reflection.block, :call
|
101
|
+
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
102
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
103
|
+
|
104
|
+
include_slice = :does_not_matter
|
105
|
+
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_reflection_value_block_with_include_data_false_mutates_the_reflection_include_data
|
109
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
110
|
+
has_one :blog do
|
111
|
+
include_data false
|
112
|
+
object.blog
|
113
|
+
end
|
114
|
+
end
|
115
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
116
|
+
|
117
|
+
# Get Reflection
|
118
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
119
|
+
|
120
|
+
# Assert
|
121
|
+
assert_respond_to reflection.block, :call
|
122
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
123
|
+
include_slice = :does_not_matter
|
124
|
+
assert_nil reflection.send(:value, serializer_instance, include_slice)
|
125
|
+
assert_equal false, reflection.options.fetch(:include_data_setting)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_reflection_value_block_with_include_data_if_sideloaded_included_mutates_the_reflection_include_data
|
129
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
130
|
+
has_one :blog do
|
131
|
+
include_data :if_sideloaded
|
132
|
+
object.blog
|
133
|
+
end
|
134
|
+
end
|
135
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
136
|
+
|
137
|
+
# Get Reflection
|
138
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
139
|
+
|
140
|
+
# Assert
|
141
|
+
assert_respond_to reflection.block, :call
|
142
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
143
|
+
include_slice = {}
|
144
|
+
assert_nil reflection.send(:value, serializer_instance, include_slice)
|
145
|
+
assert_equal :if_sideloaded, reflection.options.fetch(:include_data_setting)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_reflection_value_block_with_include_data_if_sideloaded_excluded_mutates_the_reflection_include_data
|
149
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
150
|
+
has_one :blog do
|
151
|
+
include_data :if_sideloaded
|
152
|
+
object.blog
|
153
|
+
end
|
154
|
+
end
|
155
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
156
|
+
|
157
|
+
# Get Reflection
|
158
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
159
|
+
|
160
|
+
# Assert
|
161
|
+
assert_respond_to reflection.block, :call
|
162
|
+
assert_equal true, reflection.options.fetch(:include_data_setting)
|
163
|
+
include_slice = { blog: :does_not_matter }
|
164
|
+
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
165
|
+
assert_equal :if_sideloaded, reflection.options.fetch(:include_data_setting)
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_reflection_block_with_link_mutates_the_reflection_links
|
169
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
170
|
+
has_one :blog do
|
171
|
+
link :self, 'no_uri_validation'
|
172
|
+
end
|
173
|
+
end
|
174
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
175
|
+
|
176
|
+
# Get Reflection
|
177
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
178
|
+
assert_equal @empty_links, reflection.options.fetch(:links)
|
179
|
+
|
180
|
+
# Build Association
|
181
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
182
|
+
|
183
|
+
# Assert association links empty when not yet evaluated
|
184
|
+
assert_equal @empty_links, reflection.options.fetch(:links)
|
185
|
+
assert_equal @empty_links, association.links
|
186
|
+
|
187
|
+
evaluate_association_value(association)
|
188
|
+
|
189
|
+
assert_equal @expected_links, association.links
|
190
|
+
assert_equal @expected_links, reflection.options.fetch(:links)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_reflection_block_with_link_block_mutates_the_reflection_links
|
194
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
195
|
+
has_one :blog do
|
196
|
+
link :self do
|
197
|
+
'no_uri_validation'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
202
|
+
|
203
|
+
# Get Reflection
|
204
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
205
|
+
assert_equal @empty_links, reflection.options.fetch(:links)
|
206
|
+
|
207
|
+
# Build Association
|
208
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
209
|
+
|
210
|
+
# Assert association links empty when not yet evaluated
|
211
|
+
assert_equal @empty_links, association.links
|
212
|
+
|
213
|
+
evaluate_association_value(association)
|
214
|
+
|
215
|
+
# Assert before instance_eval link
|
216
|
+
link = association.links.fetch(:self)
|
217
|
+
assert_respond_to link, :call
|
218
|
+
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
219
|
+
|
220
|
+
# Assert after instance_eval link
|
221
|
+
assert_equal @expected_links.fetch(:self), reflection.instance_eval(&link)
|
222
|
+
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_reflection_block_with_meta_mutates_the_reflection_meta
|
226
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
227
|
+
has_one :blog do
|
228
|
+
meta(id: object.blog.id)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
232
|
+
|
233
|
+
# Get Reflection
|
234
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
235
|
+
assert_nil reflection.options.fetch(:meta)
|
236
|
+
|
237
|
+
# Build Association
|
238
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
239
|
+
|
240
|
+
evaluate_association_value(association)
|
241
|
+
|
242
|
+
assert_equal @expected_meta, association.meta
|
243
|
+
assert_equal @expected_meta, reflection.options.fetch(:meta)
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_reflection_block_with_meta_block_mutates_the_reflection_meta
|
247
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
248
|
+
has_one :blog do
|
249
|
+
meta do
|
250
|
+
{ id: object.blog.id }
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
255
|
+
|
256
|
+
# Get Reflection
|
257
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
258
|
+
assert_nil reflection.options.fetch(:meta)
|
259
|
+
|
260
|
+
# Build Association
|
261
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
262
|
+
# Assert before instance_eval meta
|
263
|
+
|
264
|
+
evaluate_association_value(association)
|
265
|
+
|
266
|
+
assert_respond_to association.meta, :call
|
267
|
+
assert_respond_to reflection.options.fetch(:meta), :call
|
268
|
+
|
269
|
+
# Assert after instance_eval meta
|
270
|
+
assert_equal @expected_meta, reflection.instance_eval(&association.meta)
|
271
|
+
assert_respond_to reflection.options.fetch(:meta), :call
|
272
|
+
assert_respond_to association.meta, :call
|
273
|
+
end
|
274
|
+
|
275
|
+
# rubocop:disable Metrics/AbcSize
|
276
|
+
def test_reflection_block_with_meta_in_link_block_mutates_the_reflection_meta
|
277
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
278
|
+
has_one :blog do
|
279
|
+
link :self do
|
280
|
+
meta(id: object.blog.id)
|
281
|
+
'no_uri_validation'
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
286
|
+
|
287
|
+
# Get Reflection
|
288
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
289
|
+
assert_nil reflection.options.fetch(:meta)
|
290
|
+
assert_equal @empty_links, reflection.options.fetch(:links)
|
291
|
+
|
292
|
+
# Build Association
|
293
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
294
|
+
# Assert before instance_eval link meta
|
295
|
+
assert_nil association.meta
|
296
|
+
assert_nil reflection.options.fetch(:meta)
|
297
|
+
|
298
|
+
evaluate_association_value(association)
|
299
|
+
|
300
|
+
link = association.links.fetch(:self)
|
301
|
+
assert_respond_to link, :call
|
302
|
+
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
303
|
+
assert_nil reflection.options.fetch(:meta)
|
304
|
+
|
305
|
+
# Assert after instance_eval link
|
306
|
+
assert_equal 'no_uri_validation', reflection.instance_eval(&link)
|
307
|
+
assert_equal @expected_meta, reflection.options.fetch(:meta)
|
308
|
+
assert_equal @expected_meta, association.meta
|
309
|
+
end
|
310
|
+
# rubocop:enable Metrics/AbcSize
|
311
|
+
|
312
|
+
# rubocop:disable Metrics/AbcSize
|
313
|
+
def test_reflection_block_with_meta_block_in_link_block_mutates_the_reflection_meta
|
314
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
315
|
+
has_one :blog do
|
316
|
+
link :self do
|
317
|
+
meta do
|
318
|
+
{ id: object.blog.id }
|
319
|
+
end
|
320
|
+
'no_uri_validation'
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
325
|
+
|
326
|
+
# Get Reflection
|
327
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
328
|
+
assert_nil reflection.options.fetch(:meta)
|
329
|
+
|
330
|
+
# Build Association
|
331
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
332
|
+
assert_nil association.meta
|
333
|
+
assert_nil reflection.options.fetch(:meta)
|
334
|
+
|
335
|
+
# Assert before instance_eval link
|
336
|
+
|
337
|
+
evaluate_association_value(association)
|
338
|
+
|
339
|
+
link = association.links.fetch(:self)
|
340
|
+
assert_nil reflection.options.fetch(:meta)
|
341
|
+
assert_respond_to link, :call
|
342
|
+
assert_respond_to association.links.fetch(:self), :call
|
343
|
+
|
344
|
+
# Assert after instance_eval link
|
345
|
+
assert_equal 'no_uri_validation', reflection.instance_eval(&link)
|
346
|
+
assert_respond_to association.links.fetch(:self), :call
|
347
|
+
# Assert before instance_eval link meta
|
348
|
+
assert_respond_to reflection.options.fetch(:meta), :call
|
349
|
+
assert_respond_to association.meta, :call
|
350
|
+
|
351
|
+
# Assert after instance_eval link meta
|
352
|
+
assert_equal @expected_meta, reflection.instance_eval(&reflection.options.fetch(:meta))
|
353
|
+
assert_respond_to association.meta, :call
|
354
|
+
end
|
355
|
+
# rubocop:enable Metrics/AbcSize
|
356
|
+
|
357
|
+
def test_no_href_in_vanilla_reflection
|
358
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
359
|
+
has_one :blog do
|
360
|
+
link :self do
|
361
|
+
href 'no_uri_validation'
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
366
|
+
|
367
|
+
# Get Reflection
|
368
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
369
|
+
assert_equal @empty_links, reflection.options.fetch(:links)
|
370
|
+
|
371
|
+
# Build Association
|
372
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
373
|
+
# Assert before instance_eval link
|
374
|
+
|
375
|
+
evaluate_association_value(association)
|
376
|
+
|
377
|
+
link = association.links.fetch(:self)
|
378
|
+
assert_respond_to link, :call
|
379
|
+
|
380
|
+
# Assert after instance_eval link
|
381
|
+
exception = assert_raise(NoMethodError) do
|
382
|
+
reflection.instance_eval(&link)
|
383
|
+
end
|
384
|
+
assert_match(/undefined method `href'/, exception.message)
|
385
|
+
end
|
386
|
+
|
387
|
+
# rubocop:disable Metrics/AbcSize
|
388
|
+
def test_mutating_reflection_block_is_not_thread_safe
|
389
|
+
serializer_class = Class.new(ActiveModel::Serializer) do
|
390
|
+
has_one :blog do
|
391
|
+
meta(id: object.blog.id)
|
392
|
+
end
|
393
|
+
end
|
394
|
+
model1_meta = @expected_meta
|
395
|
+
# Evaluate reflection meta for model with id 1
|
396
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
397
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
398
|
+
assert_nil reflection.options.fetch(:meta)
|
399
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
400
|
+
|
401
|
+
evaluate_association_value(association)
|
402
|
+
|
403
|
+
assert_equal model1_meta, association.meta
|
404
|
+
assert_equal model1_meta, reflection.options.fetch(:meta)
|
405
|
+
|
406
|
+
model2_meta = @expected_meta.merge(id: 2)
|
407
|
+
# Evaluate reflection meta for model with id 2
|
408
|
+
@model.blog.id = 2
|
409
|
+
assert_equal 2, @model.blog.id # sanity check
|
410
|
+
serializer_instance = serializer_class.new(@model, @instance_options)
|
411
|
+
reflection = serializer_class._reflections.fetch(:blog)
|
412
|
+
|
413
|
+
# WARN: Thread-safety issue
|
414
|
+
# Before the reflection is evaluated, it has the value from the previous evaluation
|
415
|
+
assert_equal model1_meta, reflection.options.fetch(:meta)
|
416
|
+
|
417
|
+
association = reflection.build_association(serializer_instance, @instance_options)
|
418
|
+
|
419
|
+
evaluate_association_value(association)
|
420
|
+
|
421
|
+
assert_equal model2_meta, association.meta
|
422
|
+
assert_equal model2_meta, reflection.options.fetch(:meta)
|
423
|
+
end
|
424
|
+
# rubocop:enable Metrics/AbcSize
|
425
|
+
end
|
426
|
+
class ThreadedReflectionTest < ActiveSupport::TestCase
|
427
|
+
class Post < ::Model
|
428
|
+
attributes :id, :title, :body
|
429
|
+
associations :comments
|
430
|
+
end
|
431
|
+
class Comment < ::Model
|
432
|
+
attributes :id, :body
|
433
|
+
associations :post
|
434
|
+
end
|
435
|
+
class CommentSerializer < ActiveModel::Serializer
|
436
|
+
type 'comment'
|
437
|
+
attributes :id, :body
|
438
|
+
has_one :post
|
439
|
+
end
|
440
|
+
class PostSerializer < ActiveModel::Serializer
|
441
|
+
type 'post'
|
442
|
+
attributes :id, :title, :body
|
443
|
+
has_many :comments, serializer: CommentSerializer do
|
444
|
+
sleep 0.1
|
445
|
+
object.comments
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
# per https://github.com/rails-api/active_model_serializers/issues/2270
|
450
|
+
def test_concurrent_serialization
|
451
|
+
post1 = Post.new(id: 1, title: 'Post 1 Title', body: 'Post 1 Body')
|
452
|
+
post1.comments = [Comment.new(id: 1, body: 'Comment on Post 1', post: post1)]
|
453
|
+
post2 = Post.new(id: 2, title: 'Post 2 Title', body: 'Post 2 Body')
|
454
|
+
post2.comments = [Comment.new(id: 2, body: 'Comment on Post 2', post: post2)]
|
455
|
+
serialized_posts = {
|
456
|
+
first: Set.new,
|
457
|
+
second: Set.new
|
458
|
+
}
|
459
|
+
t1 = Thread.new do
|
460
|
+
10.times do
|
461
|
+
serialized_posts[:first] << PostSerializer.new(post1, {}).to_json
|
462
|
+
end
|
463
|
+
end
|
464
|
+
t2 = Thread.new do
|
465
|
+
10.times do
|
466
|
+
serialized_posts[:second] << PostSerializer.new(post2, {}).to_json
|
467
|
+
end
|
468
|
+
end
|
469
|
+
t1.join
|
470
|
+
t2.join
|
471
|
+
expected_first_post_serialization = '{"id":1,"title":"Post 1 Title","body":"Post 1 Body","comments":[{"id":1,"body":"Comment on Post 1"}]}'
|
472
|
+
expected_second_post_serialization = '{"id":2,"title":"Post 2 Title","body":"Post 2 Body","comments":[{"id":2,"body":"Comment on Post 2"}]}'
|
473
|
+
|
474
|
+
assert_equal [expected_second_post_serialization], serialized_posts[:second].to_a
|
475
|
+
assert_equal [expected_first_post_serialization], serialized_posts[:first].to_a
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class RootTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
@virtual_value = VirtualValue.new(id: 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_overwrite_root
|
11
|
+
serializer = VirtualValueSerializer.new(@virtual_value, root: 'smth')
|
12
|
+
assert_equal('smth', serializer.json_key)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_underscore_in_root
|
16
|
+
serializer = VirtualValueSerializer.new(@virtual_value)
|
17
|
+
assert_equal('virtual_value', serializer.json_key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
class SerializationTest < ActiveSupport::TestCase
|
4
|
+
class Blog < ActiveModelSerializers::Model
|
5
|
+
attributes :id, :name, :authors
|
6
|
+
end
|
7
|
+
class Author < ActiveModelSerializers::Model
|
8
|
+
attributes :id, :name
|
9
|
+
end
|
10
|
+
class BlogSerializer < ActiveModel::Serializer
|
11
|
+
attributes :id
|
12
|
+
attribute :name, key: :title
|
13
|
+
|
14
|
+
has_many :authors
|
15
|
+
end
|
16
|
+
class AuthorSerializer < ActiveModel::Serializer
|
17
|
+
attributes :id, :name
|
18
|
+
end
|
19
|
+
|
20
|
+
setup do
|
21
|
+
@authors = [Author.new(id: 1, name: 'Blog Author')]
|
22
|
+
@blog = Blog.new(id: 2, name: 'The Blog', authors: @authors)
|
23
|
+
@serializer_instance = BlogSerializer.new(@blog)
|
24
|
+
@serializable = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
|
25
|
+
@expected_hash = { id: 2, title: 'The Blog', authors: [{ id: 1, name: 'Blog Author' }] }
|
26
|
+
@expected_json = '{"id":2,"title":"The Blog","authors":[{"id":1,"name":"Blog Author"}]}'
|
27
|
+
end
|
28
|
+
|
29
|
+
test '#serializable_hash is the same as generated by the attributes adapter' do
|
30
|
+
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash
|
31
|
+
assert_equal @expected_hash, @serializer_instance.serializable_hash
|
32
|
+
end
|
33
|
+
|
34
|
+
test '#as_json is the same as generated by the attributes adapter' do
|
35
|
+
assert_equal @serializable.as_json, @serializer_instance.as_json
|
36
|
+
assert_equal @expected_hash, @serializer_instance.as_json
|
37
|
+
end
|
38
|
+
|
39
|
+
test '#to_json is the same as generated by the attributes adapter' do
|
40
|
+
assert_equal @serializable.to_json, @serializer_instance.to_json
|
41
|
+
assert_equal @expected_json, @serializer_instance.to_json
|
42
|
+
end
|
43
|
+
|
44
|
+
test '#to_h is an alias for #serializable_hash' do
|
45
|
+
assert_equal @serializable.serializable_hash, @serializer_instance.to_h
|
46
|
+
assert_equal @expected_hash, @serializer_instance.to_h
|
47
|
+
end
|
48
|
+
|
49
|
+
test '#to_hash is an alias for #serializable_hash' do
|
50
|
+
assert_equal @serializable.serializable_hash, @serializer_instance.to_hash
|
51
|
+
assert_equal @expected_hash, @serializer_instance.to_hash
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class SerializerForTest < ActiveSupport::TestCase
|
6
|
+
class CollectionSerializerTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
@array = [1, 2, 3]
|
9
|
+
@previous_collection_serializer = ActiveModelSerializers.config.collection_serializer
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
ActiveModelSerializers.config.collection_serializer = @previous_collection_serializer
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_serializer_for_array
|
17
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
18
|
+
assert_equal ActiveModelSerializers.config.collection_serializer, serializer
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_overwritten_serializer_for_array
|
22
|
+
new_collection_serializer = Class.new
|
23
|
+
ActiveModelSerializers.config.collection_serializer = new_collection_serializer
|
24
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
25
|
+
assert_equal new_collection_serializer, serializer
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class SerializerTest < ActiveSupport::TestCase
|
30
|
+
module ResourceNamespace
|
31
|
+
class Post < ::Model; end
|
32
|
+
class Comment < ::Model; end
|
33
|
+
|
34
|
+
class PostSerializer < ActiveModel::Serializer
|
35
|
+
class CommentSerializer < ActiveModel::Serializer
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class MyProfile < Profile
|
41
|
+
end
|
42
|
+
|
43
|
+
class CustomProfile
|
44
|
+
def serializer_class
|
45
|
+
ProfileSerializer
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Tweet < ::Model; end
|
50
|
+
TweetSerializer = Class.new
|
51
|
+
|
52
|
+
def setup
|
53
|
+
@profile = Profile.new
|
54
|
+
@my_profile = MyProfile.new
|
55
|
+
@custom_profile = CustomProfile.new
|
56
|
+
@model = ::Model.new
|
57
|
+
@tweet = Tweet.new
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_serializer_for_non_ams_serializer
|
61
|
+
serializer = ActiveModel::Serializer.serializer_for(@tweet)
|
62
|
+
assert_nil serializer
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_serializer_for_existing_serializer
|
66
|
+
serializer = ActiveModel::Serializer.serializer_for(@profile)
|
67
|
+
assert_equal ProfileSerializer, serializer
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_serializer_for_existing_serializer_with_lookup_disabled
|
71
|
+
serializer = with_serializer_lookup_disabled do
|
72
|
+
ActiveModel::Serializer.serializer_for(@profile)
|
73
|
+
end
|
74
|
+
assert_nil serializer
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_serializer_for_not_existing_serializer
|
78
|
+
serializer = ActiveModel::Serializer.serializer_for(@model)
|
79
|
+
assert_nil serializer
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_serializer_inherited_serializer
|
83
|
+
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
|
84
|
+
assert_equal ProfileSerializer, serializer
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_serializer_inherited_serializer_with_lookup_disabled
|
88
|
+
serializer = with_serializer_lookup_disabled do
|
89
|
+
ActiveModel::Serializer.serializer_for(@my_profile)
|
90
|
+
end
|
91
|
+
assert_nil serializer
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_serializer_custom_serializer
|
95
|
+
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
96
|
+
assert_equal ProfileSerializer, serializer
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_serializer_custom_serializer_with_lookup_disabled
|
100
|
+
serializer = with_serializer_lookup_disabled do
|
101
|
+
ActiveModel::Serializer.serializer_for(@custom_profile)
|
102
|
+
end
|
103
|
+
assert_equal ProfileSerializer, serializer
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_serializer_for_namespaced_resource
|
107
|
+
post = ResourceNamespace::Post.new
|
108
|
+
serializer = ActiveModel::Serializer.serializer_for(post)
|
109
|
+
assert_equal ResourceNamespace::PostSerializer, serializer
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_serializer_for_namespaced_resource_with_lookup_disabled
|
113
|
+
post = ResourceNamespace::Post.new
|
114
|
+
serializer = with_serializer_lookup_disabled do
|
115
|
+
ActiveModel::Serializer.serializer_for(post)
|
116
|
+
end
|
117
|
+
assert_nil serializer
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_serializer_for_nested_resource
|
121
|
+
comment = ResourceNamespace::Comment.new
|
122
|
+
serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
|
123
|
+
assert_equal ResourceNamespace::PostSerializer::CommentSerializer, serializer
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_serializer_for_nested_resource_with_lookup_disabled
|
127
|
+
comment = ResourceNamespace::Comment.new
|
128
|
+
serializer = with_serializer_lookup_disabled do
|
129
|
+
ResourceNamespace::PostSerializer.serializer_for(comment)
|
130
|
+
end
|
131
|
+
assert_nil serializer
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|