swagger-dsl 1.2.0 → 1.5.1

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: b131abdb9e40b72e5958ab7c2a7e34b42cf21e162ef01726feab2585b1e6e450
4
- data.tar.gz: dec48a00848fc65cf08fa11ebb4369cfa9793491ea10884bc472eb848ddeee06
3
+ metadata.gz: 786aa5eea8ae2c2ce700f8c9bf9777da355f5647788d0fd7a1756316017bc182
4
+ data.tar.gz: 879edc53b7549aa2d194428cdc66f94080adc7c1e306c9aa6d67612606d4639c
5
5
  SHA512:
6
- metadata.gz: 5ab34c7b9ca4de4d1dd3eb8bcabfdfb06a6090d6c3cace125abe1d67511bc00c876303bce1f2825a70e5573b737477f8c849e8d4539478c3bcfe5187e0f34a10
7
- data.tar.gz: da64cb44e3c4bee8ee6f4249be6393c86b181729fc2cf48e245279f94c883df775a9234f90ede60c35862e3051296b86ffdf8d376084aab398c68a8e85156f23
6
+ metadata.gz: f3462fb293dfb7f055d80ec3d027a2b33c9d5a973c84a0aaa2b84c566e7587bbfaa456a0e17e24321e1d525e22356314e9aa203d58f5c33ffac6d47d90d0f0e3
7
+ data.tar.gz: eb112da3508f85283177a6bdfb375f487880b5f32a2c18f301d21f4f221981fe7cf3e42c84f868e933032254fa944d790618be327ceba1648d1b183be79d2df0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.4.0
4
+
5
+ - add: lazy_define_paths for Rails eager_load
6
+
7
+ ## 1.3.0
8
+
9
+ - mod: put / patch both define
10
+
3
11
  ## 1.2.0
4
12
 
5
13
  - mod(brake): params default required
data/README.md CHANGED
@@ -42,11 +42,10 @@ end
42
42
  class UsersController < ApplicationController
43
43
  swagger :update do
44
44
  params do
45
- path :id, schema: :integer, required: true
45
+ path :id, schema: :integer
46
46
  query do
47
- safe schema: :boolean
47
+ safe schema: :boolean, required: false
48
48
  redirect do
49
- required true
50
49
  schema do
51
50
  string!
52
51
  format! "url"
