rspec_api_documentation-open_api 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -3
- data/bin/setup +0 -2
- data/lib/rspec_api_documentation/open_api/version.rb +1 -1
- data/lib/rspec_api_documentation/writers/open_api_json_writer.rb +1 -1
- data/lib/rspec_api_documentation/writers/open_api_writer.rb +135 -135
- data/lib/rspec_api_documentation/writers/open_api_yaml_writer.rb +1 -1
- data/rspec_api_documentation-open_api.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce385336b5da4fccad72237ddc1fa72631d18e8f4c63613e4926b79add0a9826
|
4
|
+
data.tar.gz: 67ef06d2de2781000aba3e280c13d3caeca075d2540158425241fc4fd035a11f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6682f1fadce890fa519b9a299652587277427a3ce9fab2f3d0df1cfc7c07eaf7de5cf4d46c71126d25939e42581130a7f3c7be4504566ed6678ccd141e2672f8
|
7
|
+
data.tar.gz: 4c3f2b975dc713f685fccbbdb06d39daf65b4511d75c93fc9272145c032fad13c389911b437d87ef7fe55b4df2df3e89fcf1c52bc682df9a28172e4d03204350
|
data/.rubocop.yml
CHANGED
data/bin/setup
CHANGED
@@ -2,7 +2,7 @@ module RspecApiDocumentation
|
|
2
2
|
module Writers
|
3
3
|
class OpenApiJsonWriter < OpenApiWriter
|
4
4
|
def write
|
5
|
-
File.open(configuration.docs_dir.join(
|
5
|
+
File.open(configuration.docs_dir.join('open_api.json'), 'w') do |f|
|
6
6
|
f.write JSON.pretty_generate(get_hash)
|
7
7
|
end
|
8
8
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'json'
|
2
|
+
require 'yaml'
|
3
|
+
require 'cgi'
|
4
|
+
require 'rspec_api_documentation/writers/json_writer'
|
5
5
|
|
6
6
|
module RspecApiDocumentation
|
7
7
|
module Writers
|
@@ -9,11 +9,11 @@ module RspecApiDocumentation
|
|
9
9
|
attr_writer :types, :swagger
|
10
10
|
|
11
11
|
def write
|
12
|
-
File.open(configuration.docs_dir.join(
|
12
|
+
File.open(configuration.docs_dir.join('open_api.json'), 'w') do |f|
|
13
13
|
f.write JSON.pretty_generate(get_hash)
|
14
14
|
end
|
15
15
|
|
16
|
-
File.open(configuration.docs_dir.join(
|
16
|
+
File.open(configuration.docs_dir.join('open_api.yaml'), 'w') do |f|
|
17
17
|
f.write get_hash.to_yaml
|
18
18
|
end
|
19
19
|
end
|
@@ -21,157 +21,157 @@ module RspecApiDocumentation
|
|
21
21
|
def get_hash
|
22
22
|
index.examples.each do |rspec_example|
|
23
23
|
api = JSONExample.new(rspec_example, configuration).as_json.deep_stringify_keys
|
24
|
-
description = api[
|
25
|
-
route = api[
|
24
|
+
description = api['description']
|
25
|
+
route = api['route'].gsub(%r{\:version\/}, '')
|
26
26
|
route = route.gsub(%r{\:([^\/]+)}, '{\1}')
|
27
|
-
params = route.scan(%r{\{([^\/]+)\}}).map { |param| {
|
28
|
-
swagger[
|
27
|
+
params = route.scan(%r{\{([^\/]+)\}}).map { |param| { 'name' => param[0], 'in' => 'path', 'required' => true, 'schema' => { 'type' => 'string' } } }
|
28
|
+
swagger['paths'][route] = swagger['paths'][route] || {}
|
29
29
|
responses = {}
|
30
|
-
req = api[
|
31
|
-
method = req[
|
32
|
-
result = JSON.parse(req[
|
30
|
+
req = api['requests'][0]
|
31
|
+
method = req['request_method'].downcase
|
32
|
+
result = JSON.parse(req['response_body']) if req['response_body']
|
33
33
|
properties = {}
|
34
34
|
result&.each { |k, v| properties[k] = get_properties(v) }
|
35
35
|
|
36
|
-
responses[req[
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
36
|
+
responses[req['response_status']] = {
|
37
|
+
'description' => req['response_status_text'],
|
38
|
+
'headers' => req['response_headers'].map { |name, _v| [name, { 'schema' => { 'type' => 'string' } }] }.to_h,
|
39
|
+
'content' => {
|
40
|
+
'application/json' => {
|
41
|
+
'schema' => {
|
42
|
+
'type' => 'object',
|
43
|
+
'properties' => properties
|
44
44
|
}
|
45
45
|
}
|
46
46
|
}
|
47
47
|
}
|
48
|
-
api[
|
49
|
-
params.push(
|
50
|
-
|
51
|
-
|
52
|
-
|
48
|
+
api['parameters']&.each do |param|
|
49
|
+
params.push('name' => param['name'],
|
50
|
+
'in' => 'query',
|
51
|
+
'schema' => {
|
52
|
+
'type' => 'string'
|
53
53
|
},
|
54
|
-
|
55
|
-
|
54
|
+
'description' => param['description'],
|
55
|
+
'required' => param['required'] || false)
|
56
56
|
end
|
57
57
|
|
58
|
-
req[
|
59
|
-
params.push(
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
req['request_query_parameters']&.each do |name, value|
|
59
|
+
params.push('name' => name,
|
60
|
+
'in' => 'query',
|
61
|
+
'schema' => {
|
62
|
+
'type' => 'string'
|
63
63
|
},
|
64
|
-
|
64
|
+
'example' => value)
|
65
65
|
end
|
66
|
-
if req[
|
67
|
-
req[
|
68
|
-
params.push(
|
69
|
-
|
70
|
-
|
71
|
-
|
66
|
+
if req['request_content_type'] == 'application/x-www-form-urlencoded' && api['requests'][0]['request_body']
|
67
|
+
req['request_body'].scan(/([^\&\=]*)=([^\&]*)/).map do |body|
|
68
|
+
params.push('name' => body[0],
|
69
|
+
'in' => 'query',
|
70
|
+
'schema' => {
|
71
|
+
'type' => 'string'
|
72
72
|
},
|
73
|
-
|
73
|
+
'example' => CGI.unescape(body[1]))
|
74
74
|
end
|
75
75
|
end
|
76
|
-
swagger[
|
77
|
-
swagger[
|
76
|
+
swagger['paths'][route][method] = swagger['paths'][route][method] || {}
|
77
|
+
swagger['paths'][route][method]['parameters'] = swagger['paths'][route][method]['parameters'] || []
|
78
78
|
|
79
|
-
req[
|
80
|
-
if name ==
|
81
|
-
swagger[
|
82
|
-
unless responses[401] && responses[
|
79
|
+
req['request_headers']&.each do |name, value|
|
80
|
+
if name == 'Authorization'
|
81
|
+
swagger['paths'][route][method]['security'] = if /Bearer (.*)/.match?(value)
|
82
|
+
unless responses[401] && responses['401']
|
83
83
|
responses[401] = {
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
84
|
+
'description' => 'Unauthorized Access',
|
85
|
+
'content' => {
|
86
|
+
'application/json' => {
|
87
|
+
'schema' => {
|
88
|
+
'$ref' => '#/components/schemas/unauthorized'
|
89
89
|
}
|
90
90
|
}
|
91
91
|
}
|
92
92
|
}
|
93
93
|
end
|
94
|
-
[{
|
94
|
+
[{ 'bearerAuth' => [] }]
|
95
95
|
else
|
96
|
-
[{
|
96
|
+
[{ 'basicAuth' => [] }]
|
97
97
|
end
|
98
98
|
else
|
99
|
-
params.unshift(
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
params.unshift('name' => name,
|
100
|
+
'in' => 'header',
|
101
|
+
'schema' => {
|
102
|
+
'type' => 'string'
|
103
103
|
},
|
104
|
-
|
105
|
-
|
104
|
+
'example' => value,
|
105
|
+
'required' => true)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
params.each do |param|
|
109
109
|
has = false
|
110
|
-
swagger[
|
111
|
-
if p[
|
112
|
-
swagger[
|
110
|
+
swagger['paths'][route][method]['parameters'].each_with_index do |p, i|
|
111
|
+
if p['name'] == param['name']
|
112
|
+
swagger['paths'][route][method]['parameters'][i]['example'] = swagger['paths'][route][method]['parameters'][i]['example'] || param['example']
|
113
113
|
has = true
|
114
114
|
end
|
115
115
|
end
|
116
|
-
swagger[
|
116
|
+
swagger['paths'][route][method]['parameters'].push(param) unless has
|
117
117
|
end
|
118
|
-
desc = swagger[
|
119
|
-
swagger[
|
120
|
-
if params.empty?
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
swagger[
|
118
|
+
desc = swagger['paths'][route][method]['description'] || ''
|
119
|
+
swagger['paths'][route][method]['description'] = desc + "- #{description}"
|
120
|
+
swagger['paths'][route][method]['description'] += if params.empty?
|
121
|
+
"\n"
|
122
|
+
else
|
123
|
+
", <strong>Needed Parameters:</strong>\n - #{params.map{ |p| p['name'] }.join("\n - ")} \n"
|
124
|
+
end
|
125
|
+
swagger['paths'][route][method]['responses'] = responses
|
126
126
|
end
|
127
|
-
swagger[
|
127
|
+
swagger['components']['schemas'] = types
|
128
128
|
|
129
129
|
swagger
|
130
130
|
end
|
131
131
|
|
132
132
|
def get_properties(v)
|
133
133
|
case v.class.name
|
134
|
-
when
|
134
|
+
when 'Hash'
|
135
135
|
props = v.map { |key, value| [key, get_properties(value)] }.to_h
|
136
136
|
x = {
|
137
|
-
|
138
|
-
|
137
|
+
'type' => 'object',
|
138
|
+
'properties' => props
|
139
139
|
}
|
140
|
-
if v[
|
141
|
-
type = v[
|
140
|
+
if v['type']
|
141
|
+
type = v['type'].tr('\/', '_')
|
142
142
|
types[type] = x unless types.key?(type)
|
143
143
|
if types.key?(type)
|
144
|
-
types[type][
|
144
|
+
types[type]['properties'] = hash_deep_assign(types[type]['properties'], x['properties'])
|
145
145
|
else
|
146
146
|
types[type] = x
|
147
147
|
end
|
148
|
-
return {
|
148
|
+
return { '$ref' => "#/components/schemas/#{type}" }
|
149
149
|
end
|
150
150
|
x
|
151
|
-
when
|
151
|
+
when 'Array'
|
152
152
|
{
|
153
|
-
|
154
|
-
|
153
|
+
'type' => 'array',
|
154
|
+
'items' => get_properties(v[0])
|
155
155
|
}
|
156
|
-
when
|
156
|
+
when 'TrueClass', 'FalseClass'
|
157
157
|
{
|
158
|
-
|
159
|
-
|
158
|
+
'type' => 'boolean',
|
159
|
+
'example' => v
|
160
160
|
}
|
161
|
-
when
|
161
|
+
when 'NilClass'
|
162
162
|
{
|
163
|
-
|
164
|
-
|
163
|
+
'type' => 'integer',
|
164
|
+
'nullable' => true
|
165
165
|
}
|
166
|
-
when
|
166
|
+
when 'Integer', 'Float'
|
167
167
|
{
|
168
|
-
|
169
|
-
|
168
|
+
'type' => 'number',
|
169
|
+
'example' => v
|
170
170
|
}
|
171
171
|
else
|
172
172
|
{
|
173
|
-
|
174
|
-
|
173
|
+
'type' => v.class.name.downcase,
|
174
|
+
'example' => v
|
175
175
|
}
|
176
176
|
end
|
177
177
|
end
|
@@ -183,18 +183,18 @@ module RspecApiDocumentation
|
|
183
183
|
hash_deep_assign(target[key], other[key])
|
184
184
|
elsif target.key?(key) && target[key].is_a?(Array) && other[key].is_a?(Array)
|
185
185
|
target[key].push(*other[key]).uniq
|
186
|
-
elsif key ==
|
187
|
-
if other[
|
186
|
+
elsif key == '$ref'
|
187
|
+
if other['$ref'] && target['$ref'] && target['$ref'] != other['$ref']
|
188
188
|
target = {
|
189
|
-
|
190
|
-
{
|
191
|
-
{
|
189
|
+
'oneOf' => [
|
190
|
+
{ '$ref' => target['$ref'] },
|
191
|
+
{ '$ref' => other['$ref'] }
|
192
192
|
]
|
193
193
|
}
|
194
|
-
elsif target.key?(
|
195
|
-
target[
|
194
|
+
elsif target.key?('oneOf')
|
195
|
+
target['oneOf'].push('$ref' => other['$ref']).uniq
|
196
196
|
else
|
197
|
-
target[
|
197
|
+
target['$ref'] || other['$ref']
|
198
198
|
end
|
199
199
|
elsif target.key?(key)
|
200
200
|
target[key] || other[key]
|
@@ -202,23 +202,23 @@ module RspecApiDocumentation
|
|
202
202
|
value
|
203
203
|
end
|
204
204
|
end
|
205
|
-
target = {
|
206
|
-
target = {
|
207
|
-
target = get_properties(target[
|
205
|
+
target = { 'oneOf' => target['oneOf'] } if target.key?('oneOf')
|
206
|
+
target = { '$ref' => target['$ref'] } if target.key?('$ref')
|
207
|
+
target = get_properties(target['example']) if target['example'] && target.key?('nullable')
|
208
208
|
target
|
209
209
|
end
|
210
210
|
|
211
211
|
def types
|
212
212
|
@types ||= {
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
213
|
+
'unauthorized' => {
|
214
|
+
'properties' => {
|
215
|
+
'code' => {
|
216
|
+
'type' => 'string',
|
217
|
+
'example' => 'invalid_client_credentials'
|
218
218
|
},
|
219
|
-
|
220
|
-
|
221
|
-
|
219
|
+
'message' => {
|
220
|
+
'type' => 'string',
|
221
|
+
'example' => 'Not found or invalid client credentials'
|
222
222
|
}
|
223
223
|
}
|
224
224
|
}
|
@@ -227,32 +227,32 @@ module RspecApiDocumentation
|
|
227
227
|
|
228
228
|
def swagger
|
229
229
|
@swagger ||= {
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
230
|
+
'openapi' => '3.0.0',
|
231
|
+
'info' => info,
|
232
|
+
'servers' => servers,
|
233
|
+
'paths' => {},
|
234
|
+
'components' => {
|
235
|
+
'securitySchemes' => {
|
236
|
+
'bearerAuth' => {
|
237
|
+
'type' => 'http',
|
238
|
+
'scheme' => 'bearer'
|
239
239
|
},
|
240
|
-
|
241
|
-
|
242
|
-
|
240
|
+
'basicAuth' => {
|
241
|
+
'type' => 'http',
|
242
|
+
'scheme' => 'basic'
|
243
243
|
}
|
244
244
|
},
|
245
|
-
|
245
|
+
'schemas' => {}
|
246
246
|
}
|
247
247
|
}
|
248
248
|
end
|
249
249
|
|
250
250
|
def info
|
251
|
-
configs[
|
251
|
+
configs['info']
|
252
252
|
end
|
253
253
|
|
254
254
|
def servers
|
255
|
-
configs[
|
255
|
+
configs['servers']
|
256
256
|
end
|
257
257
|
|
258
258
|
def configs
|
@@ -266,20 +266,20 @@ module RspecApiDocumentation
|
|
266
266
|
def default_configs
|
267
267
|
{
|
268
268
|
info: {
|
269
|
-
version:
|
270
|
-
title:
|
271
|
-
description:
|
269
|
+
version: '1.0.0',
|
270
|
+
title: 'Open API',
|
271
|
+
description: 'Open API',
|
272
272
|
contact: {
|
273
|
-
name:
|
273
|
+
name: 'OpenAPI'
|
274
274
|
}
|
275
275
|
},
|
276
276
|
servers: [
|
277
277
|
{
|
278
|
-
url:
|
279
|
-
description:
|
278
|
+
url: 'http://localhost:{port}',
|
279
|
+
description: 'Development server',
|
280
280
|
variables: {
|
281
281
|
port: {
|
282
|
-
default:
|
282
|
+
default: '3000'
|
283
283
|
}
|
284
284
|
}
|
285
285
|
}
|
@@ -2,7 +2,7 @@ module RspecApiDocumentation
|
|
2
2
|
module Writers
|
3
3
|
class OpenApiYamlWriter < OpenApiWriter
|
4
4
|
def write
|
5
|
-
File.open(configuration.docs_dir.join(
|
5
|
+
File.open(configuration.docs_dir.join('open_api.yaml'), 'w') do |f|
|
6
6
|
f.write get_hash.to_yaml
|
7
7
|
end
|
8
8
|
end
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
-
spec.add_dependency "rspec_api_documentation", "~>
|
33
|
+
spec.add_dependency "rspec_api_documentation", "~> 6.0"
|
34
34
|
|
35
35
|
spec.add_development_dependency "bundler", "~> 1.16"
|
36
36
|
spec.add_development_dependency "minitest", "~> 5.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_api_documentation-open_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaditya Taparia
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec_api_documentation
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|