grape-jsonapi 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +65 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +15 -0
  5. data/CHANGELOG.md +52 -0
  6. data/Gemfile +8 -0
  7. data/Gemfile.lock +219 -0
  8. data/LICENSE +21 -0
  9. data/README.md +88 -0
  10. data/Rakefile +3 -0
  11. data/grape-jsonapi.gemspec +29 -0
  12. data/lib/grape_jsonapi.rb +11 -0
  13. data/lib/grape_jsonapi/deprecated/formatter.rb +25 -0
  14. data/lib/grape_jsonapi/deprecated/parser.rb +21 -0
  15. data/lib/grape_jsonapi/endpoint_extension.rb +12 -0
  16. data/lib/grape_jsonapi/formatter.rb +92 -0
  17. data/lib/grape_jsonapi/parser.rb +195 -0
  18. data/lib/grape_jsonapi/version.rb +7 -0
  19. data/spec/lib/grape_jsonapi/deprecated.rb/formatter_spec.rb +7 -0
  20. data/spec/lib/grape_jsonapi/deprecated.rb/parser_spec.rb +7 -0
  21. data/spec/lib/grape_jsonapi/formatter_spec.rb +121 -0
  22. data/spec/lib/grape_jsonapi/parser_spec.rb +214 -0
  23. data/spec/lib/grape_jsonapi/version_spec.rb +5 -0
  24. data/spec/spec_helper.rb +6 -0
  25. data/spec/support/models/blog_post.rb +17 -0
  26. data/spec/support/models/db_record.rb +19 -0
  27. data/spec/support/models/foo.rb +28 -0
  28. data/spec/support/models/user.rb +27 -0
  29. data/spec/support/models/user_admin.rb +31 -0
  30. data/spec/support/serializers/another_blog_post_serializer.rb +9 -0
  31. data/spec/support/serializers/another_user_serializer.rb +7 -0
  32. data/spec/support/serializers/blog_post_serializer.rb +9 -0
  33. data/spec/support/serializers/db_record_serializer.rb +18 -0
  34. data/spec/support/serializers/foo_serializer.rb +23 -0
  35. data/spec/support/serializers/user_serializer.rb +9 -0
  36. metadata +165 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7774831ab89dff300b99065123ca93b91950cdda6254f1b8eaf01588736d345a