@@ -83,6 +82,31 @@ Swagger::DSL.current["info"] = {
83
82
  JSON.dump(Swagger::DSL.current)
84
83
  ```
85
84
 
85
+ ### required default
86
+
87
+ Body and response parameters are "required" default in default DSL.
88
+
89
+ So `params` are also default "required".
90
+
91
+ If you do not want it, `Swagger::DSL.current.config.default_required = false`.
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
+
86
110
  ## Development
87
111
 
88
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.
data/lib/swagger/dsl.rb CHANGED
@@ -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
@@ -3,7 +3,7 @@ require_relative "./parameters"
3
3
  require_relative "../dsl"
4
4
 
5
5
  module Swagger
6
- class DSL
6
+ class DSL < Hash
7
7
  class Components < Hash
8
8
  STRING = "".freeze
9
9
 
@@ -1,14 +1,22 @@
1
1
  module Swagger
2
- class DSL
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
@@ -3,7 +3,7 @@ require_relative "./json_schema/jimmy"
3
3
  require_relative "../dsl"
4
4
 
5
5
  module Swagger
6
- class DSL
6
+ class DSL < Hash
7
7
  class JsonSchema
8
8
  def self.by(dsl_type = nil)
9
9
  dsl_type ||= Swagger::DSL.current.config.default_dsl
@@ -2,7 +2,7 @@ require "jimmy"
2
2
  require_relative "./jimmy_patch"
3
3
 
4
4
  module Swagger
5
- class DSL
5
+ class DSL < Hash
6
6
  class JsonSchema
7
7
  class Jimmy
8
8
  DOMAIN = ::Jimmy::Domain.new("")
@@ -2,7 +2,7 @@ require "json/schema/subset/dsl"
2
2
  require_relative "../../dsl"
3
3
 
4
4
  module Swagger
5
- class DSL
5
+ class DSL < Hash
6
6
  class JsonSchema
7
7
  class SubsetDSL
8
8
  def self.dsl(&block)
@@ -3,7 +3,7 @@ require_relative "./parameters"
3
3
  require_relative "../dsl"
4
4
 
5
5
  module Swagger
6
- class DSL
6
+ class DSL < Hash
7
7
  class Operation < Hash
8
8
  FORMAT_TYPE = {
9
9
  json: "application/json",
@@ -19,19 +19,30 @@ module Swagger
19
19
  self["requestBody"] = { "content" => {}, "required" => true }
20
20
  self["responses"] = {}
21
21
  self["parameters"] = []
22
+ self["tags"] = []
22
23
  @format = format
23
24
  instance_eval(&block)
24
25
  end
25
26
 
27
+ def summary(text)
28
+ self["summary"] = text
29
+ end
30
+
31
+ def description(text)
32
+ self["description"] = text
33
+ end
34
+
35
+ def tags(*tags)
36
+ self["tags"].concat(tags.flatten(1))
37
+ end
38
+
26
39
  def params(default_required: Swagger::DSL.current.config.default_required, &block)
27
40
  self["parameters"] = Parameters.new(default_required: default_required, &block)
28
41
  end
29
42
 
30
43
  def body(format: @format, dsl: nil, &block)
31
44
  formats(format).each do |f|
32
- self["requestBody"]["content"][f] = {
33
- "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block)
34
- }
45
+ self["requestBody"]["content"][f] = { "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block) }
35
46
  end
36
47
  end
37
48
 
@@ -46,9 +57,7 @@ module Swagger
46
57
  def render(code = 200, format: @format, dsl: nil, &block)
47
58
  self["responses"][code] ||= { "content" => {} }
48
59
  formats(format).each do |f|
49
- self["responses"][code]["content"][f] = {
50
- "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block)
51
- }
60
+ self["responses"][code]["content"][f] = { "schema" => Swagger::DSL::JsonSchema.by(dsl).dsl(&block) }
52
61
  end
53
62
  end
54
63
 
@@ -1,11 +1,12 @@
1
1
  require_relative "./json_schema"
2
2
 
3
3
  module Swagger
4
- class DSL
4
+ class DSL < Hash
5
5
  class Parameter < Hash
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) }
@@ -1,7 +1,7 @@
1
1
  require_relative "./parameters_in_type"
2
2
 
3
3
  module Swagger
4
- class DSL
4
+ class DSL < Hash
5
5
  class Parameters < Array
6
6
  def initialize(default_required:, &block)
7
7
  @default_required = default_required
@@ -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
@@ -2,12 +2,21 @@ require_relative "../dsl"
2
2
  require_relative "./operation"
3
3
 
4
4
  module Swagger
5
- class DSL
5
+ class DSL < Hash
6
6
  module RailsController
7
7
  class NotMatch < StandardError; end
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,11 +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
32
+ method = %w[put patch] if %w[put patch].include?(method)
23
33
  path ||= route.path.spec.to_s.sub("(.:format)", "").gsub(/:(\w+)/, "{\\1}")
24
34
 
25
35
  operation = Swagger::DSL::Operation.new(operation_id, format: format, &block)
26
36
  Swagger::DSL.current["paths"][path] ||= {}
27
- Swagger::DSL.current["paths"][path][method] = operation
37
+ Array(method).each { |single_method| Swagger::DSL.current["paths"][path][single_method] = operation }
28
38
  end
29
39
 
30
40
  alias_method :oas3, :swagger
@@ -3,7 +3,7 @@ require "hana"
3
3
  require "active_support/core_ext/object/deep_dup"
4
4
 
5
5
  module Swagger
6
- class DSL
6
+ class DSL < Hash
7
7
  module ResolveModule
8
8
  def resolved
9
9
  JsonRefs.call(deep_dup)
@@ -2,7 +2,7 @@ require_relative "../dsl"
2
2
  require_relative "./json_schema"
3
3
 
4
4
  module Swagger
5
- class DSL
5
+ class DSL < Hash
6
6
  module Serializer
7
7
  def swagger(dsl: nil, &block)
8
8
  name = self.name.sub(/Serializer$/, "")
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  class DSL < Hash
3
- VERSION = "1.2.0".freeze
3
+ VERSION = "1.5.1".freeze
4
4
  end
5
5
  end
data/swagger-dsl.gemspec CHANGED
@@ -31,9 +31,9 @@ Gem::Specification.new do |spec|
31
31
  spec.add_dependency "json-schema-subset-dsl", "~> 1.2"
32
32
  spec.add_dependency "hana", "~> 1.3"
33
33
  spec.add_dependency "json_refs", "~> 0.1"
34
- spec.add_dependency "activesupport", "~> 6.0"
34
+ spec.add_dependency "activesupport", ">= 4.0.2"
35
35
  spec.add_development_dependency "bundler", "~> 2.0"
36
- spec.add_development_dependency "rake", "~> 10.5"
36
+ spec.add_development_dependency "rake", "~> 13.0"
37
37
  spec.add_development_dependency "rspec", "~> 3.9"
38
38
  spec.add_development_dependency "rspec-power_assert", "~> 1.1"
39
39
  spec.add_development_dependency "rubocop", "~> 0.76"
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.2.0
4
+ version: 1.5.1
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-01 00:00:00.000000000 Z
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jimmy
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: activesupport
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '6.0'
75
+ version: 4.0.2
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '6.0'
82
+ version: 4.0.2
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '10.5'
103
+ version: '13.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '10.5'
110
+ version: '13.0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -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.2.0
267
+ documentation_uri: https://www.rubydoc.info/gems/swagger-dsl/1.5.1
268
268
  post_install_message:
269
269
  rdoc_options: []
270
270
  require_paths:
@@ -280,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  - !ruby/object:Gem::Version
281
281
  version: '0'
282
282
  requirements: []
283
- rubygems_version: 3.0.3
283
+ rubygems_version: 3.1.4
284
284
  signing_key:
285
285
  specification_version: 4
286
286
  summary: Swagger (OpenAPI 3) DSL