active_model_serializers 0.10.0.rc3 → 0.10.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +37 -33
- data/.rubocop_todo.yml +13 -88
- data/.simplecov +23 -11
- data/.travis.yml +17 -12
- data/CHANGELOG.md +417 -12
- data/CONTRIBUTING.md +206 -17
- data/Gemfile +12 -11
- data/README.md +78 -286
- data/Rakefile +44 -8
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +6 -4
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +21 -15
- data/docs/general/adapters.md +79 -27
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +18 -2
- data/docs/general/getting_started.md +44 -19
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +16 -7
- data/docs/howto/add_root_key.md +3 -3
- data/docs/howto/outside_controller_use.md +25 -9
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema.md +140 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/lib/action_controller/serialization.rb +13 -9
- data/lib/active_model/serializable_resource.rb +9 -7
- data/lib/active_model/serializer.rb +93 -129
- data/lib/active_model/serializer/adapter.rb +37 -105
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +43 -7
- data/lib/active_model/serializer/adapter/json.rb +11 -37
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api.rb +127 -62
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +12 -4
- data/lib/active_model/serializer/adapter/null.rb +7 -1
- data/lib/active_model/serializer/array_serializer.rb +6 -37
- data/lib/active_model/serializer/associations.rb +21 -18
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +17 -3
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +9 -18
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +16 -3
- data/lib/active_model/serializer/reflection.rb +25 -8
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +16 -26
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/generators/{serializer → rails}/USAGE +1 -1
- data/lib/generators/{serializer → rails}/resource_override.rb +1 -3
- data/lib/generators/{serializer → rails}/serializer_generator.rb +2 -3
- data/lib/generators/{serializer → rails}/templates/serializer.rb.erb +0 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +20 -3
- data/test/action_controller/json_api/pagination_test.rb +7 -7
- data/test/action_controller/serialization_scope_name_test.rb +8 -12
- data/test/action_controller/serialization_test.rb +44 -29
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +1 -1
- data/test/adapter/fragment_cache_test.rb +3 -2
- data/test/adapter/json/belongs_to_test.rb +2 -2
- data/test/adapter/json/collection_test.rb +15 -5
- data/test/adapter/json/has_many_test.rb +3 -3
- data/test/adapter/json_api/belongs_to_test.rb +3 -3
- data/test/adapter/json_api/collection_test.rb +8 -6
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -2
- data/test/adapter/json_api/has_many_test.rb +3 -3
- data/test/adapter/json_api/has_one_test.rb +2 -2
- data/test/adapter/json_api/json_api_test.rb +2 -2
- data/test/adapter/json_api/linked_test.rb +116 -5
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +4 -4
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +27 -15
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +2 -2
- data/test/adapter/null_test.rb +2 -2
- data/test/adapter_test.rb +3 -3
- data/test/array_serializer_test.rb +30 -93
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/poro.rb +19 -51
- data/test/generators/scaffold_controller_generator_test.rb +1 -0
- data/test/generators/serializer_generator_test.rb +2 -1
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +4 -1
- data/test/logger_test.rb +2 -2
- data/test/poro_test.rb +1 -1
- data/test/serializable_resource_test.rb +1 -1
- data/test/serializers/adapter_for_test.rb +24 -28
- data/test/serializers/association_macros_test.rb +1 -1
- data/test/serializers/associations_test.rb +143 -26
- data/test/serializers/attribute_test.rb +64 -3
- data/test/serializers/attributes_test.rb +1 -6
- data/test/serializers/cache_test.rb +46 -2
- data/test/serializers/configuration_test.rb +20 -3
- data/test/serializers/fieldset_test.rb +5 -16
- data/test/serializers/meta_test.rb +38 -29
- data/test/serializers/options_test.rb +1 -1
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +78 -9
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +7 -3
- 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 +46 -6
- data/test/support/test_case.rb +14 -0
- data/test/test_helper.rb +21 -14
- metadata +160 -16
- data/lib/active_model/serializer/adapter/flatten_json.rb +0 -12
- data/lib/active_model/serializer/railtie.rb +0 -15
- data/lib/active_model/serializer/utils.rb +0 -35
- data/lib/tasks/rubocop.rake +0 -0
- data/test/capture_warnings.rb +0 -65
- data/test/utils/include_args_to_hash_test.rb +0 -79
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
|
-
class HasManyEmbedIdsTest <
|
7
|
+
class HasManyEmbedIdsTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@author = Author.new(name: 'Steve K.')
|
10
10
|
@author.bio = nil
|
@@ -2,10 +2,10 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
7
|
# Test 'has_many :assocs, serializer: AssocXSerializer'
|
8
|
-
class HasManyExplicitSerializerTest <
|
8
|
+
class HasManyExplicitSerializerTest < ActiveSupport::TestCase
|
9
9
|
def setup
|
10
10
|
@post = Post.new(title: 'New Post', body: 'Body')
|
11
11
|
@author = Author.new(name: 'Jane Blogger')
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
|
-
class HasManyTest <
|
7
|
+
class HasManyTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
ActionController::Base.cache_store.clear
|
10
10
|
@author = Author.new(id: 1, name: 'Steve K.')
|
@@ -68,7 +68,7 @@ module ActiveModel
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_limit_fields_of_linked_comments
|
71
|
-
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:comments], fields: { comment: [:id] })
|
71
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:comments], fields: { comment: [:id, :post, :author] })
|
72
72
|
expected = [{
|
73
73
|
id: '1',
|
74
74
|
type: 'comments',
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
|
-
class HasOneTest <
|
7
|
+
class HasOneTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@author = Author.new(id: 1, name: 'Steve K.')
|
10
10
|
@bio = Bio.new(id: 43, content: 'AMS Contributor')
|
@@ -2,8 +2,8 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
6
|
-
class JsonApiTest <
|
5
|
+
module Adapter
|
6
|
+
class JsonApiTest < ActiveSupport::TestCase
|
7
7
|
def setup
|
8
8
|
ActionController::Base.cache_store.clear
|
9
9
|
@author = Author.new(id: 1, name: 'Steve K.')
|
@@ -1,11 +1,16 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
|
3
|
+
NestedPost = Class.new(Model)
|
4
|
+
class NestedPostSerializer < ActiveModel::Serializer
|
5
|
+
has_many :nested_posts
|
6
|
+
end
|
7
|
+
|
2
8
|
module ActiveModel
|
3
9
|
class Serializer
|
4
|
-
|
10
|
+
module Adapter
|
5
11
|
class JsonApi
|
6
|
-
class LinkedTest <
|
12
|
+
class LinkedTest < ActiveSupport::TestCase
|
7
13
|
def setup
|
8
|
-
ActionController::Base.cache_store.clear
|
9
14
|
@author1 = Author.new(id: 1, name: 'Steve K.')
|
10
15
|
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
11
16
|
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
@@ -40,7 +45,7 @@ module ActiveModel
|
|
40
45
|
end
|
41
46
|
|
42
47
|
def test_include_multiple_posts_and_linked_array
|
43
|
-
serializer =
|
48
|
+
serializer = CollectionSerializer.new([@first_post, @second_post])
|
44
49
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
45
50
|
serializer,
|
46
51
|
include: [:comments, author: [:bio]]
|
@@ -221,7 +226,7 @@ module ActiveModel
|
|
221
226
|
end
|
222
227
|
|
223
228
|
def test_multiple_references_to_same_resource
|
224
|
-
serializer =
|
229
|
+
serializer = CollectionSerializer.new([@first_comment, @second_comment])
|
225
230
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
|
226
231
|
serializer,
|
227
232
|
include: [:post]
|
@@ -277,6 +282,112 @@ module ActiveModel
|
|
277
282
|
assert_equal expected, adapter.serializable_hash
|
278
283
|
end
|
279
284
|
end
|
285
|
+
|
286
|
+
class NoDuplicatesTest < ActiveSupport::TestCase
|
287
|
+
Post = Class.new(::Model)
|
288
|
+
Author = Class.new(::Model)
|
289
|
+
|
290
|
+
class PostSerializer < ActiveModel::Serializer
|
291
|
+
type 'posts'
|
292
|
+
belongs_to :author
|
293
|
+
end
|
294
|
+
|
295
|
+
class AuthorSerializer < ActiveModel::Serializer
|
296
|
+
type 'authors'
|
297
|
+
has_many :posts
|
298
|
+
end
|
299
|
+
|
300
|
+
def setup
|
301
|
+
@author = Author.new(id: 1, posts: [], roles: [], bio: nil)
|
302
|
+
@post1 = Post.new(id: 1, author: @author)
|
303
|
+
@post2 = Post.new(id: 2, author: @author)
|
304
|
+
@author.posts << @post1
|
305
|
+
@author.posts << @post2
|
306
|
+
|
307
|
+
@nestedpost1 = ::NestedPost.new(id: 1, nested_posts: [])
|
308
|
+
@nestedpost2 = ::NestedPost.new(id: 2, nested_posts: [])
|
309
|
+
@nestedpost1.nested_posts << @nestedpost1
|
310
|
+
@nestedpost1.nested_posts << @nestedpost2
|
311
|
+
@nestedpost2.nested_posts << @nestedpost1
|
312
|
+
@nestedpost2.nested_posts << @nestedpost2
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_no_duplicates
|
316
|
+
hash = ActiveModel::SerializableResource.new(@post1, adapter: :json_api,
|
317
|
+
include: '*.*')
|
318
|
+
.serializable_hash
|
319
|
+
expected = [
|
320
|
+
{
|
321
|
+
type: 'authors', id: '1',
|
322
|
+
relationships: {
|
323
|
+
posts: {
|
324
|
+
data: [
|
325
|
+
{ type: 'posts', id: '1' },
|
326
|
+
{ type: 'posts', id: '2' }
|
327
|
+
]
|
328
|
+
}
|
329
|
+
}
|
330
|
+
},
|
331
|
+
{
|
332
|
+
type: 'posts', id: '2',
|
333
|
+
relationships: {
|
334
|
+
author: {
|
335
|
+
data: { type: 'authors', id: '1' }
|
336
|
+
}
|
337
|
+
}
|
338
|
+
}
|
339
|
+
]
|
340
|
+
assert_equal(expected, hash[:included])
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_no_duplicates_collection
|
344
|
+
hash = ActiveModel::SerializableResource.new(
|
345
|
+
[@post1, @post2], adapter: :json_api,
|
346
|
+
include: '*.*')
|
347
|
+
.serializable_hash
|
348
|
+
expected = [
|
349
|
+
{
|
350
|
+
type: 'authors', id: '1',
|
351
|
+
relationships: {
|
352
|
+
posts: {
|
353
|
+
data: [
|
354
|
+
{ type: 'posts', id: '1' },
|
355
|
+
{ type: 'posts', id: '2' }
|
356
|
+
]
|
357
|
+
}
|
358
|
+
}
|
359
|
+
}
|
360
|
+
]
|
361
|
+
assert_equal(expected, hash[:included])
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_no_duplicates_global
|
365
|
+
hash = ActiveModel::SerializableResource.new(
|
366
|
+
@nestedpost1,
|
367
|
+
adapter: :json_api,
|
368
|
+
include: '*').serializable_hash
|
369
|
+
expected = [
|
370
|
+
type: 'nested_posts', id: '2',
|
371
|
+
relationships: {
|
372
|
+
nested_posts: {
|
373
|
+
data: [
|
374
|
+
{ type: 'nested_posts', id: '1' },
|
375
|
+
{ type: 'nested_posts', id: '2' }
|
376
|
+
]
|
377
|
+
}
|
378
|
+
}
|
379
|
+
]
|
380
|
+
assert_equal(expected, hash[:included])
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_no_duplicates_collection_global
|
384
|
+
hash = ActiveModel::SerializableResource.new(
|
385
|
+
[@nestedpost1, @nestedpost2],
|
386
|
+
adapter: :json_api,
|
387
|
+
include: '*').serializable_hash
|
388
|
+
assert_nil(hash[:included])
|
389
|
+
end
|
390
|
+
end
|
280
391
|
end
|
281
392
|
end
|
282
393
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonApi
|
7
|
+
class LinksTest < ActiveSupport::TestCase
|
8
|
+
LinkAuthor = Class.new(::Model)
|
9
|
+
class LinkAuthorSerializer < ActiveModel::Serializer
|
10
|
+
link :self do
|
11
|
+
href "//example.com/link_author/#{object.id}"
|
12
|
+
meta stuff: 'value'
|
13
|
+
end
|
14
|
+
|
15
|
+
link :other, '//example.com/resource'
|
16
|
+
|
17
|
+
link :yet_another do
|
18
|
+
"//example.com/resource/#{object.id}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def setup
|
23
|
+
@post = Post.new(id: 1337, comments: [], author: nil)
|
24
|
+
@author = LinkAuthor.new(id: 1337)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_toplevel_links
|
28
|
+
hash = ActiveModel::SerializableResource.new(
|
29
|
+
@post,
|
30
|
+
adapter: :json_api,
|
31
|
+
links: {
|
32
|
+
self: {
|
33
|
+
href: '//example.com/posts',
|
34
|
+
meta: {
|
35
|
+
stuff: 'value'
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}).serializable_hash
|
39
|
+
expected = {
|
40
|
+
self: {
|
41
|
+
href: '//example.com/posts',
|
42
|
+
meta: {
|
43
|
+
stuff: 'value'
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
assert_equal(expected, hash[:links])
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_resource_links
|
51
|
+
hash = serializable(@author, adapter: :json_api).serializable_hash
|
52
|
+
expected = {
|
53
|
+
self: {
|
54
|
+
href: '//example.com/link_author/1337',
|
55
|
+
meta: {
|
56
|
+
stuff: 'value'
|
57
|
+
}
|
58
|
+
},
|
59
|
+
other: '//example.com/resource',
|
60
|
+
yet_another: '//example.com/resource/1337'
|
61
|
+
}
|
62
|
+
assert_equal(expected, hash[:data][:links])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -6,9 +6,9 @@ require 'kaminari/hooks'
|
|
6
6
|
|
7
7
|
module ActiveModel
|
8
8
|
class Serializer
|
9
|
-
|
9
|
+
module Adapter
|
10
10
|
class JsonApi
|
11
|
-
class PaginationLinksTest <
|
11
|
+
class PaginationLinksTest < ActiveSupport::TestCase
|
12
12
|
URI = 'http://example.com'
|
13
13
|
|
14
14
|
def setup
|
@@ -22,10 +22,10 @@ module ActiveModel
|
|
22
22
|
|
23
23
|
def mock_request(query_parameters = {}, original_url = URI)
|
24
24
|
context = Minitest::Mock.new
|
25
|
-
context.expect(:
|
25
|
+
context.expect(:request_url, original_url)
|
26
26
|
context.expect(:query_parameters, query_parameters)
|
27
27
|
@options = {}
|
28
|
-
@options[:
|
28
|
+
@options[:serialization_context] = context
|
29
29
|
end
|
30
30
|
|
31
31
|
def load_adapter(paginated_collection, options = {})
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
module Deserialization
|
7
|
+
class ParseTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@hash = {
|
10
|
+
'data' => {
|
11
|
+
'type' => 'photos',
|
12
|
+
'id' => 'zorglub',
|
13
|
+
'attributes' => {
|
14
|
+
'title' => 'Ember Hamster',
|
15
|
+
'src' => 'http://example.com/images/productivity.png'
|
16
|
+
},
|
17
|
+
'relationships' => {
|
18
|
+
'author' => {
|
19
|
+
'data' => nil
|
20
|
+
},
|
21
|
+
'photographer' => {
|
22
|
+
'data' => { 'type' => 'people', 'id' => '9' }
|
23
|
+
},
|
24
|
+
'comments' => {
|
25
|
+
'data' => [
|
26
|
+
{ 'type' => 'comments', 'id' => '1' },
|
27
|
+
{ 'type' => 'comments', 'id' => '2' }
|
28
|
+
]
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
@params = ActionController::Parameters.new(@hash)
|
34
|
+
@expected = {
|
35
|
+
id: 'zorglub',
|
36
|
+
title: 'Ember Hamster',
|
37
|
+
src: 'http://example.com/images/productivity.png',
|
38
|
+
author_id: nil,
|
39
|
+
photographer_id: '9',
|
40
|
+
comment_ids: %w(1 2)
|
41
|
+
}
|
42
|
+
|
43
|
+
@illformed_payloads = [nil,
|
44
|
+
{},
|
45
|
+
{
|
46
|
+
'data' => nil
|
47
|
+
}, {
|
48
|
+
'data' => { 'attributes' => [] }
|
49
|
+
}, {
|
50
|
+
'data' => { 'relationships' => [] }
|
51
|
+
}, {
|
52
|
+
'data' => {
|
53
|
+
'relationships' => { 'rel' => nil }
|
54
|
+
}
|
55
|
+
}, {
|
56
|
+
'data' => {
|
57
|
+
'relationships' => { 'rel' => {} }
|
58
|
+
}
|
59
|
+
}]
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_hash
|
63
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash)
|
64
|
+
assert_equal(@expected, parsed_hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_actioncontroller_parameters
|
68
|
+
assert_equal(false, @params.permitted?)
|
69
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@params)
|
70
|
+
assert_equal(@expected, parsed_hash)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_illformed_payloads_safe
|
74
|
+
@illformed_payloads.each do |p|
|
75
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse(p)
|
76
|
+
assert_equal({}, parsed_hash)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_illformed_payloads_unsafe
|
81
|
+
@illformed_payloads.each do |p|
|
82
|
+
assert_raises(InvalidDocument) do
|
83
|
+
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(p)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_filter_fields_only
|
89
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, only: [:id, :title, :author])
|
90
|
+
expected = {
|
91
|
+
id: 'zorglub',
|
92
|
+
title: 'Ember Hamster',
|
93
|
+
author_id: nil
|
94
|
+
}
|
95
|
+
assert_equal(expected, parsed_hash)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_filter_fields_except
|
99
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, except: [:id, :title, :author])
|
100
|
+
expected = {
|
101
|
+
src: 'http://example.com/images/productivity.png',
|
102
|
+
photographer_id: '9',
|
103
|
+
comment_ids: %w(1 2)
|
104
|
+
}
|
105
|
+
assert_equal(expected, parsed_hash)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_keys
|
109
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, keys: { author: :user, title: :post_title })
|
110
|
+
expected = {
|
111
|
+
id: 'zorglub',
|
112
|
+
post_title: 'Ember Hamster',
|
113
|
+
src: 'http://example.com/images/productivity.png',
|
114
|
+
user_id: nil,
|
115
|
+
photographer_id: '9',
|
116
|
+
comment_ids: %w(1 2)
|
117
|
+
}
|
118
|
+
assert_equal(expected, parsed_hash)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_polymorphic
|
122
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, polymorphic: [:photographer])
|
123
|
+
expected = {
|
124
|
+
id: 'zorglub',
|
125
|
+
title: 'Ember Hamster',
|
126
|
+
src: 'http://example.com/images/productivity.png',
|
127
|
+
author_id: nil,
|
128
|
+
photographer_id: '9',
|
129
|
+
photographer_type: 'people',
|
130
|
+
comment_ids: %w(1 2)
|
131
|
+
}
|
132
|
+
assert_equal(expected, parsed_hash)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|