swagger-serializer 0.6.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +0 -0
  3. data/.gitignore +0 -0
  4. data/.prettierrc +0 -0
  5. data/.rspec +0 -0
  6. data/.rubocop.yml +0 -0
  7. data/.rubocop_airbnb.yml +0 -0
  8. data/CHANGELOG.md +0 -0
  9. data/Gemfile +0 -0
  10. data/LICENSE +0 -0
  11. data/README.md +43 -6
  12. data/Rakefile +0 -0
  13. data/bin/console +0 -0
  14. data/bin/fmt +0 -0
  15. data/bin/setup +0 -0
  16. data/lib/swagger/schema.rb +2 -0
  17. data/lib/swagger/schema/component.rb +3 -2
  18. data/lib/swagger/schema/content.rb +0 -0
  19. data/lib/swagger/schema/handle_servers.rb +0 -0
  20. data/lib/swagger/schema/header.rb +0 -0
  21. data/lib/swagger/schema/headers.rb +0 -0
  22. data/lib/swagger/schema/media_type.rb +6 -4
  23. data/lib/swagger/schema/media_type/deserializer.rb +0 -0
  24. data/lib/swagger/schema/media_type/validator.rb +0 -0
  25. data/lib/swagger/schema/operation.rb +0 -0
  26. data/lib/swagger/schema/parameter.rb +0 -0
  27. data/lib/swagger/schema/parameter_base.rb +11 -11
  28. data/lib/swagger/schema/parameters.rb +6 -4
  29. data/lib/swagger/schema/parameters/deserializer.rb +0 -0
  30. data/lib/swagger/schema/parameters/validator.rb +0 -0
  31. data/lib/swagger/schema/path_item.rb +0 -0
  32. data/lib/swagger/schema/request_body.rb +0 -0
  33. data/lib/swagger/schema/response.rb +0 -0
  34. data/lib/swagger/schema/responses.rb +0 -0
  35. data/lib/swagger/schema/schema_accessor.rb +0 -0
  36. data/lib/swagger/schema/server.rb +0 -0
  37. data/lib/swagger/schema/util.rb +0 -0
  38. data/lib/swagger/serializer.rb +4 -3
  39. data/lib/swagger/serializer/model.rb +0 -0
  40. data/lib/swagger/serializer/rails_controller.rb +6 -8
  41. data/lib/swagger/serializer/store.rb +0 -0
  42. data/lib/swagger/serializer/version.rb +1 -1
  43. data/swagger-serializer.gemspec +1 -1
  44. metadata +18 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bda748bb32d9308010609db0d3e86aa515ec85207d8ebba302f90e897139e500
4
- data.tar.gz: a32eea14539ec58fcdbf3f9ef491ffc844a18247bf3c812e6e43a0be3e772f3a
3
+ metadata.gz: 64f8aa131df07ce9de11beb3384f3c8bd94431136b0fd0032b05bc1a102a06cd
4
+ data.tar.gz: 56b1e7f8f3364a3df476bd12b4e08760c8dd5e8f39ebb079b15bc798a3a64d8c
5
5
  SHA512:
6
- metadata.gz: e8b3dd2abbbaed3fc208dc38fb5c92033eadce081a6ee031a759654c6ae6711b06d57c5ddab36251d5210e1e21052024b0802fb7a662bfbd25a09b2484626a2a
7
- data.tar.gz: 4d313ee5fb65c7de7510f7e6e16143d41deff79e93d7411e78c52acc5d3afe72f30b9bb2c547f273849af3fe3ca8446bfbafb0f134bff60d58c39fa251440fb3
6
+ metadata.gz: f8c0b337e81158f0384139aca9be740108567bb6c8d3e94ac7d4d6d404a0353bec53505938771a1bd4fab010e7170dcf8659e35d20fe14113b59267477923cd2
7
+ data.tar.gz: 97c6fc013f5e9339d23f38a1850fe20c4b5b21c0de715d7a5e0f1e019e33a6e95b52372b96503e00f411e194b376491656a0e0e5d5cf41237270c0dbc928b65f
File without changes
data/.gitignore CHANGED
File without changes
data/.prettierrc CHANGED
File without changes
data/.rspec CHANGED
File without changes
data/.rubocop.yml CHANGED
File without changes
data/.rubocop_airbnb.yml CHANGED
File without changes
data/CHANGELOG.md CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
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/Rakefile CHANGED
File without changes
data/bin/console CHANGED
File without changes
data/bin/fmt CHANGED
File without changes
data/bin/setup CHANGED
File without changes
@@ -37,6 +37,8 @@ module Swagger
37
37
  end
38
38
 
39
39
  def component(name)
40
+ # HogeSerializer
41
+ name = name.name.sub(/Serializer$/, "") if name.class == Class
40
42
  component = Util.try_hash(components, name)
41
43
  Component.new(component) if component
42
44
  end
@@ -9,8 +9,9 @@ module Swagger
9
9
  @schema = schema
10
10
  end
11
11
 
12
- def serializer(options = {})
13
- JSON::Schema::Serializer.new(@schema, options)
12
+ def serializer(options = {}, store = true)
13
+ use_options = store ? Swagger::Serializer::Store.current.serialize_options.merge(options) : options
14
+ JSON::Schema::Serializer.new(@schema, use_options)
14
15
  end
15
16
  end
16
17
  end
File without changes
File without changes
File without changes
File without changes
@@ -14,12 +14,14 @@ module Swagger
14
14
 
15
15
  define_schema_accessor :schema, :example, :examples, :encoding
