grape-jsonapi 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -1
- data/Gemfile.lock +81 -78
- data/LICENSE +1 -1
- data/README.md +12 -3
- data/lib/grape_jsonapi/formatter.rb +24 -16
- data/lib/grape_jsonapi/version.rb +1 -1
- data/spec/lib/grape_jsonapi/formatter_spec.rb +71 -42
- data/spec/lib/grape_jsonapi/version_spec.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 03c4961342732b83760ea4e20cd449d09f24002bb1645547f7199b4d12aa3b92
|
|
4
|
+
data.tar.gz: fd68faabd39cbf1332c80b0594a072f3a87b0fa8b58a667ab7ab2f40df4cadc4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad33df912cd3ac52fcdb8fedc7b8189d02d51121d2d09edf5f6b2a6bea7bb38a5860fb692f30e2e35d6bc09aad898e486165f573af345f037e92c8c3dec8d77e
|
|
7
|
+
data.tar.gz: 1ce5157fcdcfce58aa55abe37d72708fbe414b86d67c9dbde31b9ca118eb3cee53b10b99dc1a07815468ce400995602b6d3523abf3cbb496d3dcdaa3509df801
|
data/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
|
|
3
|
-
### v1.0.
|
|
3
|
+
### v1.0.2 (next)
|
|
4
4
|
|
|
5
5
|
* Your contribution here.
|
|
6
6
|
|
|
7
|
+
### v1.0.1 (January 25, 2022)
|
|
8
|
+
|
|
9
|
+
[#32](https://github.com/EmCousin/grape-jsonapi/pull/32) - [@EmCousin](https://github.com/EmCousin)
|
|
10
|
+
|
|
11
|
+
* The gem now forces API response to have a JSONAPI compliant format, even for objects that are not being serialized via a `JSONAPI::Serializer`
|
|
12
|
+
* You can now customize the `meta` and `links` properties of your response at rendering time, without having to rely on your serializers (check README.md for more information)
|
|
13
|
+
* Changed the response's data structure when the object is a heterogeneous collection (a list of objects of different classes), to make it JSONAPI compliant.
|
|
14
|
+
* Fixed a defect that was causing empty hashes to be rendered as empty arrays
|
|
15
|
+
|
|
7
16
|
### v1.0.0 (November 21, 2020)
|
|
8
17
|
|
|
9
18
|
[#14](https://github.com/EmCousin/grape_fast_jsonapi/pull/14) - [@EmCousin](https://github.com/EmCousin)
|
|
10
19
|
|
|
20
|
+
* renamed gem from `grape_fast_jsonapi` to `grape-jsonapi`
|
|
11
21
|
* Changed dependency from [fast_jsonapi](https://github.com/Netflix/fast_jsonapi) to [jsonapi-serializer](https://github.com/jsonapi-serializer/jsonapi-serializer)
|
|
12
22
|
* Deprecated `Grape::Formatter::FastJsonapi` and `Grape::FastJsonapi::Parser` in favor to `Grape::Formatter::Jsonapi` and `Grape::Jsonapi::Parser`. Will be removed in v1.1
|
|
13
23
|
* Fixed bugs due to breaking changes caused by the switch
|
data/Gemfile.lock
CHANGED
|
@@ -8,64 +8,68 @@ PATH
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
actioncable (6.
|
|
12
|
-
actionpack (= 6.
|
|
11
|
+
actioncable (6.1.3.2)
|
|
12
|
+
actionpack (= 6.1.3.2)
|
|
13
|
+
activesupport (= 6.1.3.2)
|
|
13
14
|
nio4r (~> 2.0)
|
|
14
15
|
websocket-driver (>= 0.6.1)
|
|
15
|
-
actionmailbox (6.
|
|
16
|
-
actionpack (= 6.
|
|
17
|
-
activejob (= 6.
|
|
18
|
-
activerecord (= 6.
|
|
19
|
-
activestorage (= 6.
|
|
20
|
-
activesupport (= 6.
|
|
16
|
+
actionmailbox (6.1.3.2)
|
|
17
|
+
actionpack (= 6.1.3.2)
|
|
18
|
+
activejob (= 6.1.3.2)
|
|
19
|
+
activerecord (= 6.1.3.2)
|
|
20
|
+
activestorage (= 6.1.3.2)
|
|
21
|
+
activesupport (= 6.1.3.2)
|
|
21
22
|
mail (>= 2.7.1)
|
|
22
|
-
actionmailer (6.
|
|
23
|
-
actionpack (= 6.
|
|
24
|
-
actionview (= 6.
|
|
25
|
-
activejob (= 6.
|
|
23
|
+
actionmailer (6.1.3.2)
|
|
24
|
+
actionpack (= 6.1.3.2)
|
|
25
|
+
actionview (= 6.1.3.2)
|
|
26
|
+
activejob (= 6.1.3.2)
|
|
27
|
+
activesupport (= 6.1.3.2)
|
|
26
28
|
mail (~> 2.5, >= 2.5.4)
|
|
27
29
|
rails-dom-testing (~> 2.0)
|
|
28
|
-
actionpack (6.
|
|
29
|
-
actionview (= 6.
|
|
30
|
-
activesupport (= 6.
|
|
31
|
-
rack (~> 2.0, >= 2.0.
|
|
30
|
+
actionpack (6.1.3.2)
|
|
31
|
+
actionview (= 6.1.3.2)
|
|
32
|
+
activesupport (= 6.1.3.2)
|
|
33
|
+
rack (~> 2.0, >= 2.0.9)
|
|
32
34
|
rack-test (>= 0.6.3)
|
|
33
35
|
rails-dom-testing (~> 2.0)
|
|
34
36
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
35
|
-
actiontext (6.
|
|
36
|
-
actionpack (= 6.
|
|
37
|
-
activerecord (= 6.
|
|
38
|
-
activestorage (= 6.
|
|
39
|
-
activesupport (= 6.
|
|
37
|
+
actiontext (6.1.3.2)
|
|
38
|
+
actionpack (= 6.1.3.2)
|
|
39
|
+
activerecord (= 6.1.3.2)
|
|
40
|
+
activestorage (= 6.1.3.2)
|
|
41
|
+
activesupport (= 6.1.3.2)
|
|
40
42
|
nokogiri (>= 1.8.5)
|
|
41
|
-
actionview (6.
|
|
42
|
-
activesupport (= 6.
|
|
43
|
+
actionview (6.1.3.2)
|
|
44
|
+
activesupport (= 6.1.3.2)
|
|
43
45
|
builder (~> 3.1)
|
|
44
46
|
erubi (~> 1.4)
|
|
45
47
|
rails-dom-testing (~> 2.0)
|
|
46
48
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
47
|
-
activejob (6.
|
|
48
|
-
activesupport (= 6.
|
|
49
|
+
activejob (6.1.3.2)
|
|
50
|
+
activesupport (= 6.1.3.2)
|
|
49
51
|
globalid (>= 0.3.6)
|
|
50
|
-
activemodel (6.
|
|
51
|
-
activesupport (= 6.
|
|
52
|
-
activerecord (6.
|
|
53
|
-
activemodel (= 6.
|
|
54
|
-
activesupport (= 6.
|
|
55
|
-
activestorage (6.
|
|
56
|
-
actionpack (= 6.
|
|
57
|
-
activejob (= 6.
|
|
58
|
-
activerecord (= 6.
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
activemodel (6.1.3.2)
|
|
53
|
+
activesupport (= 6.1.3.2)
|
|
54
|
+
activerecord (6.1.3.2)
|
|
55
|
+
activemodel (= 6.1.3.2)
|
|
56
|
+
activesupport (= 6.1.3.2)
|
|
57
|
+
activestorage (6.1.3.2)
|
|
58
|
+
actionpack (= 6.1.3.2)
|
|
59
|
+
activejob (= 6.1.3.2)
|
|
60
|
+
activerecord (= 6.1.3.2)
|
|
61
|
+
activesupport (= 6.1.3.2)
|
|
62
|
+
marcel (~> 1.0.0)
|
|
63
|
+
mini_mime (~> 1.0.2)
|
|
64
|
+
activesupport (6.1.3.2)
|
|
61
65
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
62
|
-
i18n (>=
|
|
63
|
-
minitest (
|
|
64
|
-
tzinfo (~>
|
|
65
|
-
zeitwerk (~> 2.
|
|
66
|
+
i18n (>= 1.6, < 2)
|
|
67
|
+
minitest (>= 5.1)
|
|
68
|
+
tzinfo (~> 2.0)
|
|
69
|
+
zeitwerk (~> 2.3)
|
|
66
70
|
ast (2.4.1)
|
|
67
71
|
builder (3.2.4)
|
|
68
|
-
concurrent-ruby (1.1.
|
|
72
|
+
concurrent-ruby (1.1.8)
|
|
69
73
|
crass (1.0.6)
|
|
70
74
|
diff-lcs (1.4.4)
|
|
71
75
|
dry-configurable (0.11.6)
|
|
@@ -100,67 +104,67 @@ GEM
|
|
|
100
104
|
mustermann-grape (~> 1.0.0)
|
|
101
105
|
rack (>= 1.3.0)
|
|
102
106
|
rack-accept
|
|
103
|
-
i18n (1.8.
|
|
107
|
+
i18n (1.8.10)
|
|
104
108
|
concurrent-ruby (~> 1.0)
|
|
105
109
|
jsonapi-serializer (2.1.0)
|
|
106
110
|
activesupport (>= 4.2)
|
|
107
|
-
loofah (2.
|
|
111
|
+
loofah (2.9.1)
|
|
108
112
|
crass (~> 1.0.2)
|
|
109
113
|
nokogiri (>= 1.5.9)
|
|
110
114
|
mail (2.7.1)
|
|
111
115
|
mini_mime (>= 0.1.1)
|
|
112
|
-
marcel (0.
|
|
113
|
-
mimemagic (~> 0.3.2)
|
|
116
|
+
marcel (1.0.1)
|
|
114
117
|
method_source (1.0.0)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
minitest (5.14.2)
|
|
118
|
+
mini_mime (1.0.3)
|
|
119
|
+
mini_portile2 (2.6.1)
|
|
120
|
+
minitest (5.14.4)
|
|
119
121
|
mustermann (1.1.1)
|
|
120
122
|
ruby2_keywords (~> 0.0.1)
|
|
121
123
|
mustermann-grape (1.0.1)
|
|
122
124
|
mustermann (>= 1.0.0)
|
|
123
|
-
nio4r (2.5.
|
|
124
|
-
nokogiri (1.
|
|
125
|
-
mini_portile2 (~> 2.
|
|
125
|
+
nio4r (2.5.7)
|
|
126
|
+
nokogiri (1.12.5)
|
|
127
|
+
mini_portile2 (~> 2.6.1)
|
|
128
|
+
racc (~> 1.4)
|
|
126
129
|
parallel (1.20.1)
|
|
127
130
|
parser (2.7.2.0)
|
|
128
131
|
ast (~> 2.4.1)
|
|
132
|
+
racc (1.5.2)
|
|
129
133
|
rack (2.2.3)
|
|
130
134
|
rack-accept (0.4.5)
|
|
131
135
|
rack (>= 0.4)
|
|
132
136
|
rack-test (1.1.0)
|
|
133
137
|
rack (>= 1.0, < 3)
|
|
134
|
-
rails (6.
|
|
135
|
-
actioncable (= 6.
|
|
136
|
-
actionmailbox (= 6.
|
|
137
|
-
actionmailer (= 6.
|
|
138
|
-
actionpack (= 6.
|
|
139
|
-
actiontext (= 6.
|
|
140
|
-
actionview (= 6.
|
|
141
|
-
activejob (= 6.
|
|
142
|
-
activemodel (= 6.
|
|
143
|
-
activerecord (= 6.
|
|
144
|
-
activestorage (= 6.
|
|
145
|
-
activesupport (= 6.
|
|
146
|
-
bundler (>= 1.
|
|
147
|
-
railties (= 6.
|
|
138
|
+
rails (6.1.3.2)
|
|
139
|
+
actioncable (= 6.1.3.2)
|
|
140
|
+
actionmailbox (= 6.1.3.2)
|
|
141
|
+
actionmailer (= 6.1.3.2)
|
|
142
|
+
actionpack (= 6.1.3.2)
|
|
143
|
+
actiontext (= 6.1.3.2)
|
|
144
|
+
actionview (= 6.1.3.2)
|
|
145
|
+
activejob (= 6.1.3.2)
|
|
146
|
+
activemodel (= 6.1.3.2)
|
|
147
|
+
activerecord (= 6.1.3.2)
|
|
148
|
+
activestorage (= 6.1.3.2)
|
|
149
|
+
activesupport (= 6.1.3.2)
|
|
150
|
+
bundler (>= 1.15.0)
|
|
151
|
+
railties (= 6.1.3.2)
|
|
148
152
|
sprockets-rails (>= 2.0.0)
|
|
149
153
|
rails-dom-testing (2.0.3)
|
|
150
154
|
activesupport (>= 4.2.0)
|
|
151
155
|
nokogiri (>= 1.6)
|
|
152
156
|
rails-html-sanitizer (1.3.0)
|
|
153
157
|
loofah (~> 2.3)
|
|
154
|
-
railties (6.
|
|
155
|
-
actionpack (= 6.
|
|
156
|
-
activesupport (= 6.
|
|
158
|
+
railties (6.1.3.2)
|
|
159
|
+
actionpack (= 6.1.3.2)
|
|
160
|
+
activesupport (= 6.1.3.2)
|
|
157
161
|
method_source
|
|
158
162
|
rake (>= 0.8.7)
|
|
159
|
-
thor (
|
|
163
|
+
thor (~> 1.0)
|
|
160
164
|
rainbow (3.0.0)
|
|
161
|
-
rake (13.0.
|
|
165
|
+
rake (13.0.3)
|
|
162
166
|
regexp_parser (2.0.0)
|
|
163
|
-
rexml (3.2.
|
|
167
|
+
rexml (3.2.5)
|
|
164
168
|
rspec (3.10.0)
|
|
165
169
|
rspec-core (~> 3.10.0)
|
|
166
170
|
rspec-expectations (~> 3.10.0)
|
|
@@ -194,15 +198,14 @@ GEM
|
|
|
194
198
|
actionpack (>= 4.0)
|
|
195
199
|
activesupport (>= 4.0)
|
|
196
200
|
sprockets (>= 3.0.0)
|
|
197
|
-
thor (1.0
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
thread_safe (~> 0.1)
|
|
201
|
+
thor (1.1.0)
|
|
202
|
+
tzinfo (2.0.4)
|
|
203
|
+
concurrent-ruby (~> 1.0)
|
|
201
204
|
unicode-display_width (1.7.0)
|
|
202
205
|
websocket-driver (0.7.3)
|
|
203
206
|
websocket-extensions (>= 0.1.0)
|
|
204
207
|
websocket-extensions (0.1.5)
|
|
205
|
-
zeitwerk (2.4.
|
|
208
|
+
zeitwerk (2.4.2)
|
|
206
209
|
|
|
207
210
|
PLATFORMS
|
|
208
211
|
ruby
|
|
@@ -216,4 +219,4 @@ DEPENDENCIES
|
|
|
216
219
|
rubocop
|
|
217
220
|
|
|
218
221
|
BUNDLED WITH
|
|
219
|
-
2.
|
|
222
|
+
2.2.3
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -6,11 +6,10 @@ Use [jsonapi-serializer](https://github.com/jsonapi-serializer/jsonapi-serialize
|
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
Add
|
|
9
|
+
Add `grape-jsonapi` to your Gemfile.
|
|
10
10
|
|
|
11
11
|
```ruby
|
|
12
|
-
gem 'grape'
|
|
13
|
-
gem 'grape-jsonapi'
|
|
12
|
+
gem 'grape-jsonapi', require: "grape_jsonapi"
|
|
14
13
|
```
|
|
15
14
|
|
|
16
15
|
## Usage
|
|
@@ -52,6 +51,16 @@ get "/" do
|
|
|
52
51
|
end
|
|
53
52
|
```
|
|
54
53
|
|
|
54
|
+
### Override `meta`and `links` properties
|
|
55
|
+
|
|
56
|
+
`meta` and `links` properties are usually defined per resource within your serializer ([here](https://github.com/jsonapi-serializer/jsonapi-serializer#meta-per-resource) and [here](https://github.com/jsonapi-serializer/jsonapi-serializer#links-per-object))
|
|
57
|
+
|
|
58
|
+
However, if you need to override those properties, you can pass them as options when rendering your response:
|
|
59
|
+
```ruby
|
|
60
|
+
user = User.find("123")
|
|
61
|
+
render user, meta: { pagination: { page: 1, total: 42 } }, links: { self: 'https://my-awesome.app.com/users/1' }
|
|
62
|
+
```
|
|
63
|
+
|
|
55
64
|
### Model parser for response documentation
|
|
56
65
|
|
|
57
66
|
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:
|
|
@@ -5,11 +5,10 @@ module Grape
|
|
|
5
5
|
module Jsonapi
|
|
6
6
|
class << self
|
|
7
7
|
def call(object, env)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
::Grape::Json.dump(object)
|
|
8
|
+
response = serializable?(object) ? serialize(object, env) : { data: object }
|
|
9
|
+
::Grape::Json.dump(
|
|
10
|
+
response.merge(env.slice('meta', 'links'))
|
|
11
|
+
)
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
private
|
|
@@ -25,17 +24,17 @@ module Grape
|
|
|
25
24
|
def serialize(object, env)
|
|
26
25
|
if object.respond_to?(:serializable_hash)
|
|
27
26
|
serializable_object(object, jsonapi_options(env)).serializable_hash
|
|
28
|
-
elsif serializable_collection?(object)
|
|
29
|
-
serializable_collection(object, jsonapi_options(env))
|
|
30
27
|
elsif object.is_a?(Hash)
|
|
31
28
|
serialize_each_pair(object, env)
|
|
29
|
+
elsif serializable_collection?(object)
|
|
30
|
+
serializable_collection(object, env)
|
|
32
31
|
else
|
|
33
32
|
object
|
|
34
33
|
end
|
|
35
34
|
end
|
|
36
35
|
|
|
37
36
|
def serializable_collection?(object)
|
|
38
|
-
object.respond_to?(:to_a) && object.all? do |o|
|
|
37
|
+
!object.nil? && object.respond_to?(:to_a) && object.any? && object.all? do |o|
|
|
39
38
|
o.respond_to?(:serializable_hash)
|
|
40
39
|
end
|
|
41
40
|
end
|
|
@@ -48,13 +47,13 @@ module Grape
|
|
|
48
47
|
serializable_class(object, options)&.new(object, options)
|
|
49
48
|
end
|
|
50
49
|
|
|
51
|
-
def serializable_collection(collection,
|
|
50
|
+
def serializable_collection(collection, env)
|
|
52
51
|
if heterogeneous_collection?(collection)
|
|
53
|
-
collection.
|
|
54
|
-
serialize_resource(o,
|
|
52
|
+
collection.each_with_object({ data: [] }) do |o, hash|
|
|
53
|
+
hash[:data].push(serialize_resource(o, env)[:data])
|
|
55
54
|
end
|
|
56
55
|
else
|
|
57
|
-
serialize_resource(collection,
|
|
56
|
+
serialize_resource(collection, env)
|
|
58
57
|
end
|
|
59
58
|
end
|
|
60
59
|
|
|
@@ -62,8 +61,10 @@ module Grape
|
|
|
62
61
|
collection.map { |item| item.class.name }.uniq.many?
|
|
63
62
|
end
|
|
64
63
|
|
|
65
|
-
def serialize_resource(resource,
|
|
66
|
-
jsonapi_serializable(resource,
|
|
64
|
+
def serialize_resource(resource, env)
|
|
65
|
+
jsonapi_serializable(resource, jsonapi_options(env))&.serializable_hash || resource.map do |item|
|
|
66
|
+
serialize(item, env)
|
|
67
|
+
end
|
|
67
68
|
end
|
|
68
69
|
|
|
69
70
|
def serializable_class(object, options)
|
|
@@ -78,8 +79,15 @@ module Grape
|
|
|
78
79
|
end
|
|
79
80
|
|
|
80
81
|
def serialize_each_pair(object, env)
|
|
81
|
-
h = {}
|
|
82
|
-
object.each_pair
|
|
82
|
+
h = { data: {} }
|
|
83
|
+
object.each_pair do |k, v|
|
|
84
|
+
serialized_value = serialize(v, env)
|
|
85
|
+
h[:data][k] = if serialized_value.is_a?(Hash) && serialized_value[:data]
|
|
86
|
+
serialized_value[:data]
|
|
87
|
+
else
|
|
88
|
+
serialized_value
|
|
89
|
+
end
|
|
90
|
+
end
|
|
83
91
|
h
|
|
84
92
|
end
|
|
85
93
|
|
|
@@ -18,12 +18,15 @@ describe Grape::Formatter::Jsonapi do
|
|
|
18
18
|
describe '.call' do
|
|
19
19
|
subject { described_class.call(object, env) }
|
|
20
20
|
let(:jsonapi_serializer_options) { nil }
|
|
21
|
-
let(:
|
|
21
|
+
let(:meta) { { pagination: { page: 1, total: 2 } } }
|
|
22
|
+
let(:links) { { self: 'https://example/org' } }
|
|
23
|
+
let(:env) { { 'jsonapi_serializer_options' => jsonapi_serializer_options, 'meta' => meta, 'links' => links } }
|
|
22
24
|
|
|
23
25
|
context 'when the object is a string' do
|
|
24
26
|
let(:object) { 'I am a string' }
|
|
27
|
+
let(:response) { ::Grape::Json.dump({ data: object, meta: meta, links: links }) }
|
|
25
28
|
|
|
26
|
-
it { is_expected.to eq
|
|
29
|
+
it { is_expected.to eq response }
|
|
27
30
|
end
|
|
28
31
|
|
|
29
32
|
context 'when the object is serializable' do
|
|
@@ -33,76 +36,102 @@ describe Grape::Formatter::Jsonapi do
|
|
|
33
36
|
|
|
34
37
|
context 'when the object has a model_name defined' do
|
|
35
38
|
let(:object) { admin }
|
|
36
|
-
|
|
39
|
+
let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) }
|
|
40
|
+
|
|
41
|
+
it { is_expected.to eq response }
|
|
37
42
|
end
|
|
38
43
|
|
|
39
44
|
context 'when the object is a active serializable model instance' do
|
|
40
45
|
let(:object) { user }
|
|
46
|
+
let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) }
|
|
41
47
|
|
|
42
|
-
it { is_expected.to eq
|
|
48
|
+
it { is_expected.to eq response }
|
|
43
49
|
end
|
|
44
50
|
|
|
45
|
-
context 'when the object is an array
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
51
|
+
context 'when the object is an array' do
|
|
52
|
+
context 'when the object is an array of active serializable model instances' do
|
|
53
|
+
let(:object) { [user, another_user] }
|
|
54
|
+
let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) }
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
it { is_expected.to eq response }
|
|
57
|
+
end
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
context 'when the array contains instances of different models' do
|
|
60
|
+
let(:object) { [user, blog_post] }
|
|
61
|
+
let(:response) do
|
|
62
|
+
::Grape::Json.dump({
|
|
63
|
+
data: [
|
|
64
|
+
UserSerializer.new(user, {}).serializable_hash[:data],
|
|
65
|
+
BlogPostSerializer.new(blog_post, {}).serializable_hash[:data]
|
|
66
|
+
],
|
|
67
|
+
meta: meta,
|
|
68
|
+
links: links
|
|
69
|
+
})
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'returns an array of jsonapi serialialized objects' do
|
|
73
|
+
expect(subject).to eq response
|
|
74
|
+
end
|
|
59
75
|
end
|
|
60
|
-
end
|
|
61
76
|
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
context 'when the object is an empty array' do
|
|
78
|
+
let(:object) { [] }
|
|
64
79
|
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
it { is_expected.to eq({ data: [], meta: meta, links: links }.to_json) }
|
|
81
|
+
end
|
|
67
82
|
|
|
68
|
-
|
|
69
|
-
|
|
83
|
+
context 'when the object is an array of null objects' do
|
|
84
|
+
let(:object) { [nil, nil] }
|
|
70
85
|
|
|
71
|
-
|
|
86
|
+
it { is_expected.to eq({ data: [nil, nil], meta: meta, links: links }.to_json) }
|
|
87
|
+
end
|
|
72
88
|
end
|
|
73
89
|
|
|
74
|
-
context 'when the object is a
|
|
75
|
-
|
|
90
|
+
context 'when the object is a hash' do
|
|
91
|
+
context 'when the object is an empty hash' do
|
|
92
|
+
let(:object) { {} }
|
|
76
93
|
|
|
77
|
-
|
|
78
|
-
|
|
94
|
+
it { is_expected.to eq({ data: {}, meta: meta, links: links }.to_json) }
|
|
95
|
+
end
|
|
79
96
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
blog_post: blog_post
|
|
85
|
-
}
|
|
97
|
+
context 'when the object is a Hash of plain values' do
|
|
98
|
+
let(:object) { user.as_json }
|
|
99
|
+
|
|
100
|
+
it { is_expected.to eq ::Grape::Json.dump({ data: user.as_json, meta: meta, links: links }) }
|
|
86
101
|
end
|
|
87
102
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
103
|
+
context 'when the object is a Hash with serializable object values' do
|
|
104
|
+
let(:object) do
|
|
105
|
+
{ user: user, blog_post: blog_post }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
let(:response) do
|
|
109
|
+
::Grape::Json.dump({
|
|
110
|
+
data: {
|
|
111
|
+
user: UserSerializer.new(user, {}).serializable_hash[:data],
|
|
112
|
+
blog_post: BlogPostSerializer.new(blog_post, {}).serializable_hash[:data]
|
|
113
|
+
},
|
|
114
|
+
meta: meta,
|
|
115
|
+
links: links
|
|
116
|
+
})
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it 'returns an hash of with jsonapi serialialized objects values' do
|
|
120
|
+
expect(subject).to eq response
|
|
121
|
+
end
|
|
93
122
|
end
|
|
94
123
|
end
|
|
95
124
|
|
|
96
125
|
context 'when the object is nil' do
|
|
97
126
|
let(:object) { nil }
|
|
98
127
|
|
|
99
|
-
it { is_expected.to eq
|
|
128
|
+
it { is_expected.to eq({ data: nil, meta: meta, links: links }.to_json) }
|
|
100
129
|
end
|
|
101
130
|
|
|
102
131
|
context 'when the object is a number' do
|
|
103
132
|
let(:object) { 42 }
|
|
104
133
|
|
|
105
|
-
it { is_expected.to eq
|
|
134
|
+
it { is_expected.to eq({ data: 42, meta: meta, links: links }.to_json) }
|
|
106
135
|
end
|
|
107
136
|
|
|
108
137
|
context 'when a custom serializer is passed as an option' do
|
|
@@ -113,7 +142,7 @@ describe Grape::Formatter::Jsonapi do
|
|
|
113
142
|
}
|
|
114
143
|
end
|
|
115
144
|
|
|
116
|
-
it { is_expected.to eq ::Grape::Json.dump(another_user_serializer.serializable_hash) }
|
|
145
|
+
it { is_expected.to eq ::Grape::Json.dump(another_user_serializer.serializable_hash.merge(meta: meta, links: links)) }
|
|
117
146
|
end
|
|
118
147
|
end
|
|
119
148
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: grape-jsonapi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emmanuel Cousin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2022-01-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: grape
|
|
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
141
141
|
- !ruby/object:Gem::Version
|
|
142
142
|
version: '0'
|
|
143
143
|
requirements: []
|
|
144
|
-
rubygems_version: 3.
|
|
144
|
+
rubygems_version: 3.1.6
|
|
145
145
|
signing_key:
|
|
146
146
|
specification_version: 4
|
|
147
147
|
summary: Use grape-jsonapi in grape
|