rspec-openapi 0.35.0 → 0.37.0
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/README.md +131 -3
- data/lib/rspec/openapi/components_updater.rb +16 -9
- data/lib/rspec/openapi/extractors/shared_extractor.rb +58 -11
- data/lib/rspec/openapi/hash_helper.rb +62 -13
- data/lib/rspec/openapi/key_transformer.rb +17 -7
- data/lib/rspec/openapi/minitest_hooks.rb +2 -1
- data/lib/rspec/openapi/parallel_records.rb +59 -4
- data/lib/rspec/openapi/partial_run.rb +84 -0
- data/lib/rspec/openapi/record.rb +2 -0
- data/lib/rspec/openapi/result_recorder.rb +21 -8
- data/lib/rspec/openapi/rspec_hooks.rb +11 -3
- data/lib/rspec/openapi/schema_builder/build_context.rb +19 -4
- data/lib/rspec/openapi/schema_builder/variation_merger.rb +74 -32
- data/lib/rspec/openapi/schema_builder.rb +116 -63
- data/lib/rspec/openapi/schema_cleaner.rb +89 -56
- data/lib/rspec/openapi/schema_file.rb +27 -20
- data/lib/rspec/openapi/schema_merger.rb +24 -6
- data/lib/rspec/openapi/schema_sorter.rb +24 -4
- data/lib/rspec/openapi/stream_parser.rb +4 -5
- data/lib/rspec/openapi/version.rb +1 -1
- data/lib/rspec/openapi/yaml_dumper.rb +103 -0
- data/lib/rspec/openapi/yaml_loader.rb +118 -0
- data/lib/rspec/openapi.rb +19 -7
- metadata +5 -2
|
@@ -7,6 +7,7 @@ require_relative 'schema_builder/variation_merger'
|
|
|
7
7
|
class << RSpec::OpenAPI::SchemaBuilder
|
|
8
8
|
# Record members holding the per-context lookup maps, by context (and kind).
|
|
9
9
|
ENUM_MEMBERS = { request: :request_enum, response: :response_enum }.freeze
|
|
10
|
+
READ_ONLY_MEMBERS = { request: :request_read_only, response: :response_read_only }.freeze
|
|
10
11
|
OVERRIDE_MEMBERS = {
|
|
11
12
|
additional_properties: {
|
|
12
13
|
request: :request_additional_properties, response: :response_additional_properties,
|
|
@@ -16,13 +17,20 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
16
17
|
},
|
|
17
18
|
}.freeze
|
|
18
19
|
|
|
20
|
+
# GET and DELETE never have a request body in OpenAPI.
|
|
21
|
+
BODYLESS_METHODS = ['delete', 'get'].freeze
|
|
22
|
+
|
|
23
|
+
# The schema holds symbol keys like the document, and nothing of the record:
|
|
24
|
+
# the data it carries is copied, so the merger can take the schema over.
|
|
25
|
+
#
|
|
19
26
|
# @param [RSpec::OpenAPI::Record] record
|
|
20
27
|
# @return [Hash]
|
|
21
28
|
def build(record)
|
|
29
|
+
http_method = record.http_method.downcase
|
|
22
30
|
{
|
|
23
31
|
paths: {
|
|
24
|
-
normalize_path(record.path) => {
|
|
25
|
-
|
|
32
|
+
normalize_path(record.path).to_sym => {
|
|
33
|
+
http_method.to_sym => build_operation(record, http_method),
|
|
26
34
|
},
|
|
27
35
|
},
|
|
28
36
|
}
|
|
@@ -30,20 +38,19 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
30
38
|
|
|
31
39
|
private
|
|
32
40
|
|
|
33
|
-
def build_operation(record)
|
|
34
|
-
|
|
35
|
-
# GET and DELETE never have a request body in OpenAPI.
|
|
36
|
-
request_body = ['delete', 'get'].include?(http_method) ? nil : build_request_body(record)
|
|
37
|
-
{
|
|
41
|
+
def build_operation(record, http_method)
|
|
42
|
+
operation = {
|
|
38
43
|
summary: record.summary,
|
|
39
|
-
tags: record.tags,
|
|
44
|
+
tags: copy(record.tags),
|
|
40
45
|
operationId: record.operation_id,
|
|
41
|
-
security: record.security,
|
|
46
|
+
security: copy(record.security),
|
|
42
47
|
deprecated: record.deprecated ? true : nil,
|
|
43
48
|
parameters: build_parameters(record),
|
|
44
|
-
requestBody:
|
|
45
|
-
responses: { record.status.to_s => build_response(record) },
|
|
46
|
-
}
|
|
49
|
+
requestBody: BODYLESS_METHODS.include?(http_method) ? nil : build_request_body(record),
|
|
50
|
+
responses: { record.status.to_s.to_sym => build_response(record) },
|
|
51
|
+
}
|
|
52
|
+
operation.compact!
|
|
53
|
+
operation
|
|
47
54
|
end
|
|
48
55
|
|
|
49
56
|
def build_response(record)
|
|
@@ -67,18 +74,18 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
67
74
|
def build_content(record)
|
|
68
75
|
disposition = normalize_content_disposition(record.response_content_disposition)
|
|
69
76
|
content_type = normalize_content_type(record.response_content_type)
|
|
70
|
-
ctx = BuildContext.
|
|
77
|
+
ctx = BuildContext.root(record, :response)
|
|
71
78
|
|
|
72
79
|
if RSpec::OpenAPI.supports_item_schema? && RSpec::OpenAPI.sequential_media_type?(content_type)
|
|
73
80
|
item_schema = build_stream_item_schema(record.response_body, content_type, ctx)
|
|
74
|
-
return { content_type => { itemSchema: item_schema } } if item_schema
|
|
81
|
+
return { content_type.to_sym => { itemSchema: item_schema } } if item_schema
|
|
75
82
|
end
|
|
76
83
|
|
|
77
84
|
schema = build_property(record.response_body, ctx, disposition: disposition)
|
|
78
85
|
example = response_example(record, disposition: disposition)
|
|
79
86
|
|
|
80
87
|
body = build_example_body(schema, record, mode: record.response_example_mode, example: example)
|
|
81
|
-
{ content_type => body }
|
|
88
|
+
{ content_type.to_sym => body }
|
|
82
89
|
end
|
|
83
90
|
|
|
84
91
|
def build_stream_item_schema(raw, content_type, ctx)
|
|
@@ -96,18 +103,30 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
96
103
|
|
|
97
104
|
case mode
|
|
98
105
|
when :multiple
|
|
99
|
-
{ schema: schema, examples: { record
|
|
106
|
+
{ schema: schema, examples: { example_name(record) => build_named_example(record, example) } }
|
|
100
107
|
else
|
|
101
108
|
# :single (default)
|
|
102
109
|
# :single may emit nil example or nil _example_summary; compact strips them.
|
|
103
|
-
|
|
110
|
+
body = {
|
|
111
|
+
schema: schema,
|
|
112
|
+
example: example,
|
|
113
|
+
_example_key: record.example_key,
|
|
114
|
+
_example_summary: example_summary(record),
|
|
115
|
+
}
|
|
116
|
+
body.compact!
|
|
117
|
+
body
|
|
104
118
|
end
|
|
105
119
|
end
|
|
106
120
|
|
|
107
121
|
def response_example(record, disposition:)
|
|
108
|
-
return nil if !example_enabled?(record) || disposition
|
|
122
|
+
return nil if !example_enabled?(record) || disposition || record.response_example_mode == :none
|
|
109
123
|
|
|
110
|
-
record.response_body
|
|
124
|
+
build_example(record.response_body)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Named like the loader names the examples it reads back.
|
|
128
|
+
def example_name(record)
|
|
129
|
+
RSpec::OpenAPI::ExampleKey.normalize(record.example_key).to_sym
|
|
111
130
|
end
|
|
112
131
|
|
|
113
132
|
def build_named_example(record, value)
|
|
@@ -115,10 +134,6 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
115
134
|
summary ? { summary: summary, value: value } : { value: value }
|
|
116
135
|
end
|
|
117
136
|
|
|
118
|
-
def example_metadata(record)
|
|
119
|
-
{ _example_key: record.example_key, _example_summary: example_summary(record) }
|
|
120
|
-
end
|
|
121
|
-
|
|
122
137
|
def example_summary(record)
|
|
123
138
|
return nil unless RSpec::OpenAPI.enable_example_summary
|
|
124
139
|
return nil if record.example_name.nil? || record.example_name.empty?
|
|
@@ -134,15 +149,12 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
134
149
|
parameters = record.path_params.map do |key, value|
|
|
135
150
|
build_parameter(key, value, location: 'path', record: record)
|
|
136
151
|
end
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
build_parameter(key, value, location: 'query', record: record)
|
|
152
|
+
flatten_query_params(record.query_params).each do |key, value|
|
|
153
|
+
parameters << build_parameter(key, value, location: 'query', record: record)
|
|
140
154
|
end
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
build_parameter(key, value, location: 'header', record: record)
|
|
155
|
+
record.request_headers.each do |key, value|
|
|
156
|
+
parameters << build_parameter(key, value, location: 'header', record: record)
|
|
144
157
|
end
|
|
145
|
-
|
|
146
158
|
parameters.empty? ? nil : parameters
|
|
147
159
|
end
|
|
148
160
|
|
|
@@ -152,21 +164,26 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
152
164
|
is_query = location == 'query'
|
|
153
165
|
required = is_query ? record.required_request_params.include?(key) : true
|
|
154
166
|
cast = try_cast(value)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
167
|
+
name = key.to_s
|
|
168
|
+
ctx = BuildContext.root(record, :request, name, key)
|
|
169
|
+
parameter = {
|
|
170
|
+
name: name,
|
|
158
171
|
in: location,
|
|
159
172
|
required: required,
|
|
160
173
|
schema: build_property(cast, ctx),
|
|
161
174
|
example: (cast if example_enabled?(record)),
|
|
162
|
-
}
|
|
175
|
+
}
|
|
176
|
+
parameter.compact!
|
|
177
|
+
parameter
|
|
163
178
|
end
|
|
164
179
|
|
|
165
180
|
def build_response_headers(record)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
181
|
+
headers = {}
|
|
182
|
+
record.response_headers.each do |key, value|
|
|
183
|
+
ctx = BuildContext.root(record, :response, key.to_s, key)
|
|
184
|
+
headers[key.to_sym] = { schema: build_property(try_cast(value), ctx) }
|
|
169
185
|
end
|
|
186
|
+
headers
|
|
170
187
|
end
|
|
171
188
|
|
|
172
189
|
def flatten_query_params(params, parent_key = nil)
|
|
@@ -186,18 +203,16 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
186
203
|
return nil if record.status >= 400 && record.request_example_mode != :multiple
|
|
187
204
|
|
|
188
205
|
content_type = normalize_content_type(record.request_content_type)
|
|
189
|
-
ctx = BuildContext.
|
|
206
|
+
ctx = BuildContext.root(record, :request)
|
|
190
207
|
schema = build_property(record.request_params, ctx)
|
|
191
|
-
example = example_enabled?(record)
|
|
208
|
+
example = build_example(record.request_params) if example_enabled?(record) && record.request_example_mode != :none
|
|
192
209
|
|
|
193
210
|
body = build_example_body(schema, record, mode: record.request_example_mode, example: example)
|
|
194
|
-
{ content: { content_type => body } }
|
|
211
|
+
{ content: { content_type.to_sym => body } }
|
|
195
212
|
end
|
|
196
213
|
|
|
197
214
|
def build_property(value, ctx, disposition: nil)
|
|
198
|
-
|
|
199
|
-
enum = infer_enum(ctx.path, ctx.record, ctx.context)
|
|
200
|
-
property = build_type(value, format: format, enum: enum)
|
|
215
|
+
property = build_type(value, format: property_format(value, ctx, disposition), enum: property_enum(value, ctx))
|
|
201
216
|
|
|
202
217
|
case value
|
|
203
218
|
when Array
|
|
@@ -208,20 +223,51 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
208
223
|
property
|
|
209
224
|
end
|
|
210
225
|
|
|
226
|
+
# A format and an enum describe a scalar. A null leaves them to another
|
|
227
|
+
# example, and an array to its items.
|
|
228
|
+
def scalar?(value)
|
|
229
|
+
!(value.nil? || value.is_a?(Array) || value.is_a?(Hash))
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def property_format(value, ctx, disposition)
|
|
233
|
+
return nil unless scalar?(value)
|
|
234
|
+
|
|
235
|
+
disposition ? 'binary' : infer_format(ctx.key, ctx.record)
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# An empty enum, such as one read from an empty table, would allow nothing.
|
|
239
|
+
def property_enum(value, ctx)
|
|
240
|
+
return nil unless scalar?(value)
|
|
241
|
+
|
|
242
|
+
enum = infer_enum(ctx.path, ctx.record, ctx.context)
|
|
243
|
+
enum unless enum == []
|
|
244
|
+
end
|
|
245
|
+
|
|
211
246
|
def apply_object_schema(property, value, ctx)
|
|
212
247
|
override = infer_override(ctx.path, ctx.record, ctx.context, :additional_properties)
|
|
213
248
|
|
|
214
249
|
if override.is_a?(Hash) && !override.empty?
|
|
215
250
|
# Schema override: the object's keys are dynamic — replace captured
|
|
216
251
|
# `properties` / `required` with the supplied dictionary value schema.
|
|
217
|
-
property[:additionalProperties] = override
|
|
252
|
+
property[:additionalProperties] = copy(override)
|
|
218
253
|
return
|
|
219
254
|
end
|
|
220
255
|
|
|
256
|
+
# Only an object property can be readOnly. A request that sends a candidate
|
|
257
|
+
# says false, which wins where the two sides meet in one schema (see
|
|
258
|
+
# SchemaMerger).
|
|
259
|
+
read_only = ctx.record[READ_ONLY_MEMBERS.fetch(ctx.context)]
|
|
221
260
|
properties = {}
|
|
222
|
-
|
|
261
|
+
required = []
|
|
262
|
+
value.each do |k, v|
|
|
263
|
+
child = build_property(v, ctx.descend(k))
|
|
264
|
+
name = k.to_s
|
|
265
|
+
child[:readOnly] = ctx.context == :response if read_only&.[](name)
|
|
266
|
+
properties[name.to_sym] = child
|
|
267
|
+
required << name
|
|
268
|
+
end
|
|
223
269
|
property[:properties] = properties
|
|
224
|
-
property[:required] =
|
|
270
|
+
property[:required] = required
|
|
225
271
|
apply_additional_properties(property, override,
|
|
226
272
|
infer_override(ctx.path, ctx.record, ctx.context, :hybrid_additional_properties),)
|
|
227
273
|
end
|
|
@@ -235,7 +281,7 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
235
281
|
if [true, false].include?(override)
|
|
236
282
|
property[:additionalProperties] = override
|
|
237
283
|
elsif hybrid_override.is_a?(Hash) && !hybrid_override.empty?
|
|
238
|
-
property[:additionalProperties] = hybrid_override
|
|
284
|
+
property[:additionalProperties] = copy(hybrid_override)
|
|
239
285
|
end
|
|
240
286
|
end
|
|
241
287
|
|
|
@@ -256,7 +302,7 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
256
302
|
end
|
|
257
303
|
end
|
|
258
304
|
|
|
259
|
-
result[:enum] = enum if enum
|
|
305
|
+
result[:enum] = copy(enum) if enum
|
|
260
306
|
result
|
|
261
307
|
end
|
|
262
308
|
|
|
@@ -296,31 +342,36 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
296
342
|
Integer(value, exception: false) || value
|
|
297
343
|
end
|
|
298
344
|
|
|
345
|
+
# A copy of a recorded value with symbol keys, and the filename of an upload.
|
|
299
346
|
def build_example(value)
|
|
300
|
-
return nil if value.nil?
|
|
301
|
-
|
|
302
|
-
adjust_value(value.dup)
|
|
303
|
-
end
|
|
304
|
-
|
|
305
|
-
def adjust_params(hash)
|
|
306
|
-
hash.transform_values! { |v| adjust_value(v) }
|
|
307
|
-
end
|
|
308
|
-
|
|
309
|
-
def adjust_value(value)
|
|
310
347
|
case value
|
|
348
|
+
when Hash
|
|
349
|
+
example = {}
|
|
350
|
+
value.each { |key, item| example[key.to_sym] = build_example(item) }
|
|
351
|
+
example
|
|
352
|
+
when Array then value.map { |item| build_example(item) }
|
|
311
353
|
when ActionDispatch::Http::UploadedFile then value.original_filename
|
|
312
|
-
when Hash then adjust_params(value)
|
|
313
|
-
when Array then value.map { |item| adjust_value(item) }
|
|
314
354
|
else value
|
|
315
355
|
end
|
|
316
356
|
end
|
|
317
357
|
|
|
358
|
+
# A record may hold what the metadata of a whole example group holds.
|
|
359
|
+
def copy(value)
|
|
360
|
+
RSpec::OpenAPI::KeyTransformer.symbolize(value)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# gsub copies the path even when nothing matches.
|
|
318
364
|
def normalize_path(path)
|
|
365
|
+
return path unless path.include?(':')
|
|
366
|
+
|
|
319
367
|
path.gsub(%r{/:([^:/]+)}, '/{\1}')
|
|
320
368
|
end
|
|
321
369
|
|
|
370
|
+
# sub copies the content type even when it carries no parameters.
|
|
322
371
|
def normalize_content_type(content_type)
|
|
323
|
-
content_type&.
|
|
372
|
+
return content_type unless content_type&.include?(';')
|
|
373
|
+
|
|
374
|
+
content_type.sub(/;.+\z/, '')
|
|
324
375
|
end
|
|
325
376
|
|
|
326
377
|
# Same logic as normalize_content_type – strips header parameters after ';'
|
|
@@ -332,8 +383,10 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
332
383
|
return VariationMerger.merge_non_object_item_variations(schemas) unless array.all?(Hash)
|
|
333
384
|
|
|
334
385
|
merged = schemas.first.dup
|
|
335
|
-
|
|
336
|
-
|
|
386
|
+
if schemas.any? { |s| s.key?(:properties) }
|
|
387
|
+
merged[:properties] = VariationMerger.merge_property_variations(schemas, allow_recursive_merge: false)
|
|
388
|
+
merged[:required] = schemas.map { |s| s[:required] || [] }.reduce(:&) || []
|
|
389
|
+
end
|
|
337
390
|
merged
|
|
338
391
|
end
|
|
339
392
|
end
|
|
@@ -16,6 +16,12 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
16
16
|
cleanup_one_of_properties!(base, spec, 'components.schemas.*')
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
# The request and response bodies, where most of the cleanup happens.
|
|
20
|
+
MEDIA_TYPE_SELECTORS = [
|
|
21
|
+
'paths.*.*.requestBody.content.application/json',
|
|
22
|
+
'paths.*.*.responses.*.content.application/json',
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
19
25
|
# Cleanup specific elements that exists in the base but not in the spec
|
|
20
26
|
#
|
|
21
27
|
# @param [Hash] base
|
|
@@ -30,22 +36,16 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
30
36
|
# cleanup parameters
|
|
31
37
|
cleanup_array!(base, spec, 'paths.*.*.parameters', [:name, :in])
|
|
32
38
|
|
|
33
|
-
# cleanup
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.schema.properties.*')
|
|
40
|
-
cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.example.*')
|
|
41
|
-
cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.examples.*')
|
|
42
|
-
|
|
43
|
-
# cleanup properties of each variant once a body became a oneOf
|
|
44
|
-
cleanup_one_of_properties!(base, spec, 'paths.*.*.requestBody.content.application/json.schema')
|
|
45
|
-
cleanup_one_of_properties!(base, spec, 'paths.*.*.responses.*.content.application/json.schema')
|
|
39
|
+
# cleanup request and response bodies, each walked down to once
|
|
40
|
+
MEDIA_TYPE_SELECTORS.each do |selector|
|
|
41
|
+
RSpec::OpenAPI::HashHelper.each_matched(base, spec, selector) do |_parent, _key, media_type, fresh|
|
|
42
|
+
cleanup_media_type!(media_type, fresh)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
46
45
|
|
|
47
|
-
#
|
|
48
|
-
|
|
46
|
+
# A full run decides readOnly anew, so a property no request sends any
|
|
47
|
+
# more stops being held at false.
|
|
48
|
+
each_component_schema(base) { |schema| delete_false_read_only!(schema) }
|
|
49
49
|
|
|
50
50
|
base
|
|
51
51
|
end
|
|
@@ -62,19 +62,74 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
# One walk over the schemas once the components are updated, which also drops
|
|
66
|
+
# the fields the builder left for the merge. `readOnly: false` is how a
|
|
67
|
+
# request says it sends a candidate property. A component shared by a request
|
|
68
|
+
# and a response keeps it, or a later partial run that sees only the response
|
|
69
|
+
# would flip it back to true. Inline under paths each side has its own
|
|
70
|
+
# schema, so there it says nothing.
|
|
71
|
+
def cleanup_schemas!(base)
|
|
72
|
+
each_component_schema(base) do |schema, under|
|
|
73
|
+
delete_temporary_fields!(schema)
|
|
74
|
+
delete_empty_required!(schema)
|
|
75
|
+
add_null_to_enum!(schema) unless under == :oneOf
|
|
76
|
+
end
|
|
77
|
+
RSpec::OpenAPI::HashHelper.each_schema(base[:paths]) do |schema, under|
|
|
78
|
+
delete_temporary_fields!(schema)
|
|
79
|
+
delete_empty_required!(schema)
|
|
80
|
+
delete_false_read_only!(schema)
|
|
81
|
+
add_null_to_enum!(schema) unless under == :oneOf
|
|
73
82
|
end
|
|
74
83
|
end
|
|
75
84
|
|
|
76
85
|
private
|
|
77
86
|
|
|
87
|
+
def each_component_schema(base, &block)
|
|
88
|
+
schemas = base.dig(:components, :schemas)
|
|
89
|
+
schemas.each_value { |schema| RSpec::OpenAPI::HashHelper.each_schema(schema, &block) } if schemas.is_a?(Hash)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The description of a response whose example is not recorded is provisional,
|
|
93
|
+
# so it stays only where no documented test set one.
|
|
94
|
+
def delete_temporary_fields!(schema)
|
|
95
|
+
schema.delete(:_example_key)
|
|
96
|
+
schema.delete(:_example_summary)
|
|
97
|
+
schema.delete(:_example_name)
|
|
98
|
+
return unless (fallback = schema.delete(:_fallback_description))
|
|
99
|
+
|
|
100
|
+
schema[:description] ||= fallback
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# A recorded example is data, so a field in it named `required` stays however
|
|
104
|
+
# it is shaped. An empty `required` must not be present.
|
|
105
|
+
def delete_empty_required!(schema)
|
|
106
|
+
schema.delete(:required) if schema.key?(:properties) && schema[:required] && schema[:required].empty?
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def delete_false_read_only!(schema)
|
|
110
|
+
schema.delete(:readOnly) if schema[:readOnly] == false
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# nullable does not loosen an enum, so the null it allows must be listed too.
|
|
114
|
+
# The merge takes the enum from the record, so this runs on every write. A
|
|
115
|
+
# variant of a oneOf is left alone, since a sibling may be the one for null.
|
|
116
|
+
def add_null_to_enum!(schema)
|
|
117
|
+
return unless schema[:nullable] == true && schema[:enum].is_a?(Array) && !schema[:enum].include?(nil)
|
|
118
|
+
|
|
119
|
+
schema[:enum] = [*schema[:enum], nil]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# The selectors are relative to the media type. A media type the spec does
|
|
123
|
+
# not hold has no fresh counterpart, and all of its entries are stale.
|
|
124
|
+
def cleanup_media_type!(media_type, fresh)
|
|
125
|
+
cleanup_hash!(media_type, fresh, 'schema.properties.*')
|
|
126
|
+
cleanup_hash!(media_type, fresh, 'example.*')
|
|
127
|
+
cleanup_hash!(media_type, fresh, 'examples.*')
|
|
128
|
+
|
|
129
|
+
# cleanup properties of each variant once a body became a oneOf
|
|
130
|
+
cleanup_one_of_properties!(media_type, fresh, 'schema')
|
|
131
|
+
end
|
|
132
|
+
|
|
78
133
|
# Security sits on operations, the hashes under a path item: a non-standard
|
|
79
134
|
# method is still inline here and only moves under additionalOperations later.
|
|
80
135
|
def each_operation(base)
|
|
@@ -85,26 +140,6 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
85
140
|
end
|
|
86
141
|
end
|
|
87
142
|
|
|
88
|
-
# Recursively remove temporary fields like :_example_key and :_example_name from the schema.
|
|
89
|
-
# Every caller has already established that it holds a Hash.
|
|
90
|
-
def cleanup_temporary_fields!(hash)
|
|
91
|
-
hash.delete(:_example_key)
|
|
92
|
-
hash.delete(:_example_summary)
|
|
93
|
-
hash.delete(:_example_name)
|
|
94
|
-
if (fallback = hash.delete(:_fallback_description))
|
|
95
|
-
hash[:description] ||= fallback
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
hash.each_value do |value|
|
|
99
|
-
case value
|
|
100
|
-
when Hash
|
|
101
|
-
cleanup_temporary_fields!(value)
|
|
102
|
-
when Array
|
|
103
|
-
value.each { |item| cleanup_temporary_fields!(item) if item.is_a?(Hash) }
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
143
|
def remove_parameters_conflicting_with_security_scheme!(path_definition, security_scheme, security_scheme_name)
|
|
109
144
|
security = path_definition[:security]
|
|
110
145
|
return if !security || security.empty?
|
|
@@ -121,9 +156,7 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
121
156
|
def cleanup_array!(base, spec, selector, fields_for_identity)
|
|
122
157
|
identity = ->(obj) { fields_for_identity.map { |field| obj[field] } }
|
|
123
158
|
|
|
124
|
-
RSpec::OpenAPI::HashHelper.
|
|
125
|
-
target_array = base.dig(*paths)
|
|
126
|
-
spec_array = spec.dig(*paths)
|
|
159
|
+
RSpec::OpenAPI::HashHelper.each_matched(base, spec, selector) do |_parent, _key, target_array, spec_array|
|
|
127
160
|
next unless target_array.is_a?(Array) && spec_array.is_a?(Array)
|
|
128
161
|
|
|
129
162
|
spec_identities = Set.new(spec_array.map(&identity))
|
|
@@ -137,10 +170,11 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
137
170
|
end
|
|
138
171
|
|
|
139
172
|
def cleanup_one_of_properties!(base, spec, selector)
|
|
140
|
-
RSpec::OpenAPI::HashHelper.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
173
|
+
RSpec::OpenAPI::HashHelper.each_matched(base, spec, selector) do |_parent, _key, schema, fresh|
|
|
174
|
+
next unless schema.is_a?(Hash) && fresh.is_a?(Hash)
|
|
175
|
+
|
|
176
|
+
variants = schema[:oneOf]
|
|
177
|
+
next unless variants.is_a?(Array)
|
|
144
178
|
|
|
145
179
|
variants.each { |variant| cleanup_variant_properties!(variant, variants, fresh) }
|
|
146
180
|
end
|
|
@@ -161,14 +195,13 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
161
195
|
variant[:properties].delete_if { |name, _| counterpart.dig(:properties, name).nil? }
|
|
162
196
|
end
|
|
163
197
|
|
|
164
|
-
#
|
|
165
|
-
# has a parent to delete the entry from.
|
|
198
|
+
# The stale entries are deleted after the walk, which iterates their parents.
|
|
166
199
|
def cleanup_hash!(base, spec, selector)
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
base.dig(*paths[0..-2]).delete(paths.last) if exist_in_base && not_in_spec
|
|
200
|
+
stale = nil
|
|
201
|
+
RSpec::OpenAPI::HashHelper.each_matched(base, spec, selector) do |parent, key, value, counterpart|
|
|
202
|
+
(stale ||= []) << [parent, key] if !value.nil? && counterpart.nil?
|
|
171
203
|
end
|
|
204
|
+
stale&.each { |parent, key| parent.delete(key) }
|
|
172
205
|
base
|
|
173
206
|
end
|
|
174
207
|
end
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
require 'fileutils'
|
|
4
4
|
require 'yaml'
|
|
5
5
|
require 'json'
|
|
6
|
+
require 'stringio'
|
|
6
7
|
|
|
7
8
|
# For Ruby 2.7
|
|
8
9
|
require 'date'
|
|
9
10
|
|
|
10
|
-
# TODO: Support JSON
|
|
11
11
|
class RSpec::OpenAPI::SchemaFile
|
|
12
12
|
# @param [String] path
|
|
13
13
|
def initialize(path)
|
|
@@ -16,28 +16,24 @@ class RSpec::OpenAPI::SchemaFile
|
|
|
16
16
|
|
|
17
17
|
# Reads the existing spec, lets the block mutate it, writes the result back
|
|
18
18
|
# and returns the (symbolized) spec so it can be mirrored to other files.
|
|
19
|
+
# A document that does not load is left as it is.
|
|
19
20
|
# @return [Hash]
|
|
20
|
-
def edit
|
|
21
|
+
def edit
|
|
21
22
|
spec = read
|
|
22
|
-
|
|
23
|
+
begin
|
|
24
|
+
yield spec
|
|
25
|
+
ensure
|
|
26
|
+
write(spec)
|
|
27
|
+
end
|
|
23
28
|
spec
|
|
24
|
-
ensure
|
|
25
|
-
write(spec)
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
# Writes an already-built spec to this file, choosing the format from the
|
|
29
|
-
# file extension.
|
|
32
|
+
# file extension. Both formats write the symbol keys as strings themselves.
|
|
30
33
|
# @param [Hash] spec
|
|
31
34
|
def write(spec)
|
|
32
|
-
stringified = RSpec::OpenAPI::KeyTransformer.stringify(spec)
|
|
33
35
|
FileUtils.mkdir_p(File.dirname(@path))
|
|
34
|
-
|
|
35
|
-
if json?
|
|
36
|
-
JSON.pretty_generate(stringified)
|
|
37
|
-
else
|
|
38
|
-
prepend_comment(YAML.dump(stringified))
|
|
39
|
-
end
|
|
40
|
-
File.write(@path, output)
|
|
36
|
+
File.write(@path, json? ? JSON.pretty_generate(spec) : yaml_document(spec))
|
|
41
37
|
end
|
|
42
38
|
|
|
43
39
|
private
|
|
@@ -46,18 +42,29 @@ class RSpec::OpenAPI::SchemaFile
|
|
|
46
42
|
def read
|
|
47
43
|
return {} unless File.exist?(@path)
|
|
48
44
|
|
|
49
|
-
content =
|
|
50
|
-
|
|
45
|
+
content = File.read(@path)
|
|
46
|
+
spec = json? ? read_json(content) : RSpec::OpenAPI::YAMLLoader.load(content)
|
|
47
|
+
spec.nil? ? {} : spec
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def read_json(content)
|
|
51
|
+
return nil if content.match?(/\A\s*\z/)
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
spec = JSON.parse(content, symbolize_names: true, max_nesting: false)
|
|
54
|
+
RSpec::OpenAPI::KeyTransformer.normalize_example_names!(spec)
|
|
53
55
|
end
|
|
54
56
|
|
|
55
|
-
def
|
|
56
|
-
|
|
57
|
+
def yaml_document(spec)
|
|
58
|
+
io = StringIO.new(+'')
|
|
59
|
+
io.write(comment_lines) unless RSpec::OpenAPI.comment.nil?
|
|
60
|
+
RSpec::OpenAPI::YAMLDumper.dump(spec, io)
|
|
61
|
+
io.string
|
|
62
|
+
end
|
|
57
63
|
|
|
64
|
+
def comment_lines
|
|
58
65
|
comment = RSpec::OpenAPI.comment.dup
|
|
59
66
|
comment << "\n" unless comment.end_with?("\n")
|
|
60
|
-
|
|
67
|
+
comment.gsub(/^/, '# ').gsub(/^# \n/, "#\n")
|
|
61
68
|
end
|
|
62
69
|
|
|
63
70
|
def json?
|