grape_fast_jsonapi 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23959b13a1fe539f5657f5e4da15712f2da6c2010c05e415426523d419831c60
4
- data.tar.gz: a5ebffa2ce6617034be70fc2a52f7a876d7d894d52ab2e865036060bf3934462
3
+ metadata.gz: 1b2ef04c9a3d2b69160a93779d60466a90a93fed1bf1109e1e32c91e8632bad8
4
+ data.tar.gz: 1ba50c7bdf05a933b7c736f2a4f36e55f5bc3d4742bb2d552260760eb63dffe2
5
5
  SHA512:
6
- metadata.gz: 44fd2eb2f433d265376c4e70b3d4a2a10b211a95f8251f041c1a6e0bc3825eaf4d0efcb1977a9e08cf77e479ca81f5d9fba1214b7afc925b018f7d87d74c74e1
7
- data.tar.gz: 842921a39b89166e57d0286db7c6dfee887821251089c6d57a5050856582cd52c06e698544ccd3740f805a7322aba0e09020fe9cfc348a9115b80750f5854fc2
6
+ metadata.gz: d5175b87b19aca5fd04a5532d901ff03f41c06f1792de26f15b0c452f5e8a3357a0237a3ad6c835bb19da20cb3b09133e08b093e3680837307e3fa23d57b1317
7
+ data.tar.gz: 148a701839831d1b6b384b93ceddcfe07799eef6b233ec7e8df449711e94a2b4cf673cc859ab8760a96f4d20d85b4ff3c16d487c63489fac589eac3e88485c8e
data/CHANGELOG.md CHANGED
@@ -1,14 +1,21 @@
1
1
  ## Changelog
2
2
 
3
- ### v0.2.2 (Next)
3
+ ### v0.2.4 (Next)
4
4
 
5
5
  * Your contribution here.
6
6
 
7
+ ### v0.2.3 (December 12, 2019)
8
+
9
+ * Reverted v0.2.2 and bumped `loofah` using `dependabot`
10
+
11
+ ### v0.2.2 (December 12, 2019)
12
+
13
+ * Fixed low severity vulnerabiliy issue with `loofah` dependency
14
+
7
15
  ### v0.2.1 (September 18, 2019)
8
16
 