4
+ data.tar.gz: 6592eb7bf01db5b64f3d777c3f77d2b49a34e0cc3dc6e1459e2f2b64ef851067
5
+ SHA512:
6
+ metadata.gz: 724fb0160e489a260e539768f587220ca66c3b37fdf21d047ab23b1303b587ce4382218da97457bf5827ec3e8c3cbb2c84b51cf7565d93c1c30f435b12ad21c9
7
+ data.tar.gz: 380f98e0324834f5a5d6e50f91475ab1a778735fc797a9289713aed8c01b01db5bf95b04026b9134219bb4309e36cea3ef3f8fb0042a744c5e77af6329ca1982
@@ -0,0 +1,65 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.6.6-node-browsers
11
+ environment:
12
+ RAILS_ENV: test
13
+ PGHOST: 127.0.0.1
14
+ PGUSER: root
15
+
16
+ # Specify service dependencies here if necessary
17
+ # CircleCI maintains a library of pre-built images
18
+ # documented at https://circleci.com/docs/2.0/circleci-images/
19
+
20
+ working_directory: ~/repo
21
+
22
+ steps:
23
+ - checkout
24
+
25
+ # Download and cache dependencies
26
+ - restore_cache:
27
+ keys:
28
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
29
+ # fallback to using the latest cache if no exact match is found
30
+ - v1-dependencies-
31
+
32
+ - run:
33
+ name: install gems
34
+ command: |
35
+ gem install bundler
36
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
37
+
38
+ - save_cache:
39
+ paths:
40
+ - ./vendor/bundle
41
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
42
+
43
+ # run tests!
44
+ - run:
45
+ name: run tests
46
+ command: |
47
+ mkdir /tmp/test-results
48
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
49
+
50
+ bundle exec rspec --format progress \
51
+ --out /tmp/test-results/rspec.xml \
52
+ --format progress \
53
+ $TEST_FILES
54
+
55
+ - run:
56
+ name: run rubocop
57
+ command: |
58
+ bundle exec rubocop
59
+
60
+ # collect reports
61
+ - store_test_results:
62
+ path: /tmp/test-results
63
+ - store_artifacts:
64
+ path: /tmp/test-results
65
+ destination: test-results
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
@@ -0,0 +1,15 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ Exclude:
8
+ - spec/**/*
9
+ TargetRubyVersion: 2.6
10
+
11
+ Metrics/ClassLength:
12
+ Max: 150
13
+
14
+ Style/Documentation:
15
+ Enabled: false
@@ -0,0 +1,52 @@
1
+ ## Changelog
2
+
3
+ ### v1.0.1 (next)
4
+
5
+ * Your contribution here.
6
+
7
+ ### v1.0.0 (November 21, 2020)
8
+
9
+ [#14](https://github.com/EmCousin/grape_fast_jsonapi/pull/14) - [@EmCousin](https://github.com/EmCousin)
10
+
11
+ * Changed dependency from [fast_jsonapi](https://github.com/Netflix/fast_jsonapi) to [jsonapi-serializer](https://github.com/jsonapi-serializer/jsonapi-serializer)
12
+ * 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
+ * Fixed bugs due to breaking changes caused by the switch
14
+ * Added and configured Rubocop
15
+ * Security updates
16
+
17
+ ### v0.2.6 (June 20, 2020)
18
+
19
+ * [#14](https://github.com/EmCousin/grape_fast_jsonapi/pull/14) and [#21](https://github.com/EmCousin/grape_fast_jsonapi/pull/21) - Fixes to swagger parser: Respect `:key` setting, fix column type rendering, allow adding to schema - [@vincentvanbush](https://github.com/vincentvanbush) and [@nathanvda](https://github.com/nathanvda)
20
+
21
+ ### v0.2.5 (January 23, 2020)
22
+
23
+ * [#18](https://github.com/EmCousin/grape_fast_jsonapi/pull/18) - Revert to model_name instead of class-name - [@dblommesteijn](https://github.com/dblommesteijn)
24
+
25
+ Note : This PR fixes a bug when serializing a ActiveRecord::Relation instance, the formatter was looking for a formatter `ActiveRecord::RelationSerializer` serializer that doesn't exist, insteafd of looking for the serializer corresponding to its model name.
26
+
27
+ * Security updates
28
+
29
+ ### v0.2.4 (December 16, 2019)
30
+
31
+ * [#15](https://github.com/EmCousin/grape_fast_jsonapi/pull/15) - Handle serializers which don't have any attributes - [@vesan](https://github.com/vesan)
32
+
33
+ ### v0.2.3 (December 12, 2019)
34
+
35
+ * Reverted v0.2.2 and bumped `loofah` using `dependabot` - [@EmCousin](https://github.com/EmCousin).
36
+
37
+ ### v0.2.2 (December 12, 2019)
38
+
39
+ * Fixed low severity vulnerabiliy issue with `loofah` dependency - [@EmCousin](https://github.com/EmCousin).
40
+
41
+ ### v0.2.1 (September 18, 2019)
42
+
43
+ * [#12](https://github.com/EmCousin/grape_fast_jsonapi/pull/12) - Removed call to `rails` and fixed a potential security issue - [@EmCousin](https://github.com/EmCousin).
44
+
45
+ ### v0.2.0 (February 8, 2019)
46
+
47
+ * [#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)
48
+ * [#6](https://github.com/EmCousin/grape_fast_jsonapi/pull/6) - Fix to make the parser compatible with latest version of fast_jsonapi (1.5 at date) - [@rromanchuk](https://github.com/rromanchuk).
49
+
50
+ ### v0.1.0
51
+
52
+ * Initial public release - [@EmCousin](https://github.com/EmCousin).
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'grape'
8
+ gem 'jsonapi-serializer'
@@ -0,0 +1,219 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ grape-jsonapi (1.0.0)
5
+ grape
6
+ jsonapi-serializer
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (6.0.3.4)
12
+ actionpack (= 6.0.3.4)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.0.3.4)
16
+ actionpack (= 6.0.3.4)
17
+ activejob (= 6.0.3.4)
18
+ activerecord (= 6.0.3.4)
19
+ activestorage (= 6.0.3.4)
20
+ activesupport (= 6.0.3.4)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.0.3.4)
23
+ actionpack (= 6.0.3.4)
24
+ actionview (= 6.0.3.4)
25
+ activejob (= 6.0.3.4)
26
+ mail (~> 2.5, >= 2.5.4)
27
+ rails-dom-testing (~> 2.0)
28
+ actionpack (6.0.3.4)
29
+ actionview (= 6.0.3.4)
30
+ activesupport (= 6.0.3.4)
31
+ rack (~> 2.0, >= 2.0.8)
32
+ rack-test (>= 0.6.3)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
+ actiontext (6.0.3.4)
36
+ actionpack (= 6.0.3.4)
37
+ activerecord (= 6.0.3.4)
38
+ activestorage (= 6.0.3.4)
39
+ activesupport (= 6.0.3.4)
40
+ nokogiri (>= 1.8.5)
41
+ actionview (6.0.3.4)
42
+ activesupport (= 6.0.3.4)
43
+ builder (~> 3.1)
44
+ erubi (~> 1.4)
45
+ rails-dom-testing (~> 2.0)
46
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
+ activejob (6.0.3.4)
48
+ activesupport (= 6.0.3.4)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.0.3.4)
51
+ activesupport (= 6.0.3.4)
52
+ activerecord (6.0.3.4)
53
+ activemodel (= 6.0.3.4)
54
+ activesupport (= 6.0.3.4)
55
+ activestorage (6.0.3.4)
56
+ actionpack (= 6.0.3.4)
57
+ activejob (= 6.0.3.4)
58
+ activerecord (= 6.0.3.4)
59
+ marcel (~> 0.3.1)
60
+ activesupport (6.0.3.4)
61
+ concurrent-ruby (~> 1.0, >= 1.0.2)
62
+ i18n (>= 0.7, < 2)
63
+ minitest (~> 5.1)
64
+ tzinfo (~> 1.1)
65
+ zeitwerk (~> 2.2, >= 2.2.2)
66
+ ast (2.4.1)
67
+ builder (3.2.4)
68
+ concurrent-ruby (1.1.7)
69
+ crass (1.0.6)
70
+ diff-lcs (1.4.4)
71
+ dry-configurable (0.11.6)
72
+ concurrent-ruby (~> 1.0)
73
+ dry-core (~> 0.4, >= 0.4.7)
74
+ dry-equalizer (~> 0.2)
75
+ dry-container (0.7.2)
76
+ concurrent-ruby (~> 1.0)
77
+ dry-configurable (~> 0.1, >= 0.1.3)
78
+ dry-core (0.4.10)
79
+ concurrent-ruby (~> 1.0)
80
+ dry-equalizer (0.3.0)
81
+ dry-inflector (0.2.0)
82
+ dry-logic (1.0.8)
83
+ concurrent-ruby (~> 1.0)
84
+ dry-core (~> 0.2)
85
+ dry-equalizer (~> 0.2)
86
+ dry-types (1.4.0)
87
+ concurrent-ruby (~> 1.0)
88
+ dry-container (~> 0.3)
89
+ dry-core (~> 0.4, >= 0.4.4)
90
+ dry-equalizer (~> 0.3)
91
+ dry-inflector (~> 0.1, >= 0.1.2)
92
+ dry-logic (~> 1.0, >= 1.0.2)
93
+ erubi (1.10.0)
94
+ globalid (0.4.2)
95
+ activesupport (>= 4.2.0)
96
+ grape (1.5.1)
97
+ activesupport
98
+ builder
99
+ dry-types (>= 1.1)
100
+ mustermann-grape (~> 1.0.0)
101
+ rack (>= 1.3.0)
102
+ rack-accept
103
+ i18n (1.8.5)
104
+ concurrent-ruby (~> 1.0)
105
+ jsonapi-serializer (2.1.0)
106
+ activesupport (>= 4.2)
107
+ loofah (2.8.0)
108
+ crass (~> 1.0.2)
109
+ nokogiri (>= 1.5.9)
110
+ mail (2.7.1)
111
+ mini_mime (>= 0.1.1)
112
+ marcel (0.3.3)
113
+ mimemagic (~> 0.3.2)
114
+ method_source (1.0.0)
115
+ mimemagic (0.3.5)
116
+ mini_mime (1.0.2)
117
+ mini_portile2 (2.4.0)
118
+ minitest (5.14.2)
119
+ mustermann (1.1.1)
120
+ ruby2_keywords (~> 0.0.1)
121
+ mustermann-grape (1.0.1)
122
+ mustermann (>= 1.0.0)
123
+ nio4r (2.5.4)
124
+ nokogiri (1.10.10)
125
+ mini_portile2 (~> 2.4.0)
126
+ parallel (1.20.1)
127
+ parser (2.7.2.0)
128
+ ast (~> 2.4.1)
129
+ rack (2.2.3)
130
+ rack-accept (0.4.5)
131
+ rack (>= 0.4)
132
+ rack-test (1.1.0)
133
+ rack (>= 1.0, < 3)
134
+ rails (6.0.3.4)
135
+ actioncable (= 6.0.3.4)
136
+ actionmailbox (= 6.0.3.4)
137
+ actionmailer (= 6.0.3.4)
138
+ actionpack (= 6.0.3.4)
139
+ actiontext (= 6.0.3.4)
140
+ actionview (= 6.0.3.4)
141
+ activejob (= 6.0.3.4)
142
+ activemodel (= 6.0.3.4)
143
+ activerecord (= 6.0.3.4)
144
+ activestorage (= 6.0.3.4)
145
+ activesupport (= 6.0.3.4)
146
+ bundler (>= 1.3.0)
147
+ railties (= 6.0.3.4)
148
+ sprockets-rails (>= 2.0.0)
149
+ rails-dom-testing (2.0.3)
150
+ activesupport (>= 4.2.0)
151
+ nokogiri (>= 1.6)
152
+ rails-html-sanitizer (1.3.0)
153
+ loofah (~> 2.3)
154
+ railties (6.0.3.4)
155
+ actionpack (= 6.0.3.4)
156
+ activesupport (= 6.0.3.4)
157
+ method_source
158
+ rake (>= 0.8.7)
159
+ thor (>= 0.20.3, < 2.0)
160
+ rainbow (3.0.0)
161
+ rake (13.0.1)
162
+ regexp_parser (2.0.0)
163
+ rexml (3.2.4)
164
+ rspec (3.10.0)
165
+ rspec-core (~> 3.10.0)
166
+ rspec-expectations (~> 3.10.0)
167
+ rspec-mocks (~> 3.10.0)
168
+ rspec-core (3.10.0)
169
+ rspec-support (~> 3.10.0)
170
+ rspec-expectations (3.10.0)
171
+ diff-lcs (>= 1.2.0, < 2.0)
172
+ rspec-support (~> 3.10.0)
173
+ rspec-mocks (3.10.0)
174
+ diff-lcs (>= 1.2.0, < 2.0)
175
+ rspec-support (~> 3.10.0)
176
+ rspec-support (3.10.0)
177
+ rubocop (1.4.2)
178
+ parallel (~> 1.10)
179
+ parser (>= 2.7.1.5)
180
+ rainbow (>= 2.2.2, < 4.0)
181
+ regexp_parser (>= 1.8)
182
+ rexml
183
+ rubocop-ast (>= 1.1.1)
184
+ ruby-progressbar (~> 1.7)
185
+ unicode-display_width (>= 1.4.0, < 2.0)
186
+ rubocop-ast (1.2.0)
187
+ parser (>= 2.7.1.5)
188
+ ruby-progressbar (1.10.1)
189
+ ruby2_keywords (0.0.2)
190
+ sprockets (4.0.2)
191
+ concurrent-ruby (~> 1.0)
192
+ rack (> 1, < 3)
193
+ sprockets-rails (3.2.2)
194
+ actionpack (>= 4.0)
195
+ activesupport (>= 4.0)
196
+ sprockets (>= 3.0.0)
197
+ thor (1.0.1)
198
+ thread_safe (0.3.6)
199
+ tzinfo (1.2.8)
200
+ thread_safe (~> 0.1)
201
+ unicode-display_width (1.7.0)
202
+ websocket-driver (0.7.3)
203
+ websocket-extensions (>= 0.1.0)
204
+ websocket-extensions (0.1.5)
205
+ zeitwerk (2.4.1)
206
+
207
+ PLATFORMS
208
+ ruby
209
+
210
+ DEPENDENCIES
211
+ grape
212
+ grape-jsonapi!
213
+ jsonapi-serializer
214
+ rails (>= 4.2.0)
215
+ rspec (~> 3.7)
216
+ rubocop
217
+
218
+ BUNDLED WITH
219
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Emmanuel Cousin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ [![CircleCI](https://circleci.com/gh/EmCousin/grape-jsonapi/tree/master.svg?style=svg)](https://circleci.com/gh/EmCousin/grape-jsonapi/tree/master)
2
+
3
+ # Grape::Jsonapi
4
+
5
+ Use [jsonapi-serializer](https://github.com/jsonapi-serializer/jsonapi-serializer) with [Grape](https://github.com/ruby-grape/grape).
6
+
7
+ ## Installation
8
+
9
+ Add the `grape` and `grape-jsonapi` gems to Gemfile.
10
+
11
+ ```ruby
12
+ gem 'grape'
13
+ gem 'grape-jsonapi'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Tell your API to use Grape::Formatter::Jsonapi
19
+
20
+ ```ruby
21
+ class API < Grape::API
22
+ content_type :jsonapi, "application/vnd.api+json"
23
+ formatter :json, Grape::Formatter::Jsonapi
24
+ formatter :jsonapi, Grape::Formatter::Jsonapi
25
+ end
26
+ ```
27
+
28
+ ### Use `render` to specify JSONAPI options
29
+
30
+ ```ruby
31
+ get "/" do
32
+ user = User.find("123")
33
+ render user, include: [:account]
34
+ end
35
+ ```
36
+
37
+ ### Use a custom serializer
38
+
39
+ ```ruby
40
+ get "/" do
41
+ user = User.find("123")
42
+ render user, serializer: 'CustomUserSerializer'
43
+ end
44
+ ```
45
+
46
+ Or
47
+
48
+ ```ruby
49
+ get "/" do
50
+ user = User.find("123")
51
+ render CustomUserSerializer.new(user).serialized_json
52
+ end
53
+ ```
54
+
55
+ ### Model parser for response documentation
56
+
57
+ 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:
58
+
59
+ ```ruby
60
+ # FastJsonapi serializer example
61
+
62
+ # app/serializers/base_serializer.rb
63
+ class BaseSerializer; end
64
+ # app/serializers/user_serializer.rb
65
+ class UserSerializer < BaseSerializer
66
+ include JSONAPI::Serializer
67
+
68
+ set_type :user
69
+ has_many :orders
70
+
71
+ attributes :name, :email
72
+ end
73
+
74
+ # config/initializers/grape_swagger.rb
75
+ GrapeSwagger.model_parsers.register(GrapeSwagger::Jsonapi::Parser, BaseSerializer)
76
+
77
+ # Your grape API endpoint
78
+ desc 'Get current user' do
79
+ success code: 200, model: UserSerializer, message: 'The current user'
80
+ # [...]
81
+ end
82
+ ```
83
+
84
+ Note that you **need** the `grape-swagger` gem for this to work, otherwise it will throw an error.
85
+
86
+ ## Credit
87
+
88
+ Code adapted from [grape-jsonapi-resources](https://github.com/cdunn/grape-jsonapi-resources)