rspec-openapi 0.36.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 +30 -3
- data/lib/rspec/openapi/extractors/shared_extractor.rb +16 -5
- data/lib/rspec/openapi/hash_helper.rb +20 -10
- data/lib/rspec/openapi/schema_builder/build_context.rb +2 -1
- data/lib/rspec/openapi/schema_builder/variation_merger.rb +48 -25
- data/lib/rspec/openapi/schema_builder.rb +25 -5
- data/lib/rspec/openapi/schema_cleaner.rb +19 -3
- data/lib/rspec/openapi/schema_merger.rb +2 -5
- data/lib/rspec/openapi/stream_parser.rb +4 -5
- data/lib/rspec/openapi/version.rb +1 -1
- data/lib/rspec/openapi/yaml_dumper.rb +2 -2
- data/lib/rspec/openapi.rb +5 -6
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 147cf0a6b1b7bdb8131d9fd7ef937175d12c987b8556ac7f15da40eed0306b11
|
|
4
|
+
data.tar.gz: 36eea5596e9be414348eab608b0d6aaa4a888191585ec3fe67787d74122fc18f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d62deb3d4d3ea2503dafd4ecb7fcf936894b710334b98fbe35131569856f992fe20e8c59259921b5c91eb711644713e4d4899205c87c50992b38006723bed67c
|
|
7
|
+
data.tar.gz: 257d8b54b1c1bab4753e105b0f10293ae5ad7525b89e2c5259065f32d8b6ac7afbabf30d0661e37299fb09b16941ce2f5d0892f3e9b8af88cbacb3c20de3de8f
|
data/README.md
CHANGED
|
@@ -655,6 +655,29 @@ it 'returns items with status', openapi: {
|
|
|
655
655
|
end
|
|
656
656
|
```
|
|
657
657
|
|
|
658
|
+
For an array of strings, use the array's own name. The enum is applied to its items, so this also works for a
|
|
659
|
+
repeated query parameter such as `sort[]`, which Rack parses into a `sort` array:
|
|
660
|
+
|
|
661
|
+
```rb
|
|
662
|
+
it 'sorts tasks', openapi: {
|
|
663
|
+
request_enum: {
|
|
664
|
+
'sort' => %w[name -name created_at -created_at],
|
|
665
|
+
},
|
|
666
|
+
} do
|
|
667
|
+
get '/tasks', params: { sort: ['-created_at'] }
|
|
668
|
+
# Parameter schema: { type: array, items: { type: string, enum: [name, -name, created_at, -created_at] } }
|
|
669
|
+
expect(response.status).to eq(200)
|
|
670
|
+
end
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
The parameter is recorded as `sort`, without the brackets. A client generated from the document sends it the OpenAPI
|
|
674
|
+
default way, `sort=name&sort=-name`, and Rack reads that as the last value alone. Make the client send
|
|
675
|
+
`sort[]=name&sort[]=-name`.
|
|
676
|
+
|
|
677
|
+
rspec-openapi 0.36.0 and earlier also put the enum on the array itself. Merging keeps keys the new schema doesn't set, so
|
|
678
|
+
a schema file generated by one of those versions keeps that array-level `enum`. Delete it once, or regenerate the file
|
|
679
|
+
from scratch.
|
|
680
|
+
|
|
658
681
|
#### Request vs Response Enums
|
|
659
682
|
|
|
660
683
|
By default, `enum` applies to both request and response bodies. If you need different enum values for request and
|
|
@@ -674,6 +697,9 @@ it 'creates a task', openapi: {
|
|
|
674
697
|
end
|
|
675
698
|
```
|
|
676
699
|
|
|
700
|
+
When a value with an enum is also recorded as `null`, the enum gets `null` too. A null type or `nullable: true`
|
|
701
|
+
does not allow a value the enum leaves out.
|
|
702
|
+
|
|
677
703
|
### Dynamic-key Object Support (`additionalProperties`)
|
|
678
704
|
|
|
679
705
|
When an endpoint returns (or accepts) an object whose keys are not part of its
|
|
@@ -1115,9 +1141,10 @@ Existing RSpec plugins which have OpenAPI integration:
|
|
|
1115
1141
|
2. In GitHub Actions, run the `prepare release` workflow manually. It bumps `lib/rspec/openapi/version.rb`, pushes
|
|
1116
1142
|
`release/v<version>` to origin, and opens a PR.
|
|
1117
1143
|
3. Review and merge the release PR into the default branch.
|
|
1118
|
-
4.
|
|
1119
|
-
|
|
1120
|
-
|
|
1144
|
+
4. In the GitHub UI, create a release with a new tag `v<version>` on the merged commit. Use "Generate release notes"
|
|
1145
|
+
for the body. Publishing the release triggers the `Publish to RubyGems` workflow, which publishes the gem.
|
|
1146
|
+
5. If the workflow fails after the gem is pushed, do not delete and recreate the release or the tag. RubyGems rejects
|
|
1147
|
+
a second push of the same version.
|
|
1121
1148
|
|
|
1122
1149
|
## License
|
|
1123
1150
|
|
|
@@ -24,7 +24,7 @@ class SharedExtractor
|
|
|
24
24
|
response_hybrid_additional_properties, request_hybrid_additional_properties =
|
|
25
25
|
resolve_hybrid_additional_properties(metadata)
|
|
26
26
|
# Enum support: response_enum and request_enum can override the general enum
|
|
27
|
-
base_enum = normalize_enum(metadata
|
|
27
|
+
base_enum = normalize_enum(metadata, :enum, example)
|
|
28
28
|
|
|
29
29
|
{
|
|
30
30
|
summary: metadata[:summary] || RSpec::OpenAPI.summary_builder.call(example),
|
|
@@ -39,8 +39,8 @@ class SharedExtractor
|
|
|
39
39
|
**example_modes(metadata, example),
|
|
40
40
|
example_key: resolve_example_key(metadata, example),
|
|
41
41
|
example_name: metadata[:example_name] || RSpec::OpenAPI.example_name_builder.call(example),
|
|
42
|
-
response_enum: normalize_enum(metadata
|
|
43
|
-
request_enum: normalize_enum(metadata
|
|
42
|
+
response_enum: normalize_enum(metadata, :response_enum, example) || base_enum,
|
|
43
|
+
request_enum: normalize_enum(metadata, :request_enum, example) || base_enum,
|
|
44
44
|
response_additional_properties: response_additional_properties,
|
|
45
45
|
request_additional_properties: request_additional_properties,
|
|
46
46
|
response_hybrid_additional_properties: response_hybrid_additional_properties,
|
|
@@ -104,13 +104,24 @@ class SharedExtractor
|
|
|
104
104
|
->(key) { patterns.any? { |pattern| pattern.is_a?(Regexp) ? pattern.match?(key) : pattern == key } }
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
-
def self.normalize_enum(
|
|
108
|
-
|
|
107
|
+
def self.normalize_enum(metadata, name, example)
|
|
108
|
+
enum_hash = metadata[name]
|
|
109
|
+
return nil if enum_hash.nil? || enum_hash == {}
|
|
110
|
+
unless enum_hash.is_a?(Hash) && enum_hash.each_value.all? { |values| scalars?(values) }
|
|
111
|
+
raise ArgumentError, "#{name} must be a Hash of property paths to Arrays of scalar values, " \
|
|
112
|
+
"got #{enum_hash.inspect}#{example_context(example)}"
|
|
113
|
+
end
|
|
109
114
|
|
|
110
115
|
# Convert all keys to strings for consistent lookup
|
|
111
116
|
enum_hash.transform_keys(&:to_s)
|
|
112
117
|
end
|
|
113
118
|
|
|
119
|
+
# Pairs such as `Order.statuses.to_a` or `pluck(:name, :id)` are a mistake for
|
|
120
|
+
# the values alone, and would allow nothing.
|
|
121
|
+
def self.scalars?(values)
|
|
122
|
+
values.is_a?(Array) && values.none? { |value| value.is_a?(Array) || value.is_a?(Hash) }
|
|
123
|
+
end
|
|
124
|
+
|
|
114
125
|
def self.normalize_additional_properties(hash)
|
|
115
126
|
return nil if hash.nil? || hash.empty?
|
|
116
127
|
|
|
@@ -43,22 +43,27 @@ class << RSpec::OpenAPI::HashHelper = Object.new
|
|
|
43
43
|
# named `type` or `nullable` would be silently mangled.
|
|
44
44
|
DATA_KEYS = [:example, :examples, :default, :enum, :const].freeze
|
|
45
45
|
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
# Keys of maps whose entries the document names, so an entry named like
|
|
47
|
+
# `default`, `nullable` or `x-mode` is never taken for a keyword.
|
|
48
|
+
NAMED_MAP_KEYS = [:properties, :schemas, :headers].freeze
|
|
49
|
+
|
|
50
|
+
# Yields every hash in obj outside the data subtrees in DATA_KEYS and the
|
|
51
|
+
# `x-` extensions, each after its descendants, with the key it sits under,
|
|
52
|
+
# directly or as a list element. The entries of a map in NAMED_MAP_KEYS are
|
|
53
|
+
# yielded whatever their names, and the map itself is not.
|
|
54
|
+
def each_schema(obj, under: nil, &block)
|
|
50
55
|
case obj
|
|
51
56
|
when Hash
|
|
52
57
|
obj.each do |key, value|
|
|
53
|
-
if key
|
|
54
|
-
value.each_value { |
|
|
55
|
-
elsif
|
|
56
|
-
each_schema(value, &block)
|
|
58
|
+
if NAMED_MAP_KEYS.include?(key) && value.is_a?(Hash)
|
|
59
|
+
value.each_value { |entry| each_schema(entry, &block) }
|
|
60
|
+
elsif walk?(key, under)
|
|
61
|
+
each_schema(value, under: key, &block)
|
|
57
62
|
end
|
|
58
63
|
end
|
|
59
|
-
yield obj
|
|
64
|
+
yield obj, under
|
|
60
65
|
when Array
|
|
61
|
-
obj.each { |value| each_schema(value, &block) }
|
|
66
|
+
obj.each { |value| each_schema(value, under: under, &block) }
|
|
62
67
|
end
|
|
63
68
|
end
|
|
64
69
|
|
|
@@ -75,6 +80,11 @@ class << RSpec::OpenAPI::HashHelper = Object.new
|
|
|
75
80
|
|
|
76
81
|
private
|
|
77
82
|
|
|
83
|
+
# A response may be named `default`, and the responses may carry extensions.
|
|
84
|
+
def walk?(key, under)
|
|
85
|
+
!key.start_with?('x-') && (under == :responses || !DATA_KEYS.include?(key))
|
|
86
|
+
end
|
|
87
|
+
|
|
78
88
|
def split_selector(selector)
|
|
79
89
|
SELECTOR_PARTS[selector]
|
|
80
90
|
end
|
|
@@ -26,8 +26,9 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
26
26
|
path ? "#{path}.#{child_key}" : child_key.to_s
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# Items take the format and enum of their array, so they share its path and key.
|
|
29
30
|
def for_array_items
|
|
30
|
-
self.class.new(record, context, path,
|
|
31
|
+
self.class.new(record, context, path, key, lookups)
|
|
31
32
|
end
|
|
32
33
|
end
|
|
33
34
|
private_constant :BuildContext
|
|
@@ -12,33 +12,27 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
12
12
|
# Drop empty `{}` schemas (e.g. items of an empty array) — they carry no
|
|
13
13
|
# type info and would otherwise spuriously mark every property of their
|
|
14
14
|
# populated siblings as nullable via the missing-key nullable rule.
|
|
15
|
-
non_empty =
|
|
15
|
+
non_empty = variations.reject(&:empty?)
|
|
16
16
|
return {} if non_empty.empty?
|
|
17
|
-
return non_empty.first if non_empty.size == 1
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
return
|
|
18
|
+
typed = non_empty.reject { |v| nullable_only?(v) }
|
|
19
|
+
return { nullable: true } if typed.empty?
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
when 'object' then merge_object_variations(non_empty)
|
|
24
|
-
when 'array' then merge_array_item_variations(non_empty)
|
|
25
|
-
else non_empty.first
|
|
26
|
-
end
|
|
21
|
+
with_nullable_from(non_empty, merge_typed_variations(widen_integers(typed)))
|
|
27
22
|
end
|
|
28
23
|
|
|
29
24
|
def merge_non_object_item_variations(schemas)
|
|
30
25
|
typed = schemas.reject { |schema| nullable_only?(schema) }
|
|
26
|
+
return { nullable: true } if typed.empty?
|
|
31
27
|
|
|
32
28
|
merged = typed.size == 1 ? typed.first.dup : merge_multi(typed)
|
|
33
|
-
merged[:nullable] = true if
|
|
29
|
+
merged[:nullable] = true if typed.size < schemas.size
|
|
34
30
|
merged
|
|
35
31
|
end
|
|
36
32
|
|
|
37
33
|
# Merge the per-key property schemas of multiple object variations.
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
# When false (callsite: array-items merging), divergent property variations
|
|
41
|
-
# become oneOf without recursive descent.
|
|
34
|
+
# `allow_recursive_merge` only decides which signals make a property
|
|
35
|
+
# nullable. Both modes merge the property schemas themselves alike.
|
|
42
36
|
def merge_property_variations(variations, allow_recursive_merge:)
|
|
43
37
|
property_keys(variations).each_with_object({}) do |key, merged_props|
|
|
44
38
|
all = variations.map { |v| v[:properties]&.[](key) }
|
|
@@ -58,6 +52,24 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
58
52
|
|
|
59
53
|
private
|
|
60
54
|
|
|
55
|
+
def merge_typed_variations(variations)
|
|
56
|
+
return variations.first if variations.size == 1
|
|
57
|
+
return merge_one_of_variations(variations) if variations.any? { |v| v.key?(:oneOf) }
|
|
58
|
+
|
|
59
|
+
types = variations.map { |v| v[:type] }.compact.uniq
|
|
60
|
+
return merge_one_of_variations(variations) if types.size > 1
|
|
61
|
+
|
|
62
|
+
case types.first
|
|
63
|
+
when 'object' then merge_object_variations(variations)
|
|
64
|
+
when 'array' then merge_array_item_variations(variations)
|
|
65
|
+
else variations.first
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def with_nullable_from(variations, merged)
|
|
70
|
+
variations.any? { |v| v[:nullable] } ? merged.merge(nullable: true) : merged
|
|
71
|
+
end
|
|
72
|
+
|
|
61
73
|
# The variations of one property come from one side of one exchange, so
|
|
62
74
|
# they agree on readOnly. The merge rebuilds the schema, so the caller puts
|
|
63
75
|
# it back on the result, beside a oneOf like nullable. The schemas are the
|
|
@@ -80,9 +92,15 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
80
92
|
merged
|
|
81
93
|
end
|
|
82
94
|
|
|
83
|
-
# Merge multiple array schemas by merging their `items` schemas.
|
|
95
|
+
# Merge multiple array schemas by merging their `items` schemas. A constraint
|
|
96
|
+
# seen in a single sample must not spread to its siblings.
|
|
84
97
|
def merge_array_item_variations(variations)
|
|
85
|
-
|
|
98
|
+
merged = variations.first.select do |key, value|
|
|
99
|
+
key == :type || key == :items ||
|
|
100
|
+
variations.all? { |variation| variation.key?(key) && variation[key] == value }
|
|
101
|
+
end
|
|
102
|
+
merged[:items] = merge_variations(variations.map { |v| v[:items] }.compact)
|
|
103
|
+
merged
|
|
86
104
|
end
|
|
87
105
|
|
|
88
106
|
def property_keys(variations)
|
|
@@ -101,8 +119,9 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
101
119
|
end
|
|
102
120
|
end
|
|
103
121
|
|
|
122
|
+
# A nil builds `{ nullable: true }`.
|
|
104
123
|
def nullable_only?(schema)
|
|
105
|
-
schema
|
|
124
|
+
schema[:nullable] == true && !schema.key?(:type) && !schema.key?(:oneOf)
|
|
106
125
|
end
|
|
107
126
|
|
|
108
127
|
def merge_single_property(prop_variations, has_nullable, variations_total:, allow_recursive_merge:)
|
|
@@ -115,8 +134,6 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
115
134
|
merge_multi(prop_variations)
|
|
116
135
|
end
|
|
117
136
|
|
|
118
|
-
return merged unless merged.is_a?(Hash)
|
|
119
|
-
|
|
120
137
|
# In recursive mode, multi-variation merges also flag nullable when the key
|
|
121
138
|
# only appeared in some of the source variations.
|
|
122
139
|
needs_nullable =
|
|
@@ -133,11 +150,11 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
133
150
|
# entries, recurse into objects and arrays, and combine divergent types into
|
|
134
151
|
# oneOf. Scalar variations of a single type collapse to the first schema.
|
|
135
152
|
def merge_multi(prop_variations)
|
|
136
|
-
return
|
|
153
|
+
return merge_one_of_variations(prop_variations) if prop_variations.any? { |p| p.key?(:oneOf) }
|
|
137
154
|
|
|
138
155
|
prop_variations = widen_integers(prop_variations)
|
|
139
156
|
prop_types = prop_variations.map { |p| p[:type] }.compact.uniq
|
|
140
|
-
return
|
|
157
|
+
return merge_one_of_variations(prop_variations) if prop_types.size > 1
|
|
141
158
|
|
|
142
159
|
case prop_types.first
|
|
143
160
|
when 'array' then merge_array_item_variations(prop_variations)
|
|
@@ -146,6 +163,16 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
146
163
|
end
|
|
147
164
|
end
|
|
148
165
|
|
|
166
|
+
def merge_one_of_variations(variations)
|
|
167
|
+
{ oneOf: merge_structured_alternatives(widen_integers(flatten_one_of(variations))) }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def merge_structured_alternatives(alternatives)
|
|
171
|
+
alternatives.group_by { |alternative| alternative[:type] }.flat_map do |type, group|
|
|
172
|
+
['array', 'object'].include?(type) && group.size > 1 ? [merge_variations(group)] : group
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
149
176
|
# Every integer is a number, so integers seen beside numbers are numbers.
|
|
150
177
|
def widen_integers(variations)
|
|
151
178
|
return variations unless variations.any? { |v| v[:type] == 'number' }
|
|
@@ -169,10 +196,6 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
169
196
|
prop.reject { |k, _| k == :nullable }
|
|
170
197
|
end
|
|
171
198
|
|
|
172
|
-
def one_of_schema(variations)
|
|
173
|
-
{ oneOf: sort_by_type(variations.map { |p| without_nullable(p) }.uniq) }
|
|
174
|
-
end
|
|
175
|
-
|
|
176
199
|
# The same order the merger keeps, so the document does not depend on which
|
|
177
200
|
# example ran first.
|
|
178
201
|
def sort_by_type(options)
|
|
@@ -212,9 +212,7 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
212
212
|
end
|
|
213
213
|
|
|
214
214
|
def build_property(value, ctx, disposition: nil)
|
|
215
|
-
|
|
216
|
-
enum = infer_enum(ctx.path, ctx.record, ctx.context)
|
|
217
|
-
property = build_type(value, format: format, enum: enum)
|
|
215
|
+
property = build_type(value, format: property_format(value, ctx, disposition), enum: property_enum(value, ctx))
|
|
218
216
|
|
|
219
217
|
case value
|
|
220
218
|
when Array
|
|
@@ -225,6 +223,26 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
225
223
|
property
|
|
226
224
|
end
|
|
227
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
|
+
|
|
228
246
|
def apply_object_schema(property, value, ctx)
|
|
229
247
|
override = infer_override(ctx.path, ctx.record, ctx.context, :additional_properties)
|
|
230
248
|
|
|
@@ -365,8 +383,10 @@ class << RSpec::OpenAPI::SchemaBuilder
|
|
|
365
383
|
return VariationMerger.merge_non_object_item_variations(schemas) unless array.all?(Hash)
|
|
366
384
|
|
|
367
385
|
merged = schemas.first.dup
|
|
368
|
-
|
|
369
|
-
|
|
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
|
|
370
390
|
merged
|
|
371
391
|
end
|
|
372
392
|
end
|
|
@@ -45,7 +45,7 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
45
45
|
|
|
46
46
|
# A full run decides readOnly anew, so a property no request sends any
|
|
47
47
|
# more stops being held at false.
|
|
48
|
-
|
|
48
|
+
each_component_schema(base) { |schema| delete_false_read_only!(schema) }
|
|
49
49
|
|
|
50
50
|
base
|
|
51
51
|
end
|
|
@@ -69,19 +69,26 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
69
69
|
# would flip it back to true. Inline under paths each side has its own
|
|
70
70
|
# schema, so there it says nothing.
|
|
71
71
|
def cleanup_schemas!(base)
|
|
72
|
-
|
|
72
|
+
each_component_schema(base) do |schema, under|
|
|
73
73
|
delete_temporary_fields!(schema)
|
|
74
74
|
delete_empty_required!(schema)
|
|
75
|
+
add_null_to_enum!(schema) unless under == :oneOf
|
|
75
76
|
end
|
|
76
|
-
RSpec::OpenAPI::HashHelper.each_schema(base[:paths]) do |schema|
|
|
77
|
+
RSpec::OpenAPI::HashHelper.each_schema(base[:paths]) do |schema, under|
|
|
77
78
|
delete_temporary_fields!(schema)
|
|
78
79
|
delete_empty_required!(schema)
|
|
79
80
|
delete_false_read_only!(schema)
|
|
81
|
+
add_null_to_enum!(schema) unless under == :oneOf
|
|
80
82
|
end
|
|
81
83
|
end
|
|
82
84
|
|
|
83
85
|
private
|
|
84
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
|
+
|
|
85
92
|
# The description of a response whose example is not recorded is provisional,
|
|
86
93
|
# so it stays only where no documented test set one.
|
|
87
94
|
def delete_temporary_fields!(schema)
|
|
@@ -103,6 +110,15 @@ class << RSpec::OpenAPI::SchemaCleaner = Object.new
|
|
|
103
110
|
schema.delete(:readOnly) if schema[:readOnly] == false
|
|
104
111
|
end
|
|
105
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
|
+
|
|
106
122
|
# The selectors are relative to the media type. A media type the spec does
|
|
107
123
|
# not hold has no fresh counterpart, and all of its entries are stale.
|
|
108
124
|
def cleanup_media_type!(media_type, fresh)
|
|
@@ -127,19 +127,16 @@ class << RSpec::OpenAPI::SchemaMerger = Object.new
|
|
|
127
127
|
def merge_hash_entry!(base, key, value, in_schema)
|
|
128
128
|
# Handle example/examples conflict - convert to examples when mixed
|
|
129
129
|
normalize_example_fields!(base[key], value)
|
|
130
|
+
return if base[key].key?(:$ref)
|
|
130
131
|
|
|
131
132
|
if in_schema && one_of_needed?(base[key], value)
|
|
132
133
|
merge_one_of!(base[key], value)
|
|
133
|
-
|
|
134
|
-
base[key] = value
|
|
135
|
-
elsif !base[key].key?(:$ref)
|
|
134
|
+
else
|
|
136
135
|
merge_schema!(base[key], value, in_schema: in_schema)
|
|
137
136
|
end
|
|
138
137
|
end
|
|
139
138
|
|
|
140
139
|
def one_of_needed?(base, spec)
|
|
141
|
-
return false if base.key?(:$ref)
|
|
142
|
-
|
|
143
140
|
spec.key?(:oneOf) || conflicting_types?(base, spec)
|
|
144
141
|
end
|
|
145
142
|
|
|
@@ -13,16 +13,15 @@ class << RSpec::OpenAPI::StreamParser = Object.new
|
|
|
13
13
|
else raw.split("\n") # NDJSON / JSON Lines
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
chunks.
|
|
16
|
+
chunks.each_with_object([]) { |chunk, items| parse_json(chunk, items) }
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
private
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
# Appends rather than returns, since null and false are items too.
|
|
22
|
+
def parse_json(chunk, items)
|
|
22
23
|
text = chunk.to_s.strip
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
JSON.parse(text)
|
|
24
|
+
items << JSON.parse(text) unless text.empty?
|
|
26
25
|
rescue JSON::ParserError
|
|
27
26
|
nil
|
|
28
27
|
end
|
|
@@ -93,11 +93,11 @@ class RSpec::OpenAPI::YAMLDumper < Psych::Visitors::YAMLTree
|
|
|
93
93
|
@emitter = emitter
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
# :
|
|
96
|
+
# simplecov:disable
|
|
97
97
|
# Reached by values other than hashes, arrays and plain scalars, such as the
|
|
98
98
|
# dates of a hand-written document, which the suite does not hold.
|
|
99
99
|
def register(_target, yaml_obj)
|
|
100
100
|
yaml_obj
|
|
101
101
|
end
|
|
102
|
-
# :
|
|
102
|
+
# simplecov:enable
|
|
103
103
|
end
|
data/lib/rspec/openapi.rb
CHANGED
|
@@ -145,13 +145,12 @@ module RSpec::OpenAPI
|
|
|
145
145
|
# Allow Rails request specs to issue extra verbs (e.g. QUERY); ActionDispatch
|
|
146
146
|
# otherwise rejects unknown verbs. No-op outside Rails.
|
|
147
147
|
def register_http_methods(methods)
|
|
148
|
-
# :
|
|
148
|
+
# simplecov:disable
|
|
149
149
|
# Guards users without Rails. Every job here installs it, so this arm is
|
|
150
|
-
# unreachable from the suite.
|
|
151
|
-
# this used to carry never actually excluded anything.
|
|
150
|
+
# unreachable from the suite.
|
|
152
151
|
return unless defined?(ActionDispatch::Request::HTTP_METHODS)
|
|
153
152
|
|
|
154
|
-
# :
|
|
153
|
+
# simplecov:enable
|
|
155
154
|
Array(methods).each do |method|
|
|
156
155
|
verb = method.to_s.upcase
|
|
157
156
|
next if ActionDispatch::Request::HTTP_METHODS.include?(verb)
|
|
@@ -166,7 +165,7 @@ end
|
|
|
166
165
|
if ENV['OPENAPI']
|
|
167
166
|
RSpec::OpenAPI::Config.load_environment_settings
|
|
168
167
|
|
|
169
|
-
# :
|
|
168
|
+
# simplecov:disable
|
|
170
169
|
# These two rescues report a framework the user does not have. Every job here
|
|
171
170
|
# installs both Hanami and Rails, so no run can reach them.
|
|
172
171
|
begin
|
|
@@ -184,7 +183,7 @@ if ENV['OPENAPI']
|
|
|
184
183
|
else
|
|
185
184
|
require 'rspec/openapi/extractors/rails'
|
|
186
185
|
end
|
|
187
|
-
# :
|
|
186
|
+
# simplecov:enable
|
|
188
187
|
end
|
|
189
188
|
|
|
190
189
|
require 'rspec/openapi/minitest_hooks' if Object.const_defined?('Minitest')
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rspec-openapi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.37.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Takashi Kokubun
|
|
@@ -102,7 +102,7 @@ licenses:
|
|
|
102
102
|
metadata:
|
|
103
103
|
homepage_uri: https://github.com/exoego/rspec-openapi
|
|
104
104
|
source_code_uri: https://github.com/exoego/rspec-openapi
|
|
105
|
-
changelog_uri: https://github.com/exoego/rspec-openapi/releases/tag/v0.
|
|
105
|
+
changelog_uri: https://github.com/exoego/rspec-openapi/releases/tag/v0.37.0
|
|
106
106
|
rubygems_mfa_required: 'true'
|
|
107
107
|
rdoc_options: []
|
|
108
108
|
require_paths:
|