jsonapi-serializers 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -11
- data/lib/jsonapi-serializers/serializer.rb +2 -5
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +38 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d89dc4f009841a5431c7e79060da4208c2d7dde2
|
4
|
+
data.tar.gz: 1eb85ed60e4d91ba8f33ed28a8151ac3ff21cf71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa10630f9a9181a194e831cf538d65c7fcafa910ee7054e3838efbe30ef1775eb447b4f4eb84c4900d9fa4a99a539caf9df6d47f0e8db3299a0b4116ec0d9c08
|
7
|
+
data.tar.gz: c8d6f28e77762831850999491d2f05237ed0a4c2fd4215a09d88b7d8ccdefa2732955da77ebe1009bbc43cf6270e659ffb830f1544880c234783db0495c20767
|
data/README.md
CHANGED
@@ -543,12 +543,19 @@ end
|
|
543
543
|
|
544
544
|
## Sinatra example
|
545
545
|
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
546
|
+
Here's an example using [Sinatra](http://www.sinatrarb.com) and
|
547
|
+
[Sequel ORM](http://sequel.jeremyevans.net) instead of Rails and ActiveRecord.
|
548
|
+
The important takeaways here are that:
|
549
|
+
|
550
|
+
1. The `:tactical_eager_loading` plugin will greatly reduce the number of
|
551
|
+
queries performed when sideloading associated records. You can add this
|
552
|
+
plugin to a single model (as demonstrated here), or globally to all models.
|
553
|
+
For more information, please see the Sequel
|
554
|
+
[documentation](http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/TacticalEagerLoading.html).
|
555
|
+
1. The `:skip_collection_check` option must be set to true in order for
|
556
|
+
JSONAPI::Serializer to be able to serialize a single Sequel::Model instance.
|
557
|
+
1. You should call `#all` on your Sequel::Dataset instances before passing them
|
558
|
+
to JSONAPI::Serializer to greatly reduce the number of queries performed.
|
552
559
|
|
553
560
|
```ruby
|
554
561
|
require 'sequel'
|
@@ -575,15 +582,13 @@ class BaseSerializer
|
|
575
582
|
end
|
576
583
|
|
577
584
|
class PostSerializer < BaseSerializer
|
578
|
-
|
579
|
-
attribute :content
|
585
|
+
attributes :title, :content
|
580
586
|
|
581
587
|
has_many :comments
|
582
588
|
end
|
583
589
|
|
584
590
|
class CommentSerializer < BaseSerializer
|
585
|
-
|
586
|
-
attribute :body
|
591
|
+
attributes :username, :content
|
587
592
|
|
588
593
|
has_one :post
|
589
594
|
end
|
@@ -642,12 +647,12 @@ end
|
|
642
647
|
## Unfinished business
|
643
648
|
|
644
649
|
* Support for passing `context` through to serializers is partially complete, but needs more work.
|
645
|
-
* Support for a `serializer_class` attribute on objects that overrides serializer discovery, would love a PR contribution for this.
|
646
650
|
* Support for the `fields` spec is planned, would love a PR contribution for this.
|
647
651
|
* Support for pagination/sorting is unlikely to be supported because it would likely involve coupling to ActiveRecord, but please open an issue if you have ideas of how to support this generically.
|
648
652
|
|
649
653
|
## Release notes
|
650
654
|
|
655
|
+
* v0.6.1: Spec compliance fix, include intermediate resources on compound documents.
|
651
656
|
* v0.6.0: Support for polymorphic collections and inheriting serializer attributes.
|
652
657
|
* v0.5.0: Support for explicit serializer discovery.
|
653
658
|
* v0.4.0: Support for declaring multiple `attributes`.
|
@@ -466,12 +466,9 @@ module JSONAPI
|
|
466
466
|
def self.merge_relationship_path(path, data)
|
467
467
|
parts = path.split('.', 2)
|
468
468
|
current_level = parts[0].strip
|
469
|
-
data[current_level] ||= {}
|
469
|
+
data[current_level] ||= {_include: true}
|
470
470
|
|
471
|
-
if parts.length ==
|
472
|
-
# Leaf node.
|
473
|
-
data[current_level].merge!({_include: true})
|
474
|
-
elsif parts.length == 2
|
471
|
+
if parts.length == 2
|
475
472
|
# Need to recurse more.
|
476
473
|
merge_relationship_path(parts[1], data[current_level])
|
477
474
|
end
|
data/spec/serializer_spec.rb
CHANGED
@@ -531,23 +531,23 @@ describe JSONAPI::Serializer do
|
|
531
531
|
long_comments.each { |c| c.post = post }
|
532
532
|
|
533
533
|
expected_data = {
|
534
|
-
# Note that in this case the primary data does not include linkage for 'long-comments',
|
535
|
-
# forcing clients to still have to request linkage from long-comments and post. This is an
|
536
|
-
# odd but valid data state because the user requested to only include the leaf author node,
|
537
|
-
# and we only automatically expose direct children linkages if they match given includes.
|
538
|
-
#
|
539
|
-
# Spec: Resource linkage in a compound document allows a client to link together
|
540
|
-
# all of the included resource objects without having to GET any relationship URLs.
|
541
|
-
# http://jsonapi.org/format/#document-structure-resource-relationships
|
542
|
-
#
|
543
|
-
# Also, spec: A request for comments.author should not automatically also include
|
544
|
-
# comments in the response. This can happen if the client already has the comments locally,
|
545
|
-
# and now wants to fetch the associated authors without fetching the comments again.
|
546
|
-
# http://jsonapi.org/format/#fetching-includes
|
547
534
|
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
548
535
|
'included' => [
|
549
|
-
#
|
550
|
-
|
536
|
+
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
|
537
|
+
# http://jsonapi.org/format/#document-structure-compound-documents
|
538
|
+
serialize_primary(post.long_comments.first, {
|
539
|
+
serializer: MyApp::LongCommentSerializer,
|
540
|
+
include_linkages: ['post']
|
541
|
+
}),
|
542
|
+
serialize_primary(post.long_comments.last, {
|
543
|
+
serializer: MyApp::LongCommentSerializer,
|
544
|
+
include_linkages: ['post']
|
545
|
+
}),
|
546
|
+
serialize_primary(post, {
|
547
|
+
serializer: MyApp::PostSerializer,
|
548
|
+
include_linkages: ['author', 'post.long-comments', ]
|
549
|
+
}),
|
550
|
+
serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer})
|
551
551
|
],
|
552
552
|
}
|
553
553
|
includes = ['long-comments.post.author']
|
@@ -568,13 +568,21 @@ describe JSONAPI::Serializer do
|
|
568
568
|
long_comments.each { |c| c.post = post }
|
569
569
|
|
570
570
|
expected_data = {
|
571
|
-
# Same note about primary data linkages as above.
|
572
571
|
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
573
572
|
'included' => [
|
573
|
+
serialize_primary(first_comment, {
|
574
|
+
serializer: MyApp::LongCommentSerializer,
|
575
|
+
include_linkages: ['user']
|
576
|
+
}),
|
577
|
+
serialize_primary(second_comment, {
|
578
|
+
serializer: MyApp::LongCommentSerializer,
|
579
|
+
include_linkages: ['user']
|
580
|
+
}),
|
574
581
|
serialize_primary(first_user, {serializer: MyAppOtherNamespace::UserSerializer}),
|
575
582
|
serialize_primary(second_user, {serializer: MyAppOtherNamespace::UserSerializer}),
|
576
583
|
],
|
577
584
|
}
|
585
|
+
|
578
586
|
includes = ['long-comments.user']
|
579
587
|
actual_data = JSONAPI::Serializer.serialize(post, include: includes)
|
580
588
|
|
@@ -625,8 +633,18 @@ describe JSONAPI::Serializer do
|
|
625
633
|
expected_data = {
|
626
634
|
'data' => expected_primary_data,
|
627
635
|
'included' => [
|
628
|
-
serialize_primary(long_comments.first, {
|
629
|
-
|
636
|
+
serialize_primary(long_comments.first, {
|
637
|
+
serializer: MyApp::LongCommentSerializer,
|
638
|
+
include_linkages: ['post'],
|
639
|
+
}),
|
640
|
+
serialize_primary(long_comments.last, {
|
641
|
+
serializer: MyApp::LongCommentSerializer,
|
642
|
+
include_linkages: ['post'],
|
643
|
+
}),
|
644
|
+
serialize_primary(post, {
|
645
|
+
serializer: MyApp::PostSerializer,
|
646
|
+
include_linkages: ['author'],
|
647
|
+
}),
|
630
648
|
serialize_primary(post.author, {serializer: MyAppOtherNamespace::UserSerializer}),
|
631
649
|
],
|
632
650
|
}
|
@@ -684,7 +702,7 @@ describe JSONAPI::Serializer do
|
|
684
702
|
it 'correctly handles multi-level relationship paths' do
|
685
703
|
result = JSONAPI::Serializer.send(:parse_relationship_paths, ['foo.bar'])
|
686
704
|
expect(result).to eq({
|
687
|
-
'foo' => {'bar' => {_include: true}}
|
705
|
+
'foo' => {_include: true, 'bar' => {_include: true}}
|
688
706
|
})
|
689
707
|
end
|
690
708
|
it 'correctly handles multi-level relationship paths with same parent' do
|
@@ -713,7 +731,7 @@ describe JSONAPI::Serializer do
|
|
713
731
|
paths = ['foo', 'foo.bar.baz']
|
714
732
|
result = JSONAPI::Serializer.send(:parse_relationship_paths, paths)
|
715
733
|
expect(result).to eq({
|
716
|
-
'foo' => {_include: true, 'bar' => {'baz' => {_include: true}}}
|
734
|
+
'foo' => {_include: true, 'bar' => {_include: true, 'baz' => {_include: true}}}
|
717
735
|
})
|
718
736
|
end
|
719
737
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fotinakis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|