-
Notifications
You must be signed in to change notification settings - Fork 64
/
schema_builder.rb
234 lines (201 loc) · 5.83 KB
/
schema_builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# frozen_string_literal: true
class << RSpec::OpenAPI::SchemaBuilder = Object.new
# @param [RSpec::OpenAPI::Record] record
# @return [Hash]
def build(record)
response = {
description: record.description,
}
response_headers = build_response_headers(record)
response[:headers] = response_headers unless response_headers.empty?
if record.response_body
disposition = normalize_content_disposition(record.response_content_disposition)
has_content = !normalize_content_type(record.response_content_type).nil?
if has_content
response[:content] = {
normalize_content_type(record.response_content_type) => {
schema: build_property(record.response_body, disposition: disposition),
example: response_example(record, disposition: disposition),
}.compact,
}
end
end
http_method = record.http_method.downcase
{
paths: {
normalize_path(record.path) => {
http_method => {
summary: record.summary,
tags: record.tags,
operationId: record.operation_id,
security: record.security,
deprecated: record.deprecated ? true : nil,
parameters: build_parameters(record),
requestBody: http_method == 'get' ? nil : build_request_body(record),
responses: {
record.status.to_s => response,
},
}.compact,
},
},
}
end
private
def enrich_with_required_keys(obj)
obj[:required] = obj[:properties]&.keys || []
obj
end
def response_example(record, disposition:)
return nil if !example_enabled? || disposition
record.response_body
end
def example_enabled?
RSpec::OpenAPI.enable_example
end
def build_parameters(record)
path_params = record.path_params.map do |key, value|
{
name: build_parameter_name(key, value),
in: 'path',
required: true,
schema: build_property(try_cast(value)),
example: (try_cast(value) if example_enabled?),
}.compact
end
query_params = record.query_params.map do |key, value|
{
name: build_parameter_name(key, value),
in: 'query',
required: record.required_request_params.include?(key),
schema: build_property(try_cast(value)),
example: (try_cast(value) if example_enabled?),
}.compact
end
header_params = record.request_headers.map do |key, value|
{
name: build_parameter_name(key, value),
in: 'header',
required: true,
schema: build_property(try_cast(value)),
example: (try_cast(value) if example_enabled?),
}.compact
end
parameters = path_params + query_params + header_params
return nil if parameters.empty?
parameters
end
def build_response_headers(record)
headers = {}
record.response_headers.each do |key, value|
headers[key] = {
schema: build_property(try_cast(value)),
}.compact
end
headers
end
def build_parameter_name(key, value)
key = key.to_s
if value.is_a?(Hash) && (value_keys = value.keys).size == 1
value_key = value_keys.first
build_parameter_name("#{key}[#{value_key}]", value[value_key])
else
key
end
end
def build_request_body(record)
return nil if record.request_content_type.nil?
return nil if record.status >= 400
{
content: {
normalize_content_type(record.request_content_type) => {
schema: build_property(record.request_params),
example: (build_example(record.request_params) if example_enabled?),
}.compact,
},
}
end
def build_property(value, disposition: nil)
property = build_type(value, disposition)
case value
when Array
property[:items] = if value.empty?
{} # unknown
else
build_property(value.first)
end
when Hash
property[:properties] = {}.tap do |properties|
value.each do |key, v|
properties[key] = build_property(v)
end
end
property = enrich_with_required_keys(property)
end
property
end
def build_type(value, disposition)
return { type: 'string', format: 'binary' } if disposition
case value
when String
{ type: 'string' }
when Integer
{ type: 'integer' }
when Float
{ type: 'number', format: 'float' }
when TrueClass, FalseClass
{ type: 'boolean' }
when Array
{ type: 'array' }
when Hash
{ type: 'object' }
when ActionDispatch::Http::UploadedFile
{ type: 'string', format: 'binary' }
when NilClass
{ nullable: true }
else
raise NotImplementedError, "type detection is not implemented for: #{value.inspect}"
end
end
# Convert an always-String param to an appropriate type
def try_cast(value)
Integer(value)
rescue TypeError, ArgumentError
value
end
def build_example(value)
return nil if value.nil?
value = value.dup
adjust_params(value)
end
def adjust_params(value)
value.each do |key, v|
case v
when ActionDispatch::Http::UploadedFile
value[key] = v.original_filename
when Hash
adjust_params(v)
when Array
result = v.map do |item|
case item
when ActionDispatch::Http::UploadedFile
item.original_filename
when Hash
adjust_params(item)
else
item
end
end
value[key] = result
end
end
end
def normalize_path(path)
path.gsub(%r{/:([^:/]+)}, '/{\1}')
end
def normalize_content_type(content_type)
content_type&.sub(/;.+\z/, '')
end
def normalize_content_disposition(content_disposition)
content_disposition&.sub(/;.+\z/, '')
end
end