16
16
 
17
- def serializer(options = {})
18
- JSON::Schema::Serializer.new(schema, options)
17
+ def serializer(options = {}, store = true)
18
+ use_options = store ? Swagger::Serializer::Store.current.serialize_options.merge(options) : options
19
+ JSON::Schema::Serializer.new(schema, use_options)
19
20
  end
20
21
 
21
- def deserializer(options = {})
22
- Deserializer.new(schema, options)
22
+ def deserializer(options = {}, store = true)
23
+ use_options = store ? Swagger::Serializer::Store.current.deserialize_options.merge(options) : options
24
+ Deserializer.new(schema, use_options)
23
25
  end
24
26
 
25
27
  def validator
File without changes
File without changes
File without changes
File without changes
@@ -11,17 +11,17 @@ module Swagger
11
11
  end
12
12
 
13
13
  define_schema_accessor %i[
14
- description
15
- required
16
- deprecated
17
- allowEmptyValue
18
- style
19
- explode
20
- allowReserved
21
- schema
22
- example
23
- examples
24
- ].freeze
14
+ description
15
+ required
16
+ deprecated
17
+ allowEmptyValue
18
+ style
19
+ explode
20
+ allowReserved
21
+ schema
22
+ example
23
+ examples
24
+ ].freeze
25
25
 
26
26
  def to_json_schema
27
27
  schema
@@ -57,12 +57,14 @@ module Swagger
57
57
  schema
58
58
  end
59
59
 
60
- def serializer(options = {})
61
- JSON::Schema::Serializer.new(to_json_schema, options)
60
+ def serializer(options = {}, store = true)
61
+ use_options = store ? Swagger::Serializer::Store.current.serialize_options.merge(options) : options
62
+ JSON::Schema::Serializer.new(to_json_schema, use_options)
62
63
  end
63
64
 
64
- def deserializer(options = {})
65
- Deserializer.new(self, options)
65
+ def deserializer(options = {}, store = true)
66
+ use_options = store ? Swagger::Serializer::Store.current.deserialize_options.merge(options) : options
67
+ Deserializer.new(self, use_options)
66
68
  end
67
69
 
68
70
  def validator
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -7,8 +7,9 @@ require "yaml"
7
7
 
8
8
  module Swagger
9
9
  module Serializer
10
- def initialize(model)
10
+ def initialize(model, context = nil)
11
11
  @model = model
12
+ @context = context
12
13
  end
13
14
 
14
15
  def respond_to_missing?(name, include_private)
@@ -21,8 +22,8 @@ module Swagger
21
22
  end
22
23
  end
23
24
 
24
- def method_missing(name, *args)
25
- @model.public_send(name, *args)
25
+ def method_missing(name, *args, &block)
26
+ @model.public_send(name, *args, &block)
26
27
  end
27
28
 
28
29
  def nil?
File without changes
@@ -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(inject_context: context)
13
13
  end
14
14
 
15
15
  def swagger_operation
@@ -37,7 +37,7 @@ module Swagger::Serializer::RailsController
37
37
 
38
38
  def parameter_deserializer
39
39
  @parameter_deserializer ||=
40
- swagger_operation.parameters.deserializer(Swagger::Serializer::Store.current.deserialize_options)
40
+ swagger_operation.parameters.deserializer
41
41
  end
42
42
 
43
43
  def body_params
@@ -47,9 +47,7 @@ module Swagger::Serializer::RailsController
47
47
  def body_deserializer
48
48
  @body_deserializer ||=
49
49
  if request.content_type
50
- swagger_operation.request_body.content[request.content_type]&.deserializer(
51
- Swagger::Serializer::Store.current.deserialize_options,
52
- )
50
+ swagger_operation.request_body.content[request.content_type]&.deserializer
53
51
  end
54
52
  end
55
53
 
File without changes
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  module Serializer
3
- VERSION = "0.6.2".freeze
3
+ VERSION = "0.8.3".freeze
4
4
  end
5
5
  end
@@ -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.4"
30
+ spec.add_dependency "json-schema-serializer", [">= 1.6", "< 3"]
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,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-09 00:00:00.000000000 Z
11
+ date: 2021-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema-serializer
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.4'
19
+ version: '1.6'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '1.4'
29
+ version: '1.6'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: snake_camel
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -178,7 +184,7 @@ dependencies:
178
184
  - - "~>"
179
185
  - !ruby/object:Gem::Version
180
186
  version: '0.9'
181
- description:
187
+ description:
182
188
  email:
183
189
  - info@narazaka.net
184
190
  executables: []
@@ -234,8 +240,8 @@ metadata:
234
240
  homepage_uri: https://github.com/Narazaka/swagger-serializer
235
241
  source_code_uri: https://github.com/Narazaka/swagger-serializer.git
236
242
  changelog_uri: https://github.com/Narazaka/swagger-serializer/blob/master/CHANGELOG.md
237
- documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.6.2
238
- post_install_message:
243
+ documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.8.3
244
+ post_install_message:
239
245
  rdoc_options: []
240
246
  require_paths:
241
247
  - lib
@@ -250,9 +256,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
256
  - !ruby/object:Gem::Version
251
257
  version: '0'
252
258
  requirements: []
253
- rubyforge_project:
254
- rubygems_version: 2.7.6
255
- signing_key:
259
+ rubygems_version: 3.2.3
260
+ signing_key:
256
261
  specification_version: 4
257
262
  summary: Swagger (OpenAPI 3) schema based Serializer
258
263
  test_files: []