Class: SwaggerYard::OpenAPI
Instance Attribute Summary
Attributes inherited from Swagger
#specification
Instance Method Summary
collapse
Methods inherited from Swagger
#initialize, new, object_new
Instance Method Details
#components ⇒ Object
27
28
29
30
31
32
|
# File 'lib/swagger_yard/openapi.rb', line 27
def components
{
"schemas" => models(specification.model_objects),
"securitySchemes" => security_defs(specification.security_objects)
}
end
|
#definitions ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/swagger_yard/openapi.rb', line 19
def definitions
{
"paths" => paths(specification.path_objects),
"tags" => tags(specification.tag_objects),
"components" => components
}
end
|
#map_security(h) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/swagger_yard/openapi.rb', line 93
def map_security(h)
h = Hash[h.map { |k, v| [k.to_s, v] }] case type = h['type'].to_s
when 'apiKey', 'http'
h
when 'oauth2'
if (authUrl = h.delete('authorizationUrl')) && (flow = h.delete('flow'))
{ 'type' => 'oauth2', 'flows' => {
flow => { 'authorizationUrl' => authUrl } } }.tap do |result|
(h.keys - ['type']).each do |t|
result['flows'][flow][t] = h[t]
end
result['flows'][flow]['scopes'] = {} unless result['flows'][flow]['scopes']
end
else
h
end
else
{ 'type' => 'http', 'scheme' => type }.tap do |result|
result['bearerFormat'] = h['format'] if h['format']
end
end.tap do |result|
result['description'] = h['description'] unless h['description'].nil? || h['description'].empty?
end
end
|
11
12
13
14
15
16
17
|
# File 'lib/swagger_yard/openapi.rb', line 11
def metadata
{
'openapi' => '3.0.0',
'info' => Info.new.to_h,
'servers' => [{'url' => SwaggerYard.config.api_base_path}]
}
end
|
#model_path ⇒ Object
7
8
9
|
# File 'lib/swagger_yard/openapi.rb', line 7
def model_path
'#/components/schemas/'
end
|
#operation(op) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/swagger_yard/openapi.rb', line 48
def operation(op)
op_hash = super
if body_param = op.parameters.detect { |p| p.param_type == 'body' }
op_hash['requestBody'] = {
'description' => body_param.description,
'content' => {
'application/json' => {
'schema' => body_param.type.schema_with(model_path: model_path)
}
}
}
end
op_hash
end
|
#parameters(params) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/swagger_yard/openapi.rb', line 34
def parameters(params)
params.select { |param| param.param_type != 'body' }.map do |param|
{ "name" => param.name,
"description" => param.description,
"required" => param.required,
"in" => param.param_type
}.tap do |h|
schema = param.type.schema_with(model_path: model_path)
h["schema"] = schema
h["explode"] = true if !Array(param.allow_multiple).empty? && schema["items"]
end
end
end
|
#response(resp, op) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/swagger_yard/openapi.rb', line 63
def response(resp, op)
{}.tap do |h|
h['description'] = resp && resp.description || op.summary || ''
if resp && resp.type && (schema = resp.type.schema_with(model_path: model_path))
h['content'] = { 'application/json' => { 'schema' => schema } }
h['content']['application/json']['example'] = resp.example if resp.example
end
end
end
|
#security(obj) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/swagger_yard/openapi.rb', line 80
def security(obj)
case obj.type
when /api_?key/i
{ 'type' => 'apiKey', 'name' => obj.key, 'in' => obj.name }
when /bearer/i
{ 'type' => obj.type, 'name' => obj.name, 'format' => obj.key }
else
{ 'type' => obj.type, 'name' => obj.name }
end.tap do |result|
result['description'] = obj.description if obj.description && !obj.description.empty?
end
end
|
#security_defs(security_objects) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/swagger_yard/openapi.rb', line 73
def security_defs(security_objects)
defs = super
Hash[defs.map do |name, d|
[name, map_security(d)]
end]
end
|
#to_h ⇒ Object
3
4
5
|
# File 'lib/swagger_yard/openapi.rb', line 3
def to_h
metadata.merge(definitions)
end
|