swagger-serializer 0.6.1 → 0.8.2

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 +50 -12
  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 +6 -1
  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 +2 -2
  44. metadata +16 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da9cac70a15262e0347ae5505044169f3e9a6f80aec1f604d8f7ff7bf431cc94
4
- data.tar.gz: c6e8270890b50456e51a27423a9c55fca437e60363fe43ad3b14ae4adae377cc
3
+ metadata.gz: 2db9e6f2be15977410edbebc03527173bb0fdd180c086b599f18065044a26a96
4
+ data.tar.gz: 0a27a92bd4be55bdfb246b3f31f4ed6b561e54a69b68a26489fb0514c3f0850a
5
5
  SHA512:
6
- metadata.gz: 5aab9030106681ed9f9807d04d15d79d0353bbc3d8655304db2f433b56623db466e8ac74e1bc8efcdfcafd2edae374d701631153c67f995dca0317905d50ff0b
7
- data.tar.gz: 6579c2a177c3b8790de0672119d69514397f755a37488078017baa5f8ac02f2090c455f1cb972c88aeac4ba5598b8b808f74d6643029ddab644e8082ed58dee5
6
+ metadata.gz: 524e018e98512e7b4382b4d10b52bf065caaa330584de8a667a10922be1b78efea19f64f3c8979587ef201dda7ccae0c1c20ce5a4c719ff0ec6d2b634af13634
7
+ data.tar.gz: f930d17e8bbfd2ae84bfd7c76d658aa456d1776c1e667dac20010e3f8beb09db658b887a61deb66c9bfe89155828113c615f23fab9b40f1454b0b827d90bba28
File without changes
data/.gitignore CHANGED
File without changes
File without changes
data/.rspec CHANGED
File without changes
File without changes
File without changes
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -33,12 +33,16 @@ Load it in initializer.
33
33
  # config/initializers/swagger_serializer.rb
34
34
 
35
35
  if Rails.application.config.eager_load
36
+ Swagger::DSL.current.config.lazy_define_paths = true
36
37
  Rails.application.config.after_initialize do
38
+ Rails.application.reload_routes!
39
+ Swagger::DSL.current.define_paths!
37
40
  Swagger::Schema.current = Swagger::Schema.new(Swagger::DSL.current.resolved)
38
41
  end
39
42
  else
40
43
  Swagger::Schema.current = Swagger::Schema.new(Swagger::DSL.current)
41
- Swagger::Serializer::Store.current.options[:resolver] = Swagger::DSL.current.resolver
44
+ Swagger::Serializer::Store.current.serialize_options[:resolver] = Swagger::DSL.current.resolver
45
+ Swagger::Serializer::Store.current.deserialize_options[:resolver] = Swagger::DSL.current.resolver
42
46
  end
43
47
  ```
44
48
 
@@ -50,12 +54,12 @@ class ApplicationController < ActionController::Base
50
54
  include Swagger::Serializer::RailsController
51
55
  extend Swagger::DSL::RailsController
52
56
 
53
- def render_ok(data)
54
- render_as_schema 200, :json, data
57
+ def render_ok(data, context = nil)
58
+ render_as_schema 200, :json, data, context
55
59
  end
56
60
 
57
- def render_bad(data)
58
- render_as_schema 400, :json, data
61
+ def render_bad(data, context = nil)
62
+ render_as_schema 400, :json, data, context
59
63
  end
60
64
  end
61
65
  ```
@@ -186,7 +190,8 @@ The key is configurable by
186
190
 
187
191
  ```ruby
188
192
  # in config/initializers/swagger_serializer.rb
189
- Swagger::Serializer::Store.current.options[:inject_key] = "my_inject_key"
193
+ Swagger::Serializer::Store.current.serialize_options[:inject_key] = "my_inject_key"
194
+ Swagger::Serializer::Store.current.deserialize_options[:inject_key] = "my_inject_key"
190
195
  Swagger::DSL.current.config.inject_key = "my_inject_key"
191
196
  ```
192
197
 
@@ -260,8 +265,8 @@ Use it in controllers.
260
265
  class ApplicationController < ActionController::Base
261
266
  include Swagger::Serializer::RailsController
262
267
 
263
- def render_ok(data)
264
- render_as_schema 200, :json, data
268
+ def render_ok(data, context = nil)
269
+ render_as_schema 200, :json, data, context
265
270
  end
266
271
  end
267
272
  ```
@@ -299,10 +304,6 @@ end
299
304
 
300
305
  Now you can get `{ "id" => 42, "name" => "me!!!!" }`.
301
306
 
302
- This serializer class detection uses the schema's `title` key.
303
- If you want to use `Foo::BarSerializer`, set `Foo::Bar` to `title` key.
304
- The key is configurable by `Swagger::Serializer::Store.current.options[:inject_key] = "my_inject_key"`.
305
-
306
307
  Sometimes model needs direct serialize.
307
308
 
308
309
  ```ruby
@@ -316,6 +317,43 @@ end
316
317
 
317
318
  Now you can get serialized result by `p User.first.serialize`.
318
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
+
319
357
  ## Development
320
358
 
321
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
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
@@ -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
@@ -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)
@@ -24,5 +25,9 @@ module Swagger
24
25
  def method_missing(name, *args)
25
26
  @model.public_send(name, *args)
26
27
  end
28
+
29
+ def nil?
30
+ @model.nil?
31
+ end
27
32
  end
28
33
  end
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.1".freeze
3
+ VERSION = "0.8.2".freeze
4
4
  end
5
5
  end
@@ -27,10 +27,10 @@ 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
- spec.add_development_dependency "rake", "~> 10.5"
33
+ spec.add_development_dependency "rake", "~> 13.0"
34
34
  spec.add_development_dependency "rspec", "~> 3.9"
35
35
  spec.add_development_dependency "rspec-power_assert", "~> 1.1"
36
36
  spec.add_development_dependency "rubocop", "~> 0.76"
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.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-03 00:00:00.000000000 Z
11
+ date: 2020-11-06 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
@@ -58,14 +64,14 @@ dependencies:
58
64
  requirements:
59
65
  - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '10.5'
67
+ version: '13.0'
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
72
  - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '10.5'
74
+ version: '13.0'
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rspec
71
77
  requirement: !ruby/object:Gem::Requirement
@@ -234,7 +240,7 @@ 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.1
243
+ documentation_uri: https://www.rubydoc.info/gems/swagger-serializer/0.8.2
238
244
  post_install_message:
239
245
  rdoc_options: []
240
246
  require_paths:
@@ -250,8 +256,7 @@ 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
259
+ rubygems_version: 3.0.3
255
260
  signing_key:
256
261
  specification_version: 4
257
262
  summary: Swagger (OpenAPI 3) schema based Serializer