active_model_serializers 0.10.7 → 0.10.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +14 -3
- data/CHANGELOG.md +29 -1
- data/Gemfile +5 -3
- data/README.md +2 -2
- data/Rakefile +2 -0
- data/active_model_serializers.gemspec +2 -1
- data/docs/general/serializers.md +16 -4
- data/lib/action_controller/serialization.rb +2 -0
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +2 -0
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
- data/lib/active_model/serializer/collection_serializer.rb +6 -6
- data/lib/active_model/serializer/concerns/caching.rb +5 -1
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +2 -0
- data/lib/active_model/serializer/has_many_reflection.rb +2 -0
- data/lib/active_model/serializer/has_one_reflection.rb +2 -0
- data/lib/active_model/serializer/lazy_association.rb +6 -2
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +5 -0
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +22 -14
- data/lib/active_model_serializers/adapter/attributes.rb +2 -0
- data/lib/active_model_serializers/adapter/base.rb +2 -0
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +14 -9
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +2 -0
- data/lib/active_model_serializers/model.rb +2 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +2 -0
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +20 -10
- data/lib/generators/rails/resource_override.rb +2 -0
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +2 -0
- data/test/action_controller/adapter_selector_test.rb +2 -0
- data/test/action_controller/explicit_serializer_test.rb +2 -0
- data/test/action_controller/json/include_test.rb +2 -0
- data/test/action_controller/json_api/deserialization_test.rb +2 -0
- data/test/action_controller/json_api/errors_test.rb +2 -0
- data/test/action_controller/json_api/fields_test.rb +2 -0
- data/test/action_controller/json_api/linked_test.rb +2 -0
- data/test/action_controller/json_api/pagination_test.rb +2 -0
- data/test/action_controller/json_api/transform_test.rb +2 -0
- data/test/action_controller/lookup_proc_test.rb +2 -0
- data/test/action_controller/namespace_lookup_test.rb +2 -0
- data/test/action_controller/serialization_scope_name_test.rb +2 -0
- data/test/action_controller/serialization_test.rb +11 -3
- data/test/active_model_serializers/adapter_for_test.rb +2 -0
- data/test/active_model_serializers/json_pointer_test.rb +2 -0
- data/test/active_model_serializers/logging_test.rb +2 -0
- data/test/active_model_serializers/model_test.rb +2 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +2 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +2 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +2 -0
- data/test/active_model_serializers/test/schema_test.rb +2 -0
- data/test/active_model_serializers/test/serializer_test.rb +2 -0
- data/test/active_record_test.rb +2 -0
- data/test/adapter/attributes_test.rb +2 -0
- data/test/adapter/deprecation_test.rb +2 -0
- data/test/adapter/json/belongs_to_test.rb +2 -0
- data/test/adapter/json/collection_test.rb +2 -0
- data/test/adapter/json/has_many_test.rb +2 -0
- data/test/adapter/json/transform_test.rb +2 -0
- data/test/adapter/json_api/belongs_to_test.rb +2 -0
- data/test/adapter/json_api/collection_test.rb +2 -0
- data/test/adapter/json_api/errors_test.rb +2 -0
- data/test/adapter/json_api/fields_test.rb +2 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
- data/test/adapter/json_api/has_many_test.rb +2 -0
- data/test/adapter/json_api/has_one_test.rb +2 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +2 -0
- data/test/adapter/json_api/json_api_test.rb +2 -0
- data/test/adapter/json_api/linked_test.rb +2 -0
- data/test/adapter/json_api/links_test.rb +18 -1
- data/test/adapter/json_api/pagination_links_test.rb +2 -0
- data/test/adapter/json_api/parse_test.rb +2 -0
- data/test/adapter/json_api/relationship_test.rb +2 -0
- data/test/adapter/json_api/resource_meta_test.rb +2 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
- data/test/adapter/json_api/transform_test.rb +2 -0
- data/test/adapter/json_api/type_test.rb +2 -0
- data/test/adapter/json_test.rb +2 -0
- data/test/adapter/null_test.rb +2 -0
- data/test/adapter/polymorphic_test.rb +2 -0
- data/test/adapter_test.rb +2 -0
- data/test/array_serializer_test.rb +2 -0
- data/test/benchmark/app.rb +2 -0
- data/test/benchmark/benchmarking_support.rb +2 -0
- data/test/benchmark/bm_active_record.rb +2 -0
- data/test/benchmark/bm_adapter.rb +2 -0
- data/test/benchmark/bm_caching.rb +2 -0
- data/test/benchmark/bm_lookup_chain.rb +2 -0
- data/test/benchmark/bm_transform.rb +2 -0
- data/test/benchmark/controllers.rb +2 -0
- data/test/benchmark/fixtures.rb +2 -0
- data/test/cache_test.rb +69 -3
- data/test/collection_serializer_test.rb +2 -0
- data/test/fixtures/active_record.rb +2 -0
- data/test/fixtures/poro.rb +2 -0
- data/test/generators/scaffold_controller_generator_test.rb +2 -0
- data/test/generators/serializer_generator_test.rb +2 -0
- data/test/grape_test.rb +2 -0
- data/test/lint_test.rb +2 -0
- data/test/logger_test.rb +2 -0
- data/test/poro_test.rb +2 -0
- data/test/serializable_resource_test.rb +2 -0
- data/test/serializers/association_macros_test.rb +2 -0
- data/test/serializers/associations_test.rb +52 -0
- data/test/serializers/attribute_test.rb +2 -0
- data/test/serializers/attributes_test.rb +2 -0
- data/test/serializers/caching_configuration_test_isolated.rb +2 -0
- data/test/serializers/configuration_test.rb +2 -0
- data/test/serializers/fieldset_test.rb +2 -0
- data/test/serializers/meta_test.rb +2 -0
- data/test/serializers/options_test.rb +2 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +2 -0
- data/test/serializers/reflection_test.rb +54 -0
- data/test/serializers/root_test.rb +2 -0
- data/test/serializers/serialization_test.rb +2 -0
- data/test/serializers/serializer_for_test.rb +2 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +2 -0
- data/test/support/isolated_unit.rb +2 -0
- data/test/support/rails5_shims.rb +2 -0
- data/test/support/rails_app.rb +2 -0
- data/test/support/serialization_testing.rb +2 -0
- data/test/test_helper.rb +2 -0
- metadata +56 -35
data/test/adapter/json_test.rb
CHANGED
data/test/adapter/null_test.rb
CHANGED
data/test/adapter_test.rb
CHANGED
data/test/benchmark/app.rb
CHANGED
data/test/benchmark/fixtures.rb
CHANGED
data/test/cache_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'tmpdir'
|
3
5
|
require 'tempfile'
|
@@ -53,6 +55,11 @@ module ActiveModelSerializers
|
|
53
55
|
has_many :roles
|
54
56
|
has_one :bio
|
55
57
|
end
|
58
|
+
class AuthorSerializerWithCache < ActiveModel::Serializer
|
59
|
+
cache
|
60
|
+
|
61
|
+
attributes :name
|
62
|
+
end
|
56
63
|
|
57
64
|
class Blog < ::Model
|
58
65
|
attributes :name
|
@@ -144,6 +151,65 @@ module ActiveModelSerializers
|
|
144
151
|
@blog_serializer = BlogSerializer.new(@blog)
|
145
152
|
end
|
146
153
|
|
154
|
+
def test_expiring_of_cache_at_update_of_record
|
155
|
+
original_cache_versioning = :none
|
156
|
+
|
157
|
+
if ARModels::Author.respond_to?(:cache_versioning)
|
158
|
+
original_cache_versioning = ARModels::Author.cache_versioning
|
159
|
+
ARModels::Author.cache_versioning = true
|
160
|
+
end
|
161
|
+
|
162
|
+
author = ARModels::Author.create(name: 'Foo')
|
163
|
+
author_json = AuthorSerializerWithCache.new(author).as_json
|
164
|
+
|
165
|
+
assert_equal 'Foo', author_json[:name]
|
166
|
+
|
167
|
+
author.update_attributes(name: 'Bar')
|
168
|
+
author_json = AuthorSerializerWithCache.new(author).as_json
|
169
|
+
|
170
|
+
expected = 'Bar'
|
171
|
+
actual = author_json[:name]
|
172
|
+
if ENV['APPVEYOR'] && actual != expected
|
173
|
+
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
174
|
+
else
|
175
|
+
assert_equal expected, actual
|
176
|
+
end
|
177
|
+
ensure
|
178
|
+
ARModels::Author.cache_versioning = original_cache_versioning unless original_cache_versioning == :none
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_cache_expiration_in_collection_on_update_of_record
|
182
|
+
original_cache_versioning = :none
|
183
|
+
|
184
|
+
if ARModels::Author.respond_to?(:cache_versioning)
|
185
|
+
original_cache_versioning = ARModels::Author.cache_versioning
|
186
|
+
ARModels::Author.cache_versioning = true
|
187
|
+
end
|
188
|
+
|
189
|
+
foo = 'Foo'
|
190
|
+
foo2 = 'Foo2'
|
191
|
+
author = ARModels::Author.create(name: foo)
|
192
|
+
author2 = ARModels::Author.create(name: foo2)
|
193
|
+
author_collection = [author, author, author2]
|
194
|
+
|
195
|
+
collection_json = render_object_with_cache(author_collection, each_serializer: AuthorSerializerWithCache)
|
196
|
+
actual = collection_json
|
197
|
+
expected = [{ name: foo }, { name: foo }, { name: foo2 }]
|
198
|
+
if ENV['APPVEYOR'] && actual != expected
|
199
|
+
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
200
|
+
else
|
201
|
+
assert_equal expected, actual
|
202
|
+
end
|
203
|
+
|
204
|
+
bar = 'Bar'
|
205
|
+
author.update!(name: bar)
|
206
|
+
|
207
|
+
collection_json = render_object_with_cache(author_collection, each_serializer: AuthorSerializerWithCache)
|
208
|
+
assert_equal [{ name: bar }, { name: bar }, { name: foo2 }], collection_json
|
209
|
+
ensure
|
210
|
+
ARModels::Author.cache_versioning = original_cache_versioning unless original_cache_versioning == :none
|
211
|
+
end
|
212
|
+
|
147
213
|
def test_explicit_cache_store
|
148
214
|
default_store = Class.new(ActiveModel::Serializer) do
|
149
215
|
cache
|
@@ -415,7 +481,7 @@ module ActiveModelSerializers
|
|
415
481
|
adapter_options = {}
|
416
482
|
adapter_instance = ActiveModelSerializers::Adapter::Attributes.new(serializers, adapter_options)
|
417
483
|
serializers.serializable_hash(adapter_options, options, adapter_instance)
|
418
|
-
cached_attributes =
|
484
|
+
cached_attributes = options.fetch(:cached_attributes).with_indifferent_access
|
419
485
|
|
420
486
|
include_directive = ActiveModelSerializers.default_include_directive
|
421
487
|
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive).with_indifferent_access
|
@@ -446,9 +512,9 @@ module ActiveModelSerializers
|
|
446
512
|
serializers.serializable_hash(adapter_options, options, adapter_instance)
|
447
513
|
|
448
514
|
# Should find something with read_multi now
|
449
|
-
|
515
|
+
options = {}
|
450
516
|
serializers.serializable_hash(adapter_options, options, adapter_instance)
|
451
|
-
cached_attributes =
|
517
|
+
cached_attributes = options.fetch(:cached_attributes)
|
452
518
|
|
453
519
|
include_directive = ActiveModelSerializers.default_include_directive
|
454
520
|
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive)
|
data/test/fixtures/poro.rb
CHANGED
data/test/grape_test.rb
CHANGED
data/test/lint_test.rb
CHANGED
data/test/logger_test.rb
CHANGED
data/test/poro_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
module ActiveModel
|
3
5
|
class Serializer
|
@@ -285,6 +287,56 @@ module ActiveModel
|
|
285
287
|
end
|
286
288
|
end
|
287
289
|
|
290
|
+
class AssociationsNamespacedSerializersTest < ActiveSupport::TestCase
|
291
|
+
class Post < ::Model
|
292
|
+
associations :comments, :author, :description
|
293
|
+
|
294
|
+
def latest_comments
|
295
|
+
comments[0..3]
|
296
|
+
end
|
297
|
+
end
|
298
|
+
class Comment < ::Model; end
|
299
|
+
class Author < ::Model; end
|
300
|
+
class Description < ::Model; end
|
301
|
+
|
302
|
+
class ResourceNamespace
|
303
|
+
class PostSerializer < ActiveModel::Serializer
|
304
|
+
has_many :comments, namespace: ResourceNamespace
|
305
|
+
has_many :latest_comments, namespace: ResourceNamespace
|
306
|
+
belongs_to :author, namespace: ResourceNamespace
|
307
|
+
has_one :description, namespace: ResourceNamespace
|
308
|
+
end
|
309
|
+
class CommentSerializer < ActiveModel::Serializer; end
|
310
|
+
class AuthorSerializer < ActiveModel::Serializer; end
|
311
|
+
class DescriptionSerializer < ActiveModel::Serializer; end
|
312
|
+
end
|
313
|
+
|
314
|
+
def setup
|
315
|
+
@comment = Comment.new
|
316
|
+
@author = Author.new
|
317
|
+
@description = Description.new
|
318
|
+
@post = Post.new(comments: [@comment],
|
319
|
+
author: @author,
|
320
|
+
description: @description)
|
321
|
+
@post_serializer = ResourceNamespace::PostSerializer.new(@post)
|
322
|
+
end
|
323
|
+
|
324
|
+
def test_associations_namespaced_serializers
|
325
|
+
@post_serializer.associations.each do |association|
|
326
|
+
case association.key
|
327
|
+
when :comments, :latest_comments
|
328
|
+
assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
|
329
|
+
when :author
|
330
|
+
assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
|
331
|
+
when :description
|
332
|
+
assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
|
333
|
+
else
|
334
|
+
flunk "Unknown association: #{key}"
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
288
340
|
class NestedSerializersTest < ActiveSupport::TestCase
|
289
341
|
class Post < ::Model
|
290
342
|
associations :comments, :author, :description
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
module ActiveModel
|
3
5
|
class Serializer
|
@@ -423,5 +425,57 @@ module ActiveModel
|
|
423
425
|
end
|
424
426
|
# rubocop:enable Metrics/AbcSize
|
425
427
|
end
|
428
|
+
class ThreadedReflectionTest < ActiveSupport::TestCase
|
429
|
+
class Post < ::Model
|
430
|
+
attributes :id, :title, :body
|
431
|
+
associations :comments
|
432
|
+
end
|
433
|
+
class Comment < ::Model
|
434
|
+
attributes :id, :body
|
435
|
+
associations :post
|
436
|
+
end
|
437
|
+
class CommentSerializer < ActiveModel::Serializer
|
438
|
+
type 'comment'
|
439
|
+
attributes :id, :body
|
440
|
+
has_one :post
|
441
|
+
end
|
442
|
+
class PostSerializer < ActiveModel::Serializer
|
443
|
+
type 'post'
|
444
|
+
attributes :id, :title, :body
|
445
|
+
has_many :comments, serializer: CommentSerializer do
|
446
|
+
sleep 0.1
|
447
|
+
object.comments
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
# per https://github.com/rails-api/active_model_serializers/issues/2270
|
452
|
+
def test_concurrent_serialization
|
453
|
+
post1 = Post.new(id: 1, title: 'Post 1 Title', body: 'Post 1 Body')
|
454
|
+
post1.comments = [Comment.new(id: 1, body: 'Comment on Post 1', post: post1)]
|
455
|
+
post2 = Post.new(id: 2, title: 'Post 2 Title', body: 'Post 2 Body')
|
456
|
+
post2.comments = [Comment.new(id: 2, body: 'Comment on Post 2', post: post2)]
|
457
|
+
serialized_posts = {
|
458
|
+
first: Set.new,
|
459
|
+
second: Set.new
|
460
|
+
}
|
461
|
+
t1 = Thread.new do
|
462
|
+
10.times do
|
463
|
+
serialized_posts[:first] << PostSerializer.new(post1, {}).to_json
|
464
|
+
end
|
465
|
+
end
|
466
|
+
t2 = Thread.new do
|
467
|
+
10.times do
|
468
|
+
serialized_posts[:second] << PostSerializer.new(post2, {}).to_json
|
469
|
+
end
|
470
|
+
end
|
471
|
+
t1.join
|
472
|
+
t2.join
|
473
|
+
expected_first_post_serialization = '{"id":1,"title":"Post 1 Title","body":"Post 1 Body","comments":[{"id":1,"body":"Comment on Post 1"}]}'
|
474
|
+
expected_second_post_serialization = '{"id":2,"title":"Post 2 Title","body":"Post 2 Body","comments":[{"id":2,"body":"Comment on Post 2"}]}'
|
475
|
+
|
476
|
+
assert_equal [expected_second_post_serialization], serialized_posts[:second].to_a
|
477
|
+
assert_equal [expected_first_post_serialization], serialized_posts[:first].to_a
|
478
|
+
end
|
479
|
+
end
|
426
480
|
end
|
427
481
|
end
|
data/test/support/rails_app.rb
CHANGED