swagger-dsl 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a014577228f6444f0b6f6d4b3766d8231e280b4a8d099e1676f1d3711a832bab
4
- data.tar.gz: a8495609f16a93e3af4dfacacafc400a3f14235836dc7b49e6907afafaa287c1
3
+ metadata.gz: 4ec890325cd8a13a5777720b3351c4c041d91e393fba975379795bace2860d95
4
+ data.tar.gz: 8082c1127a67be4060d2dbb32e1f89955b5b12642861198bdf1d0b627be750db
5
5
  SHA512:
6
- metadata.gz: d86adb4e85d2162ea20b5cee7f6bb99c3447a7e00a3d6ceb6d427c104e20d0819cb372664ebfcc7677c3d2854c445d5feb059ca8e56bca288e29b1f854b6ee62
7
- data.tar.gz: b1235bc9d31acb7c2828c6e45bf3123f9f2a907079e4dee1c6008c4ebdb276d0f00ed724469dfde91f33eb2e9fc4bf030e809e14c712c255d15d99de55a91e7a
6
+ metadata.gz: ef51b9cdb97ec79798999e4b4d5414e8bdd9b79e696d5492e11a3ec2f16fbb63be0222fd81a03bbe9fd57532b1b4a7e87c074c2c6fc0e855814c8ba8075c0e87
7
+ data.tar.gz: cce2fd4ff859619d69ab8fedf7668f7f70f83c60ab825ec31a60f40608f3fe30de2de82177815d68008c3d87530642860882f672b3488b9278da3483232f8f67
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.4.0
4
+
5
+ - add: lazy_define_paths for Rails eager_load
6
+
3
7
  ## 1.3.0
4
8
 
5
9
  - mod: put / patch both define
data/README.md CHANGED
@@ -90,6 +90,23 @@ So `params` are also default "required".
90
90
 
91
91
  If you do not want it, `Swagger::DSL.current.config.default_required = false`.
92
92
 
93
+ ### If Rails eager_load = true
94
+
95
+ Rails controllers will be loaded before loading the routes when eager_load is enabled.
96
+
97
+ So set `Swagger::DSL.current.config.lazy_define_paths = true`, `reload_routes!` and `Swagger::DSL.current.define_paths!`
98
+
99
+ ```ruby
100
+ if Rails.application.config.eager_load
101
+ Swagger::DSL.current.config.lazy_define_paths = true
102
+ Rails.application.config.after_initialize do
103
+ Rails.application.reload_routes!
104
+ Swagger::DSL.current.define_paths!
105
+ JSON.dump(Swagger::DSL.current)
106
+ end
107
+ end
108
+ ```
109
+
93
110
  ## Development
94
111
 
95
112
  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.
@@ -15,7 +15,7 @@ module Swagger
15
15
  end
16
16
  end
17
17
 
18
- attr_reader :config
18
+ attr_reader :config, :define_paths_procs
19
19
 
20
20
  def initialize(schema = nil, config: Config.new)
21
21
  merge!(schema || {})
@@ -24,6 +24,11 @@ module Swagger
24
24
  self["paths"] ||= {}
25
25
  self["components"] = Components[self["components"] || {}]
26
26
  @config = config
27
+ @define_paths_procs = []
28
+ end
29
+
30
+ def define_paths!
31
+ define_paths_procs.each(&:call)
27
32
  end
28
33
  end
29
34
  end
@@ -1,14 +1,22 @@
1
1
  module Swagger
2
2
  class DSL < Hash
3
3
  class Config
4
- attr_accessor :inject_key, :default_dsl, :dsl_options, :eager, :default_required
4
+ attr_accessor :inject_key, :default_dsl, :dsl_options, :eager, :default_required, :lazy_define_paths
5
5
 
6
- def initialize(inject_key: "title", default_dsl: nil, dsl_options: nil, eager: false, default_required: true)
6
+ def initialize(
7
+ inject_key: "title",
8
+ default_dsl: nil,
9
+ dsl_options: nil,
10
+ eager: false,
11
+ default_required: true,
12
+ lazy_define_paths: false
13
+ )
7
14
  @inject_key = inject_key
8
15
  @default_dsl = default_dsl
9
16
  @dsl_options ||= { reference_name: ->(name) { name.sub(/Serializer$/, "") } }
10
17
  @eager = eager
11
18
  @default_required = default_required
19
+ @lazy_define_paths = lazy_define_paths
12
20
  end