9
17
  * [#12](https://github.com/EmCousin/grape_fast_jsonapi/pull/12) - Removed call to `rails` and fixed a potential security issue
10
18
 
11
-
12
19
  ### v0.2.0 (February 8, 2019)
13
20
 
14
21
  * [#5](https://github.com/EmCousin/grape_fast_jsonapi/pull/5): Provide custom Grape Swagger parser for fast_jsonapi object serializers, as well as unit test coverage - [@EmCousin](https://github.com/EmCousin)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape_fast_jsonapi (0.2.1)
4
+ grape_fast_jsonapi (0.2.3)
5
5
  fast_jsonapi (>= 1.5)
6
6
  grape
7
7
 
@@ -71,7 +71,7 @@ GEM
71
71
  coercible (1.0.0)
72
72
  descendants_tracker (~> 0.0.1)
73
73
  concurrent-ruby (1.1.5)
74
- crass (1.0.4)
74
+ crass (1.0.5)
75
75
  descendants_tracker (0.0.4)
76
76
  thread_safe (~> 0.3, >= 0.3.1)
77
77
  diff-lcs (1.3)
@@ -91,7 +91,7 @@ GEM
91
91
  i18n (1.6.0)
92
92
  concurrent-ruby (~> 1.0)
93
93
  ice_nine (0.11.2)
94
- loofah (2.2.3)
94
+ loofah (2.3.1)
95
95
  crass (~> 1.0.2)
96
96
  nokogiri (>= 1.5.9)
97
97
  mail (2.7.1)
@@ -107,7 +107,7 @@ GEM
107
107
  mustermann-grape (1.0.0)
108
108
  mustermann (~> 1.0.0)
109
109
  nio4r (2.5.1)
110
- nokogiri (1.10.4)
110
+ nokogiri (1.10.5)
111
111
  mini_portile2 (~> 2.4.0)
112
112
  rack (2.0.7)
113
113
  rack-accept (0.4.5)
data/README.md CHANGED
@@ -32,6 +32,24 @@ get "/" do
32
32
  end
33
33
  ```
34
34
 
35
+ ### Use a custom serializer
36
+
37
+ ```ruby
38
+ get "/" do
39
+ user = User.find("123")
40
+ render user, serializer: 'CustomUserSerializer'
41
+ end
42
+ ```
43
+
44
+ Or
45
+
46
+ ```ruby
47
+ get "/" do
48
+ user = User.find("123")
49
+ render CustomUserSerializer.new(user).serialized_json
50
+ end
51
+ ```
52
+
35
53
  ### Model parser for response documentation
36
54
 
37
55
  When using Grape with Swagger via [grape-swagger](https://github.com/ruby-grape/grape-swagger), you can generate response documentation automatically via the provided following model parser:
@@ -36,22 +36,32 @@ module Grape
36
36
  end
37
37
 
38
38
  def fast_jsonapi_serializable(object, options)
39
- serializable_class(object)&.new(object, options)
39
+ serializable_class(object, options)&.new(object, options)
40
40
  end
41
41
 
42
42
  def serializable_collection(collection, options)
43
- if collection.map(&:model_name).uniq.count > 1
43
+ if heterogeneous_collection?(collection)
44
44
  collection.map do |o|
45
45
  fast_jsonapi_serializable(o, options).serializable_hash || o.map(&:serializable_hash)
46
46
  end
47
47
  else
48
- fast_jsonapi_serializable(collection, options).serializable_hash || collection.map(&:serializable_hash)
48
+ fast_jsonapi_serializable(collection, options)&.serializable_hash || collection.map(&:serializable_hash)
49
49
  end
50
50
  end
51
51
 
52
- def serializable_class(object)
53
- object = object.first if object.is_a?(Array)
54
- (object.model_name.name + 'Serializer').safe_constantize
52
+ def heterogeneous_collection?(collection)
53
+ collection.map { |item| item.class.name }.uniq.size > 1
54
+ end
55
+
56
+ def serializable_class(object, options)
57
+ klass_name = options['serializer'] || options[:serializer]
58
+ klass_name ||= begin
59
+ object = object.first if object.is_a?(Array)
60
+
61
+ object.class.name + 'Serializer'
62
+ end
63
+
64
+ klass_name&.safe_constantize
55
65
  end
56
66
 
57
67
  def serialize_each_pair(object, env)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Grape
4
4
  module FastJsonapi
5
- VERSION = '0.2.1'.freeze
5
+ VERSION = '0.2.3'.freeze
6
6
  end
7
7
  end
@@ -14,7 +14,8 @@ describe Grape::Formatter::FastJsonapi do
14
14
 
15
15
  describe '.call' do
16
16
  subject { described_class.call(object, env) }
17
- let(:env) { {} }
17
+ let(:fast_jsonapi_options) { nil }
18
+ let(:env) { { 'fast_jsonapi_options' => fast_jsonapi_options } }
18
19
 
19
20
  context 'when the object is a string' do
20
21
  let(:object) { "I am a string" }
@@ -24,6 +25,7 @@ describe Grape::Formatter::FastJsonapi do
24
25
 
25
26
  context 'when the object is serializable' do
26
27
  let(:user_serializer) { UserSerializer.new(object, {}) }
28
+ let(:another_user_serializer) { AnotherUserSerializer.new(object, {}) }
27
29
  let(:blog_post_serializer) { BlogPostSerializer.new(object, {}) }
28
30
 
29
31
  context 'when the object is a active serializable model instance' do
@@ -36,19 +38,31 @@ describe Grape::Formatter::FastJsonapi do
36
38
  let(:object) { [user, another_user] }
37
39
 
38
40
  it { is_expected.to eq ::Grape::Json.dump(user_serializer.serializable_hash) }
41
+ end
39
42
 
40
- context "when the array contains instances of different models" do
41
- let(:object) { [user, blog_post] }
43
+ context "when the array contains instances of different models" do
44
+ let(:object) { [user, blog_post] }
42
45
 
43
- it 'returns an array of jsonapi serialialized objects' do
44
- expect(subject).to eq(::Grape::Json.dump([
45
- UserSerializer.new(user, {}).serializable_hash,
46
- BlogPostSerializer.new(blog_post, {}).serializable_hash
47
- ]))
48
- end
46
+ it 'returns an array of jsonapi serialialized objects' do
47
+ expect(subject).to eq(::Grape::Json.dump([
48
+ UserSerializer.new(user, {}).serializable_hash,
49
+ BlogPostSerializer.new(blog_post, {}).serializable_hash
50
+ ]))
49
51
  end
50
52
  end
51
53
 
54
+ context 'when the object is an empty array ' do
55
+ let(:object) { [] }
56
+
57
+ it { is_expected.to eq ::Grape::Json.dump(object) }
58
+ end
59
+
60
+ context 'when the object is an array of null objects ' do
61
+ let(:object) { [nil, nil] }
62
+
63
+ it { is_expected.to eq ::Grape::Json.dump(object) }
64
+ end
65
+
52
66
  context 'when the object is a Hash of plain values' do
53
67
  let(:object) { user.as_json }
54
68
 
@@ -82,6 +96,15 @@ describe Grape::Formatter::FastJsonapi do
82
96
 
83
97
  it { is_expected.to eq '42' }
84
98
  end
99
+
100
+ context 'when a custom serializer is passed as an option' do
101
+ let(:object) { user }
102
+ let(:fast_jsonapi_options) { {
103
+ 'serializer' => '::AnotherUserSerializer'
104
+ } }
105
+
106
+ it { is_expected.to eq ::Grape::Json.dump(another_user_serializer.serializable_hash) }
107
+ end
85
108
  end
86
109
  end
87
110
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe Grape::FastJsonapi::VERSION do
4
- it { is_expected.to eq '0.2.1' }
4
+ it { is_expected.to eq '0.2.3' }
5
5
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class BlogPost
4
- extend ActiveModel::Naming
5
4
  include ActiveModel::Serialization
6
5
 
7
6
  attr_accessor :id, :title, :body
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class User
4
- extend ActiveModel::Naming
5
4
  include ActiveModel::Serialization
6
5
 
7
6
  attr_accessor :id, :first_name, :last_name, :password, :email
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AnotherUserSerializer
4
+ include FastJsonapi::ObjectSerializer
5
+
6
+ attributes :email
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_fast_jsonapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Cousin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-18 00:00:00.000000000 Z
11
+ date: 2019-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -94,6 +94,7 @@ files:
94
94
  - spec/spec_helper.rb
95
95
  - spec/support/models/blog_post.rb
96
96
  - spec/support/models/user.rb
97
+ - spec/support/serializers/another_user_serializer.rb
97
98
  - spec/support/serializers/blog_post_serializer.rb
98
99
  - spec/support/serializers/user_serializer.rb
99
100
  homepage: https://github.com/EmCousin/grape_fast_jsonapi
@@ -126,5 +127,6 @@ test_files:
126
127
  - spec/spec_helper.rb
127
128
  - spec/support/models/blog_post.rb
128
129
  - spec/support/models/user.rb
130
+ - spec/support/serializers/another_user_serializer.rb
129
131
  - spec/support/serializers/blog_post_serializer.rb
130
132
  - spec/support/serializers/user_serializer.rb