swagger-serializer 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -6
- data/lib/swagger/serializer.rb +2 -1
- data/lib/swagger/serializer/rails_controller.rb +4 -4
- data/lib/swagger/serializer/version.rb +1 -1
- data/swagger-serializer.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c981a97501496cce000677d6bb67c02db75b948d00541c683ef56c0fcf738d47
|
4
|
+
data.tar.gz: 1f53440f04121534346790081311a9e8907ffce11c8244c9bc8ca5dc4c405889
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2da0855070adbcaf65cd2e52569bff14fdb30b106af975acbcfbf33c18cd61a37a4e0b934580599c8479dda4de3e0b0c8baf67cd6aef786608ced9991821a9b7
|
7
|
+
data.tar.gz: 56a07032f129c2fa4d624347fd4ce60adff1ff0397afaaeb53a8c04b84afa39fa275b8391d5e13e70e7c49e148f0e4f9becf0621f41a560d9904f2e4979c2806
|
data/README.md
CHANGED
@@ -54,12 +54,12 @@ class ApplicationController < ActionController::Base
|
|
54
54
|
include Swagger::Serializer::RailsController
|
55
55
|
extend Swagger::DSL::RailsController
|
56
56
|
|
57
|
-
def render_ok(data)
|
58
|
-
render_as_schema 200, :json, data
|
57
|
+
def render_ok(data, context = nil)
|
58
|
+
render_as_schema 200, :json, data, context
|
59
59
|
end
|
60
60
|
|
61
|
-
def render_bad(data)
|
62
|
-
render_as_schema 400, :json, data
|
61
|
+
def render_bad(data, context = nil)
|
62
|
+
render_as_schema 400, :json, data, context
|
63
63
|
end
|
64
64
|
end
|
65
65
|
```
|
@@ -265,8 +265,8 @@ Use it in controllers.
|
|
265
265
|
class ApplicationController < ActionController::Base
|
266
266
|
include Swagger::Serializer::RailsController
|
267
267
|
|
268
|
-
def render_ok(data)
|
269
|
-
render_as_schema 200, :json, data
|
268
|
+
def render_ok(data, context = nil)
|
269
|
+
render_as_schema 200, :json, data, context
|
270
270
|
end
|
271
271
|
end
|
272
272
|
```
|
@@ -317,6 +317,43 @@ end
|
|
317
317
|
|
318
318
|
Now you can get serialized result by `p User.first.serialize`.
|
319
319
|
|
320
|
+
### @context
|
321
|
+
|
322
|
+
Collection serialization sometimes needs context data for avoiding N+1 access or some other reason.
|
323
|
+
|
324
|
+
`render_as_schema`'s 4th parameter is the context that will be passed to serializers `@context`.
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
# app/serializers/item_serializer.rb
|
328
|
+
class ItemSerializer < BaseSerializer
|
329
|
+
swagger do
|
330
|
+
id :integer
|
331
|
+
rate :integer
|
332
|
+
end
|
333
|
+
|
334
|
+
def rate
|
335
|
+
@context[:rates][id]
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
# app/controllers/items_controller.rb
|
340
|
+
class ItemsController < ApplicationController
|
341
|
+
swagger :index do
|
342
|
+
render 200 do
|
343
|
+
array! do
|
344
|
+
cref! ItemSerializer
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
def index
|
350
|
+
items = Item.all
|
351
|
+
rates = ItemRate.calc_item_rates(items)
|
352
|
+
render_ok items, { rates: rates }
|
353
|
+
end
|
354
|
+
end
|
355
|
+
```
|
356
|
+
|
320
357
|
## Development
|
321
358
|
|
322
359
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/swagger/serializer.rb
CHANGED
@@ -3,13 +3,13 @@ module Swagger::Serializer::RailsController
|
|
3
3
|
|
4
4
|
# render
|
5
5
|
|
6
|
-
def render_as_schema(code, format, data)
|
7
|
-
render format => render_serializer(code, format).serialize(data)
|
6
|
+
def render_as_schema(code, format, data, context = nil)
|
7
|
+
render format => render_serializer(code, format, context).serialize(data)
|
8
8
|
end
|
9
9
|
|
10
|
-
def render_serializer(code, format)
|
10
|
+
def render_serializer(code, format, context = nil)
|
11
11
|
response_obj = swagger_operation.responses[code]
|
12
|
-
response_obj.content.send(format).serializer(Swagger::Serializer::Store.current.serialize_options)
|
12
|
+
response_obj.content.send(format).serializer(Swagger::Serializer::Store.current.serialize_options.merge(inject_context: context))
|
13
13
|
end
|
14
14
|
|
15
15
|
def swagger_operation
|
data/swagger-serializer.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = %w[lib]
|
29
29
|
|
30
|
-
spec.add_dependency "json-schema-serializer", "~> 1.
|
30
|
+
spec.add_dependency "json-schema-serializer", "~> 1.6"
|
31
31
|
spec.add_dependency "snake_camel", "~> 1.1"
|
32
32
|
spec.add_development_dependency "bundler", "~> 2"
|
33
33
|
spec.add_development_dependency "rake", "~> 13.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Narazaka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema-serializer
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.6'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: snake_camel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -234,7 +234,7 @@ metadata:
|
|
234
234
|
homepage_uri: https://github.com/Narazaka/swagger-serializer
|
235
235
|
source_code_uri: https://github.com/Narazaka/swagger-serializer.git
|
236
236
|
changelog_uri: https://github.com/Narazaka/swagger-serializer/blob/master/CHANGELOG.md
|
237
|
-
documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.
|
237
|
+
documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.7.0
|
238
238
|
post_install_message:
|
239
239
|
rdoc_options: []
|
240
240
|
require_paths:
|