zero-rails_openapi 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +0 -2
- data/Gemfile.lock +2 -5
- data/README.md +19 -17
- data/documentation/examples/goods_doc.rb +14 -2
- data/lib/oas_objs/example_obj.rb +88 -0
- data/lib/oas_objs/helpers.rb +1 -1
- data/lib/oas_objs/media_type_obj.rb +14 -5
- data/lib/oas_objs/ref_obj.rb +0 -1
- data/lib/oas_objs/request_body_obj.rb +2 -2
- data/lib/oas_objs/response_obj.rb +5 -5
- data/lib/oas_objs/schema_obj.rb +31 -23
- data/lib/open_api/config.rb +1 -0
- data/lib/open_api/dsl.rb +4 -4
- data/lib/open_api/dsl/api_info_obj.rb +16 -12
- data/lib/open_api/dsl/common_dsl.rb +4 -4
- data/lib/open_api/dsl/ctrl_info_obj.rb +4 -0
- data/lib/open_api/generator.rb +3 -0
- data/lib/open_api/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 996bf583a71a6c1b34daa068d9fad89761fd8074
|
4
|
+
data.tar.gz: 97858fcf27fdf3bf491e28d8fcba9b5061cebf7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a46294ecd676e965ac1c4b0c3f7e0944751feae640882872d06f7ff52547b1c5b4bc4b71311d5a6b75fa98c66f18c2eb4832c7d2fc70b05c846cdae3456ec170
|
7
|
+
data.tar.gz: 16d36540905bee8b7f242dfadab8cc6f5edeffb83f88b936fc4da8ee6d2941fe0c206030781c8ae8121eed9a6e459dc5a8387b691b19491716a995ca1b1788a8
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at zhandao. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
zero-rails_openapi (1.3.
|
4
|
+
zero-rails_openapi (1.3.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -44,8 +44,5 @@ DEPENDENCIES
|
|
44
44
|
rspec (~> 3.0)
|
45
45
|
zero-rails_openapi!
|
46
46
|
|
47
|
-
RUBY VERSION
|
48
|
-
ruby 2.4.1p111
|
49
|
-
|
50
47
|
BUNDLED WITH
|
51
|
-
1.16.0
|
48
|
+
1.16.0
|
data/README.md
CHANGED
@@ -278,7 +278,8 @@ parameters, request body, responses, securities, servers.
|
|
278
278
|
# method signature
|
279
279
|
param param_type, name, type, required, schema_hash = { }
|
280
280
|
# usage
|
281
|
-
param :query, :page, Integer, :req, range: { gt: 0, le: 5 }, desc: 'page'
|
281
|
+
param :query, :page, Integer, :req, range: { gt: 0, le: 5 }, desc: 'page',
|
282
|
+
examples: { :example => 5 }
|
282
283
|
|
283
284
|
# method signature
|
284
285
|
param_ref component_key, *component_keys
|
@@ -320,7 +321,7 @@ parameters, request body, responses, securities, servers.
|
|
320
321
|
|
321
322
|
```ruby
|
322
323
|
# method signature
|
323
|
-
request_body required, media_type, desc = '',
|
324
|
+
request_body required, media_type, desc = '', hash = { }
|
324
325
|
# usage
|
325
326
|
request_body :opt, :form, type: { id!: Integer, name: String }
|
326
327
|
|
@@ -330,13 +331,13 @@ parameters, request body, responses, securities, servers.
|
|
330
331
|
body_ref :Body
|
331
332
|
|
332
333
|
# method signature
|
333
|
-
body! media_type, desc = '',
|
334
|
+
body! media_type, desc = '', hash = { }
|
334
335
|
# usage
|
335
336
|
body :json
|
336
337
|
|
337
338
|
# method implement
|
338
|
-
def form desc = '',
|
339
|
-
body :form, desc,
|
339
|
+
def form desc = '', hash = { }
|
340
|
+
body :form, desc, hash
|
340
341
|
end
|
341
342
|
# usage
|
342
343
|
form! 'register', data: {
|
@@ -353,40 +354,41 @@ parameters, request body, responses, securities, servers.
|
|
353
354
|
}
|
354
355
|
|
355
356
|
# method implement
|
356
|
-
def file! media_type, desc = '',
|
357
|
-
body! media_type, desc,
|
357
|
+
def file! media_type, desc = '', hash = { type: File }
|
358
|
+
body! media_type, desc, hash
|
358
359
|
end
|
359
360
|
```
|
360
361
|
|
361
|
-
**Notice:** Each API can only declare a request body.
|
362
|
-
That is, all of the above methods you can only choose one of them.
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
schema_hash: As above (see param), but more than a `type` (schema type).
|
362
|
+
(1) **Notice:** Each API can only declare a request body.
|
363
|
+
That is, all of the above methods you can only choose one of them.
|
364
|
+
(2) Media Type: We provide some [mapping](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/oas_objs/media_type_obj.rb) from symbols to real media-types.
|
365
|
+
(3) schema_hash: As above (see param), it's just one more a `type` (schema type).
|
366
|
+
(4) `examples` usage see [goods_doc](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/goods_doc.rb)
|
367
367
|
|
368
368
|
- response family methods (OAS - [Response Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#response-object))
|
369
369
|
- `response` (`resp`)
|
370
370
|
- `response_ref`
|
371
371
|
- `default_response` (`dft_resp`)
|
372
372
|
- `error_response` (`other_response`, `oth_resp`, `error`, `err_resp`): Are `response`'s aliases, should be used in the error response context.
|
373
|
+
- `override_response` # TODO
|
373
374
|
|
374
375
|
Define the responses for the API(operation).
|
375
376
|
You can use the Response Object to link to request body that is defined at the components/responses by method response_ref().
|
376
377
|
|
377
378
|
```ruby
|
378
379
|
# method signature
|
379
|
-
response code, desc, media_type = nil,
|
380
|
+
response code, desc, media_type = nil, hash = { }
|
380
381
|
# usage
|
381
|
-
response
|
382
|
+
response 200, 'query result export', :pdf, type: File
|
382
383
|
|
383
384
|
# method signature
|
384
385
|
response_ref code_compkey_hash
|
385
386
|
# usage
|
386
|
-
response_ref
|
387
|
+
response_ref 700 => :RespComp, 800 => :RespComp
|
387
388
|
```
|
388
389
|
|
389
|
-
**practice:** Combined with wrong class, automatically generate error responses. TODO
|
390
|
+
(1) **practice:** Combined with wrong class, automatically generate error responses. TODO
|
391
|
+
(2) `examples` usage see [goods_doc](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/goods_doc.rb)
|
390
392
|
|
391
393
|
- security: TODO
|
392
394
|
|
@@ -14,7 +14,14 @@ class V2::GoodsDoc < BaseDoc
|
|
14
14
|
'expensive goods': :expensive,
|
15
15
|
'cheap goods': :cheap,
|
16
16
|
}
|
17
|
-
query :search_type, String, enum: %w[name creator category price]
|
17
|
+
# query :search_type, String, enum: %w[name creator category price]
|
18
|
+
do_query by: {
|
19
|
+
:search_type => { type: String, enum: %w[ name creator category price ] },
|
20
|
+
:export => { type: Boolean, desc: 'export as Excel format', examples: {
|
21
|
+
:right_input => true,
|
22
|
+
:wrong_input => 'wrong input'
|
23
|
+
}}
|
24
|
+
}
|
18
25
|
end
|
19
26
|
|
20
27
|
|
@@ -27,7 +34,12 @@ class V2::GoodsDoc < BaseDoc
|
|
27
34
|
:is_online => { type: Boolean, desc: 'it\'s online?' },
|
28
35
|
:remarks => { type: String, desc: 'remarks' },
|
29
36
|
:pic_path => { type: String, desc: 'picture url', is: :url },
|
30
|
-
}
|
37
|
+
},
|
38
|
+
exp_by: %i[ name category_id price ],
|
39
|
+
examples: {
|
40
|
+
:right_input => [ 'good1', 6, 5.7 ],
|
41
|
+
:wrong_input => [ 'good2', 0, -1 ]
|
42
|
+
}
|
31
43
|
end
|
32
44
|
|
33
45
|
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'oas_objs/helpers'
|
2
|
+
require 'oas_objs/ref_obj'
|
3
|
+
|
4
|
+
module OpenApi
|
5
|
+
module DSL
|
6
|
+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#exampleObject
|
7
|
+
class ExampleObj < Hash
|
8
|
+
include Helpers
|
9
|
+
|
10
|
+
attr_accessor :processed, :examples_hash, :keys_of_value
|
11
|
+
|
12
|
+
def initialize(examples_hash, keys_of_value = nil)
|
13
|
+
self.examples_hash = examples_hash
|
14
|
+
self.keys_of_value = keys_of_value
|
15
|
+
end
|
16
|
+
|
17
|
+
def process
|
18
|
+
self.processed =
|
19
|
+
examples_hash.map do |(name, value)|
|
20
|
+
value =
|
21
|
+
if keys_of_value.present? && value.is_a?(Array)
|
22
|
+
{ value: Hash[keys_of_value.zip(value)] }
|
23
|
+
elsif value.is_a?(Symbol) && value['$']
|
24
|
+
{ '$ref': RefObj.new(:example, value).process }
|
25
|
+
else
|
26
|
+
{ value: value }
|
27
|
+
end
|
28
|
+
|
29
|
+
{ name => value }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
__END__
|
38
|
+
|
39
|
+
# in a model
|
40
|
+
schemas:
|
41
|
+
properties:
|
42
|
+
name:
|
43
|
+
type: string
|
44
|
+
examples:
|
45
|
+
name:
|
46
|
+
$ref: http://example.org/petapi-examples/openapi.json#/components/examples/name-example
|
47
|
+
|
48
|
+
# in a request body:
|
49
|
+
requestBody:
|
50
|
+
content:
|
51
|
+
'application/json':
|
52
|
+
schema:
|
53
|
+
$ref: '#/components/schemas/Address'
|
54
|
+
examples:
|
55
|
+
foo:
|
56
|
+
summary: A foo example
|
57
|
+
value: {"foo": "bar"}
|
58
|
+
bar:
|
59
|
+
summary: A bar example
|
60
|
+
value: {"bar": "baz"}
|
61
|
+
'application/xml':
|
62
|
+
examples:
|
63
|
+
xmlExample:
|
64
|
+
summary: This is an example in XML
|
65
|
+
externalValue: 'http://example.org/examples/address-example.xml'
|
66
|
+
|
67
|
+
# in a parameter
|
68
|
+
parameters:
|
69
|
+
- name: 'zipCode'
|
70
|
+
in: 'query'
|
71
|
+
schema:
|
72
|
+
type: 'string'
|
73
|
+
format: 'zip-code'
|
74
|
+
examples:
|
75
|
+
zip-example:
|
76
|
+
$ref: '#/components/examples/zip-example'
|
77
|
+
|
78
|
+
# in a response
|
79
|
+
responses:
|
80
|
+
'200':
|
81
|
+
description: your car appointment has been booked
|
82
|
+
content:
|
83
|
+
application/json:
|
84
|
+
schema:
|
85
|
+
$ref: '#/components/schemas/SuccessResponse'
|
86
|
+
examples:
|
87
|
+
confirmation-success:
|
88
|
+
$ref: '#/components/examples/confirmation-success'
|
data/lib/oas_objs/helpers.rb
CHANGED
@@ -1,19 +1,28 @@
|
|
1
1
|
require 'oas_objs/schema_obj'
|
2
|
+
require 'oas_objs/example_obj'
|
2
3
|
|
3
4
|
module OpenApi
|
4
5
|
module DSL
|
5
6
|
# https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#media-type-object
|
6
7
|
class MediaTypeObj < Hash
|
7
|
-
attr_accessor :media_type, :schema
|
8
|
-
|
8
|
+
attr_accessor :media_type, :schema, :examples
|
9
|
+
|
10
|
+
def initialize(media_type, hash)
|
11
|
+
examples_hash = hash.delete(:examples)
|
12
|
+
exp_by = hash.delete(:exp_by)
|
13
|
+
schema_type = hash.values_at(:type, :data).compact.first
|
14
|
+
exp_by = schema_type.keys if exp_by == :all
|
15
|
+
|
16
|
+
self.examples = ExampleObj.new(examples_hash, exp_by) if examples_hash.present?
|
9
17
|
self.media_type = media_type_mapping media_type
|
10
|
-
self.schema = SchemaObj.new(
|
18
|
+
self.schema = SchemaObj.new(schema_type, hash)
|
11
19
|
end
|
12
20
|
|
13
21
|
def process
|
14
22
|
schema_processed = schema.process
|
15
|
-
|
16
|
-
|
23
|
+
result = schema_processed.values.join.blank? ? { } : { schema: schema_processed }
|
24
|
+
result.merge!(examples: examples.process) unless examples.nil?
|
25
|
+
media_type.nil? ? { } : { media_type => result }
|
17
26
|
end
|
18
27
|
|
19
28
|
# https://swagger.io/docs/specification/media-types/
|
data/lib/oas_objs/ref_obj.rb
CHANGED
@@ -9,8 +9,8 @@ module OpenApi
|
|
9
9
|
include Helpers
|
10
10
|
|
11
11
|
attr_accessor :processed, :media_type
|
12
|
-
def initialize(required, media_type, desc,
|
13
|
-
self.media_type = MediaTypeObj.new(media_type,
|
12
|
+
def initialize(required, media_type, desc, hash)
|
13
|
+
self.media_type = MediaTypeObj.new(media_type, hash)
|
14
14
|
self.processed = { required: required.to_s.match?(/req/), description: desc }
|
15
15
|
end
|
16
16
|
|
@@ -8,11 +8,11 @@ module OpenApi
|
|
8
8
|
include Helpers
|
9
9
|
|
10
10
|
attr_accessor :processed, :code, :media_type
|
11
|
-
def initialize(desc, media_type,
|
12
|
-
@
|
11
|
+
def initialize(desc, media_type, hash)
|
12
|
+
@hash = hash
|
13
13
|
@mt = media_type
|
14
14
|
self.code = code.to_s
|
15
|
-
self.media_type = MediaTypeObj.new(media_type,
|
15
|
+
self.media_type = MediaTypeObj.new(media_type, hash)
|
16
16
|
self.processed = { description: desc }
|
17
17
|
end
|
18
18
|
|
@@ -22,8 +22,8 @@ module OpenApi
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def override type_hash
|
25
|
-
@
|
26
|
-
self.media_type = MediaTypeObj.new(@mt, @
|
25
|
+
@hash[:type].merge!(type_hash)
|
26
|
+
self.media_type = MediaTypeObj.new(@mt, @hash)
|
27
27
|
self
|
28
28
|
end
|
29
29
|
end
|
data/lib/oas_objs/schema_obj.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'oas_objs/helpers'
|
2
2
|
require 'open_api/config'
|
3
3
|
require 'oas_objs/ref_obj'
|
4
|
+
require 'oas_objs/example_obj'
|
4
5
|
|
5
6
|
module OpenApi
|
6
7
|
module DSL
|
@@ -9,6 +10,7 @@ module OpenApi
|
|
9
10
|
include Helpers
|
10
11
|
|
11
12
|
attr_accessor :processed, :type
|
13
|
+
|
12
14
|
def initialize(type, schema_hash)
|
13
15
|
self.processed = { }
|
14
16
|
# [Note] Here is no limit to type, even if the input isn't up to OAS,
|
@@ -26,20 +28,21 @@ module OpenApi
|
|
26
28
|
return processed if @preprocessed
|
27
29
|
|
28
30
|
processed.merge! processed_type
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
}
|
31
|
+
reducx processed_enum_and_length,
|
32
|
+
processed_range,
|
33
|
+
processed_is_and_format(param_name),
|
34
|
+
{
|
35
|
+
pattern: _pattern&.inspect&.delete('/'),
|
36
|
+
default: '_default',
|
37
|
+
examples: self[:examples].present? ? ExampleObj.new(self[:examples], self[:exp_by]).process : nil,
|
38
|
+
},
|
39
|
+
{ as: _as, permit: _permit, not_permit: _npermit, req_if: _req_if, opt_if: _opt_if }
|
39
40
|
then_merge!
|
41
|
+
processed[:default] = _default unless _default.nil?
|
40
42
|
|
41
|
-
|
43
|
+
reducx(processed_desc options).then_merge!
|
42
44
|
end
|
45
|
+
|
43
46
|
alias process process_for
|
44
47
|
|
45
48
|
def preprocess_with_desc desc, param_name = nil
|
@@ -87,7 +90,7 @@ module OpenApi
|
|
87
90
|
recursive_array_type t
|
88
91
|
elsif t.is_a? Symbol
|
89
92
|
RefObj.new(:schema, t).process
|
90
|
-
elsif t.in? %w[float double int32 int64] #
|
93
|
+
elsif t.in? %w[float double int32 int64] # to README: 这些值应该传 string 进来, symbol 只允许 $ref
|
91
94
|
{ type: t.match?('int') ? 'integer' : 'number', format: t}
|
92
95
|
elsif t.in? %w[binary base64]
|
93
96
|
{ type: 'string', format: t}
|
@@ -99,6 +102,7 @@ module OpenApi
|
|
99
102
|
{ type: t }
|
100
103
|
end
|
101
104
|
end
|
105
|
+
|
102
106
|
def recursive_obj_type(t) # DSL use { prop_name: prop_type } to represent object structure
|
103
107
|
return processed_type(t) if !t.is_a?(Hash) || t.key?(:type)
|
104
108
|
|
@@ -114,6 +118,7 @@ module OpenApi
|
|
114
118
|
end
|
115
119
|
_schema.keep_if &value_present
|
116
120
|
end
|
121
|
+
|
117
122
|
def recursive_array_type(t)
|
118
123
|
if t.is_a? Array
|
119
124
|
{
|
@@ -148,16 +153,12 @@ module OpenApi
|
|
148
153
|
|
149
154
|
# generate length range fields by _lth array
|
150
155
|
lth = _length || [ ]
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
}
|
156
|
+
max = lth.is_a?(Array) ? lth.first : ("#{lth}".match?('ge') ? "#{lth}".split('_').last.to_i : nil)
|
157
|
+
min = lth.is_a?(Array) ? lth.last : ("#{lth}".match?('le') ? "#{lth}".split('_').last.to_i : nil)
|
158
|
+
if processed[:type] == 'array'
|
159
|
+
{ minItems: max, maxItems: min }
|
156
160
|
else
|
157
|
-
{
|
158
|
-
minLength: lth.is_a?(Array) ? lth.first : ("#{lth}".match?('ge') ? "#{lth}".split('_').last.to_i : nil),
|
159
|
-
maxLength: lth.is_a?(Array) ? lth.last : ("#{lth}".match?('le') ? "#{lth}".split('_').last.to_i : nil)
|
160
|
-
}
|
161
|
+
{ minLength: max, maxLength: min }
|
161
162
|
end.merge!(enum: _enum).keep_if &value_present
|
162
163
|
end
|
163
164
|
|
@@ -180,12 +181,12 @@ module OpenApi
|
|
180
181
|
it.merge! is: _is
|
181
182
|
end
|
182
183
|
end
|
184
|
+
|
183
185
|
def recognize_is_options_in(name)
|
184
186
|
# identify whether `is` patterns matched the name, if so, generate `is`.
|
185
187
|
Config.is_options.each do |pattern|
|
186
188
|
self._is = pattern or break if name.match? /#{pattern}/
|
187
189
|
end if _is.nil?
|
188
|
-
self.delete :_is if _is.in?([:x, :we])
|
189
190
|
end
|
190
191
|
|
191
192
|
|
@@ -203,6 +204,8 @@ module OpenApi
|
|
203
204
|
_as: %i[ as to for map mapping ], # NOT OAS Spec, it's for zero-params_processor
|
204
205
|
_permit: %i[ permit pmt ], # NOT OAS Spec, it's for zero-params_processor
|
205
206
|
_npermit: %i[ npmt not_permit unpermit ], # NOT OAS Spec, it's for zero-params_processor
|
207
|
+
_req_if: %i[ req_if req_when ], # NOT OAS Spec, it's for zero-params_processor
|
208
|
+
_opt_if: %i[ opt_if opt_when ], # NOT OAS Spec, it's for zero-params_processor
|
206
209
|
}.each do |key, aliases|
|
207
210
|
define_method key do
|
208
211
|
aliases.each do |alias_name|
|
@@ -226,7 +229,12 @@ Primitive Sample
|
|
226
229
|
|
227
230
|
{
|
228
231
|
"type": "string",
|
229
|
-
"format": "email"
|
232
|
+
"format": "email",
|
233
|
+
"examples": {
|
234
|
+
"exp1": {
|
235
|
+
"value": 'val'
|
236
|
+
}
|
237
|
+
}
|
230
238
|
}
|
231
239
|
|
232
240
|
Simple Model
|
data/lib/open_api/config.rb
CHANGED
data/lib/open_api/dsl.rb
CHANGED
@@ -36,22 +36,22 @@ module OpenApi
|
|
36
36
|
# select the routing info (corresponding to the current method) from the routing list.
|
37
37
|
action_path = "#{@_ctrl_path ||= controller_path}##{method}"
|
38
38
|
routes_info = ctrl_routes_list&.select { |api| api[:action_path].match? /^#{action_path}$/ }&.first
|
39
|
-
pp "[ZRO
|
39
|
+
pp "[ZRO Warning] Routing mapping failed: #{@_ctrl_path}##{method}" and return if routes_info.nil?
|
40
40
|
Generator.generate_builder_file(action_path, builder) if builder.present?
|
41
41
|
|
42
42
|
# structural { #path: { #http_method:{ } } }, for pushing into Paths Object.
|
43
43
|
path = (@_api_infos ||= { })[routes_info[:path]] ||= { }
|
44
44
|
current_api = path[routes_info[:http_verb]] =
|
45
|
-
ApiInfoObj.new(action_path, skip: skip, use: use)
|
45
|
+
ApiInfoObj.new(action_path, skip: Array(skip), use: Array(use))
|
46
46
|
.merge! description: '', summary: summary, operationId: method, tags: [@_apis_tag],
|
47
47
|
parameters: [ ], requestBody: '', responses: { }, security: [ ], servers: [ ]
|
48
48
|
|
49
49
|
current_api.tap do |api|
|
50
50
|
[method, :all].each do |key| # blocks_store_key
|
51
|
-
@_apis_blocks&.[](key)&.each { |blk| api.instance_eval
|
51
|
+
@_apis_blocks&.[](key)&.each { |blk| api.instance_eval(&blk) }
|
52
52
|
end
|
53
53
|
api.param_use = [ ] # skip 和 use 是对 dry 块而言的
|
54
|
-
api.instance_eval
|
54
|
+
api.instance_eval(&block) if block_given?
|
55
55
|
api._process_objs
|
56
56
|
api.delete_if { |_, v| v.blank? }
|
57
57
|
end
|
@@ -64,12 +64,12 @@ module OpenApi
|
|
64
64
|
self[:parameters].concat([component_key].concat(keys).map { |key| RefObj.new(:parameter, key).process })
|
65
65
|
end
|
66
66
|
|
67
|
-
def request_body required, media_type, desc = '',
|
68
|
-
self[:requestBody] = RequestBodyObj.new(required, media_type, desc,
|
67
|
+
def request_body required, media_type, desc = '', hash = { }
|
68
|
+
self[:requestBody] = RequestBodyObj.new(required, media_type, desc, hash).process
|
69
69
|
end
|
70
70
|
|
71
|
-
def _request_body_agent media_type, desc = '',
|
72
|
-
request_body (@method_name['!'] ? :req : :opt), media_type, desc,
|
71
|
+
def _request_body_agent media_type, desc = '', hash = { }
|
72
|
+
request_body (@method_name['!'] ? :req : :opt), media_type, desc, hash
|
73
73
|
end
|
74
74
|
|
75
75
|
def body_ref component_key
|
@@ -88,17 +88,21 @@ module OpenApi
|
|
88
88
|
end
|
89
89
|
|
90
90
|
# TODO: 目前只能写一句 request body,包括 form 和 file, 需要同时支持一下扁平化
|
91
|
-
def form desc = '',
|
92
|
-
body :form, desc,
|
91
|
+
def form desc = '', hash = { }
|
92
|
+
body :form, desc, hash
|
93
93
|
end
|
94
|
-
|
95
|
-
|
94
|
+
|
95
|
+
def form! desc = '', hash = { }
|
96
|
+
body! :form, desc, hash
|
96
97
|
end
|
97
|
-
|
98
|
-
|
98
|
+
|
99
|
+
# TODO: 这种情况下 form 和 file 无法共存,需要解决(通过 Discriminator?)
|
100
|
+
def file media_type, desc = '', hash = { type: File }
|
101
|
+
body media_type, desc, hash
|
99
102
|
end
|
100
|
-
|
101
|
-
|
103
|
+
|
104
|
+
def file! media_type, desc = '', hash = { type: File }
|
105
|
+
body! media_type, desc, hash
|
102
106
|
end
|
103
107
|
|
104
108
|
def security scheme_name, requirements = [ ]
|
@@ -23,12 +23,12 @@ module OpenApi
|
|
23
23
|
end
|
24
24
|
|
25
25
|
# `code`: when defining components, `code` means `component_key`
|
26
|
-
def response code, desc, media_type = nil,
|
27
|
-
(self[:responses] ||= { })[code] = ResponseObj.new(desc, media_type,
|
26
|
+
def response code, desc, media_type = nil, hash = { }
|
27
|
+
(self[:responses] ||= { })[code] = ResponseObj.new(desc, media_type, hash)
|
28
28
|
end
|
29
29
|
|
30
|
-
def default_response desc, media_type = nil,
|
31
|
-
response :default, desc, media_type,
|
30
|
+
def default_response desc, media_type = nil, hash = { }
|
31
|
+
response :default, desc, media_type, hash
|
32
32
|
end
|
33
33
|
|
34
34
|
{ # alias_methods mapping
|
@@ -11,6 +11,10 @@ module OpenApi
|
|
11
11
|
end
|
12
12
|
arrow_enable :schema
|
13
13
|
|
14
|
+
def example summary, example_hash
|
15
|
+
# TODO
|
16
|
+
end
|
17
|
+
|
14
18
|
def param component_key, param_type, name, type, required, schema_hash = { }
|
15
19
|
(self[:parameters] ||= { })[component_key] =
|
16
20
|
ParamObj.new(name, param_type, type, required, schema_hash).process
|
data/lib/open_api/generator.rb
CHANGED
@@ -36,6 +36,9 @@ module OpenApi
|
|
36
36
|
doc[:components].merge! ctrl_infos[:components] || { }
|
37
37
|
end
|
38
38
|
doc[:components].delete_if { |_, v| v.blank? }
|
39
|
+
doc[:tags] = doc[:tags].sort { |a, b| a[:name] <=> b[:name] }
|
40
|
+
doc[:paths] = doc[:paths].sort.to_h
|
41
|
+
|
39
42
|
($open_apis ||= { })[api_name] ||=
|
40
43
|
ActiveSupport::HashWithIndifferentAccess.new(doc.delete_if { |_, v| v.blank? })
|
41
44
|
end
|
data/lib/open_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zero-rails_openapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhandao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- documentation/examples/goods_doc.rb
|
82
82
|
- documentation/examples/open_api.rb
|
83
83
|
- documentation/parameter.md
|
84
|
+
- lib/oas_objs/example_obj.rb
|
84
85
|
- lib/oas_objs/helpers.rb
|
85
86
|
- lib/oas_objs/media_type_obj.rb
|
86
87
|
- lib/oas_objs/param_obj.rb
|
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
requirements: []
|
121
122
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.6.
|
123
|
+
rubygems_version: 2.6.12
|
123
124
|
signing_key:
|
124
125
|
specification_version: 4
|
125
126
|
summary: Generate the OpenAPI Specification 3 documentation for Rails application.
|