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 +4 -4
- data/CHANGELOG.md +9 -2
- data/Gemfile.lock +4 -4
- data/README.md +18 -0
- data/lib/grape_fast_jsonapi/formatter.rb +16 -6
- data/lib/grape_fast_jsonapi/version.rb +1 -1
- data/spec/lib/grape_fast_jsonapi/formatter_spec.rb +32 -9
- data/spec/lib/grape_fast_jsonapi/version_spec.rb +1 -1
- data/spec/support/models/blog_post.rb +0 -1
- data/spec/support/models/user.rb +0 -1
- data/spec/support/serializers/another_user_serializer.rb +7 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b2ef04c9a3d2b69160a93779d60466a90a93fed1bf1109e1e32c91e8632bad8
|
4
|
+
data.tar.gz: 1ba50c7bdf05a933b7c736f2a4f36e55f5bc3d4742bb2d552260760eb63dffe2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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.
|
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.
|
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.
|
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.
|
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
|
-
|
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)
|
48
|
+
fast_jsonapi_serializable(collection, options)&.serializable_hash || collection.map(&:serializable_hash)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
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)
|
@@ -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(:
|
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
|
-
|
41
|
-
|
43
|
+
context "when the array contains instances of different models" do
|
44
|
+
let(:object) { [user, blog_post] }
|
42
45
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
data/spec/support/models/user.rb
CHANGED
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.
|
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-
|
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
|