60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/scorpio/google_api_document.rb', line 60
def to_openapi_hash(options = {})
ad = self
ad_methods = []
if ad['methods']
ad_methods += ad['methods'].map do |mn, m|
m.tap do
m.send(:define_singleton_method, :resource_name) { }
m.send(:define_singleton_method, :method_name) { mn }
end
end
end
ad_methods += ad.resources.map do |rn, r|
(r['methods'] || {}).map do |mn, m|
m.tap do
m.send(:define_singleton_method, :resource_name) { rn }
m.send(:define_singleton_method, :method_name) { mn }
end
end
end.inject([], &:+)
paths = ad_methods.group_by { |m| m['path'] }.map do |path, path_methods|
unless path =~ %r(\A/)
path = '/' + path
end
operations = path_methods.group_by { |m| m['httpMethod'] }.map do |http_method, http_method_methods|
if http_method_methods.size > 1
end
method = http_method_methods.first
unused_path_params = Addressable::Template.new(path).variables
{http_method.downcase => {}.tap do |operation|
operation['tags'] = method.resource_name ? [method.resource_name] : []
operation['description'] = method['description'] if method['description']
operation['operationId'] = method['id'] || (method.resource_name ? "#{method.resource_name}.#{method.method_name}" : method.method_name)
if method['parameters']
operation['parameters'] = method['parameters'].map do |name, parameter|
{}.tap do |op_param|
op_param['description'] = parameter.description if parameter.description
op_param['name'] = name
op_param['in'] = if parameter.location
parameter.location
elsif unused_path_params.include?(name)
'path'
else
'query'
end
unused_path_params.delete(name) if op_param['in'] == 'path'
op_param['required'] = parameter.key?('required') ? parameter['required'] : op_param['in'] == 'path' ? true : false
op_param['type'] = parameter.type || 'string'
op_param['format'] = parameter['format'] if parameter['format']
end
end
end
if unused_path_params.any?
operation['parameters'] ||= []
operation['parameters'] += unused_path_params.map do |param_name|
{
'name' => param_name,
'in' => 'path',
'required' => true,
'type' => 'string',
}
end
end
if method['request']
operation['parameters'] ||= []
operation['parameters'] << {
'name' => 'body',
'in' => 'body',
'required' => true,
'schema' => method['request'],
}
end
if method['response']
operation['responses'] = {
'default' => {
'description' => 'default response',
'schema' => method['response'],
},
}
end
end}
end.inject({}, &:update)
{path => operations}
end.inject({}, &:update)
openapi = {
'swagger' => '2.0',
'info' => { 'title' => ad.title || ad.name,
'description' => ad.description,
'version' => ad.version || '',
'contact' => {
'name' => ad.ownerName,
}.reject { |_, v| v.nil? },
},
'host' => ad.rootUrl ? Addressable::URI.parse(ad.rootUrl).host : ad.baseUrl ? Addressable::URI.parse(ad.baseUrl).host : ad.name, 'basePath' => begin
path = ad.servicePath || ad.basePath || (ad.baseUrl ? Addressable::URI.parse(ad.baseUrl).path : '/')
path =~ %r(\A/) ? path : "/" + path
end,
'schemes' => ad.rootUrl ? [Addressable::URI.parse(ad.rootUrl).scheme] : ad.baseUrl ? [Addressable::URI.parse(ad.rootUrl).scheme] : [], 'consumes' => ['application/json'], 'produces' => ['application/json'],
'tags' => paths.flat_map { |_, p| p.flat_map { |_, op| (op['tags'] || []).map { |n| {'name' => n} } } }.uniq,
'paths' => paths, }
if ad.schemas
openapi['definitions'] = ad.schemas
ad.schemas.each do |name, schema|
openapi = JSI::Util.ycomb do |rec|
proc do |object|
if object.respond_to?(:to_hash)
object.merge(object.map do |k, v|
if k == '$ref' && (v == schema['id'] || v == "#/schemas/#{name}" || v == name)
{k => "#/definitions/#{name}"}
else
JSI::Util.ycomb do |toopenapirec|
proc do |toopenapiobject|
toopenapiobject = toopenapiobject.to_openapi if toopenapiobject.respond_to?(:to_openapi)
if toopenapiobject.respond_to?(:to_hash)
toopenapiobject.map { |k2, v2| {toopenapirec.call(k2) => toopenapirec.call(v2)} }.inject({}, &:update)
elsif toopenapiobject.respond_to?(:to_ary)
toopenapiobject.map(&toopenapirec)
elsif toopenapiobject.is_a?(Symbol)
toopenapiobject.to_s
elsif [String, TrueClass, FalseClass, NilClass, Numeric].any? { |c| toopenapiobject.is_a?(c) }
toopenapiobject
else
raise(TypeError, "bad (not jsonifiable) object: #{toopenapiobject.pretty_inspect}")
end
end
end.call({k => rec.call(v)})
end
end.inject({}, &:merge))
elsif object.respond_to?(:to_ary)
object.map(&rec)
else
object
end
end
end.call(openapi)
end
end
JSI::Util.as_json(openapi)
end
|