13
21
  end
14
22
  end
@@ -29,9 +29,7 @@ module Swagger
29
29
 
30
30
  def body(format: @format, dsl: nil, &block)
31
31
  formats(format).each do |f|
32
- self["requestBody"]["content"][f] = {
33
- "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block)
34
- }
32
+ self["requestBody"]["content"][f] = { "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block) }
35
33
  end
36
34
  end
37
35
 
@@ -46,9 +44,7 @@ module Swagger
46
44
  def render(code = 200, format: @format, dsl: nil, &block)
47
45
  self["responses"][code] ||= { "content" => {} }
48
46
  formats(format).each do |f|
49
- self["responses"][code]["content"][f] = {
50
- "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block)
51
- }
47
+ self["responses"][code]["content"][f] = { "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block) }
52
48
  end
53
49
  end
54
50
 
@@ -6,6 +6,7 @@ module Swagger
6
6
  def initialize(options, *args, &block)
7
7
  @default_required = options[:default_required]
8
8
  self["required"] = true if @default_required
9
+
9
10
  unless args.empty?
10
11
  self["name"] = args.first
11
12
  args[1..-1].each { |arg| merge!(arg.map { |k, v| [k.to_s, v] }.to_h) }
@@ -10,7 +10,11 @@ module Swagger
10
10
 
11
11
  %i[path query header cookie].each do |in_type|
12
12
  define_method(in_type) do |*args, &block|
13
- args.empty? ? ParametersInType.new(self, in_type, { default_required: @default_required }, &block) : self << Parameter.new({ default_required: @default_required }, *args, in: in_type, &block)
13
+ if args.empty?
14
+ ParametersInType.new(self, in_type, { default_required: @default_required }, &block)
15
+ else
16
+ self << Parameter.new({ default_required: @default_required }, *args, in: in_type, &block)
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -8,6 +8,15 @@ module Swagger
8
8
  class NotExactMatch < StandardError; end
9
9
 
10
10
  def swagger(action, format = :json, path: nil, method: nil, &block)
11
+ if Swagger::DSL.current.config.lazy_define_paths
12
+ Swagger::DSL.current.define_paths_procs <<
13
+ -> { swagger_define_path(action, format, path: path, method: method, &block) }
14
+ else
15
+ swagger_define_path(action, format, path: path, method: method, &block)
16
+ end
17
+ end
18
+
19
+ def swagger_define_path(action, format = :json, path: nil, method: nil, &block)
11
20
  operation_id = "#{name}##{action}"
12
21
 
13
22
  defaults = { action: action.to_s, controller: name.underscore.sub(/_controller$/, "") }
@@ -20,14 +29,12 @@ module Swagger
20
29
  if method.include?("|")
21
30
  raise NotExactMatch, "route matched but verb can be #{verb}! specify :method key like 'get'."
22
31
  end
23
- method = ["put", "patch"] if ["put", "patch"].include?(method)
32
+ method = %w[put patch] if %w[put patch].include?(method)
24
33
  path ||= route.path.spec.to_s.sub("(.:format)", "").gsub(/:(\w+)/, "{\\1}")
25
34
 
26
35
  operation = Swagger::DSL::Operation.new(operation_id, format: format, &block)
27
36
  Swagger::DSL.current["paths"][path] ||= {}
28
- Array(method).each do |single_method|
29
- Swagger::DSL.current["paths"][path][single_method] = operation
30
- end
37
+ Array(method).each { |single_method| Swagger::DSL.current["paths"][path][single_method] = operation }
31
38
  end
32
39
 
33
40
  alias_method :oas3, :swagger
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  class DSL < Hash
3
- VERSION = "1.3.0".freeze
3
+ VERSION = "1.4.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagger-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
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: 2019-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jimmy
@@ -264,7 +264,7 @@ metadata:
264
264
  homepage_uri: https://github.com/Narazaka/swagger-dsl
265
265
  source_code_uri: https://github.com/Narazaka/swagger-dsl.git
266
266
  changelog_uri: https://github.com/Narazaka/swagger-dsl/blob/master/CHANGELOG.md
267
- documentation_uri: https://www.rubydoc.info/gems/swagger-dsl/1.3.0
267
+ documentation_uri: https://www.rubydoc.info/gems/swagger-dsl/1.4.0
268
268
  post_install_message:
269
269
  rdoc_options: []
270
270
  require_paths: