openapi_parser 2.5.0 → 2.7.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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yaml +1 -3
  3. data/CHANGELOG.md +41 -0
  4. data/README.md +44 -1
  5. data/lib/openapi_parser/concerns/media_type_selectable.rb +18 -0
  6. data/lib/openapi_parser/concerns/schema_loader/creator.rb +7 -2
  7. data/lib/openapi_parser/concerns/schema_loader/hash_body_loader.rb +1 -1
  8. data/lib/openapi_parser/config.rb +5 -0
  9. data/lib/openapi_parser/errors.rb +36 -0
  10. data/lib/openapi_parser/schema_validator/all_of_validator.rb +16 -11
  11. data/lib/openapi_parser/schema_validator/any_of_validator.rb +1 -1
  12. data/lib/openapi_parser/schema_validator/base.rb +35 -9
  13. data/lib/openapi_parser/schema_validator/float_validator.rb +4 -0
  14. data/lib/openapi_parser/schema_validator/integer_validator.rb +17 -1
  15. data/lib/openapi_parser/schema_validator/multiple_of.rb +26 -0
  16. data/lib/openapi_parser/schema_validator/nil_validator.rb +4 -0
  17. data/lib/openapi_parser/schema_validator/object_validator.rb +21 -0
  18. data/lib/openapi_parser/schema_validator/one_of_validator.rb +4 -7
  19. data/lib/openapi_parser/schema_validator.rb +95 -18
  20. data/lib/openapi_parser/schemas/components.rb +4 -0
  21. data/lib/openapi_parser/schemas/discriminator.rb +4 -0
  22. data/lib/openapi_parser/schemas/media_type.rb +4 -0
  23. data/lib/openapi_parser/schemas/openapi.rb +36 -3
  24. data/lib/openapi_parser/schemas/path_item.rb +29 -6
  25. data/lib/openapi_parser/schemas/request_body.rb +1 -1
  26. data/lib/openapi_parser/schemas/response.rb +5 -1
  27. data/lib/openapi_parser/schemas/schema.rb +12 -1
  28. data/lib/openapi_parser/spec_validator/rule.rb +24 -3
  29. data/lib/openapi_parser/spec_validator/rules/additional_operations_before_32.rb +26 -0
  30. data/lib/openapi_parser/spec_validator/rules/allow_empty_value_deprecation.rb +26 -0
  31. data/lib/openapi_parser/spec_validator/rules/cookie_style_before_32.rb +26 -0
  32. data/lib/openapi_parser/spec_validator/rules/default_mapping_before_32.rb +29 -0
  33. data/lib/openapi_parser/spec_validator/rules/defs_in_30.rb +25 -0
  34. data/lib/openapi_parser/spec_validator/rules/dependent_required_in_30.rb +25 -0
  35. data/lib/openapi_parser/spec_validator/rules/example_value_fields_before_32.rb +48 -0
  36. data/lib/openapi_parser/spec_validator/rules/media_types_before_32.rb +24 -0
  37. data/lib/openapi_parser/spec_validator/rules/query_method_before_32.rb +26 -0
  38. data/lib/openapi_parser/spec_validator/rules/querystring_before_32.rb +26 -0
  39. data/lib/openapi_parser/spec_validator/rules/response_summary_before_32.rb +25 -0
  40. data/lib/openapi_parser/spec_validator/rules/security_scheme_fields_before_32.rb +41 -0
  41. data/lib/openapi_parser/spec_validator/rules/server_name_before_32.rb +35 -0
  42. data/lib/openapi_parser/spec_validator/rules/streaming_fields_before_32.rb +56 -0
  43. data/lib/openapi_parser/spec_validator/rules/tag_fields_before_32.rb +34 -0
  44. data/lib/openapi_parser/spec_validator/rules/xml_attribute_deprecation.rb +29 -0
  45. data/lib/openapi_parser/spec_validator/rules/xml_node_type_before_32.rb +29 -0
  46. data/lib/openapi_parser/spec_validator/rules/xml_wrapped_deprecation.rb +29 -0
  47. data/lib/openapi_parser/spec_validator.rb +36 -0
  48. data/lib/openapi_parser/version.rb +1 -1
  49. data/lib/openapi_parser.rb +5 -4
  50. data/sig/openapi_parser/config.rbs +1 -0
  51. data/sig/openapi_parser/schema_validator.rbs +2 -0
  52. data/sig/openapi_parser/schema_validators/base.rbs +4 -2
  53. data/sig/openapi_parser/spec_validator.rbs +81 -0
  54. data/sig/openapi_parser.rbs +5 -3
  55. data/sig/wip_types.rbs +6 -0
  56. metadata +20 -1
@@ -1,6 +1,7 @@
1
1
  require_relative 'schema_validator/options'
2
2
  require_relative 'schema_validator/enumable'
3
3
  require_relative 'schema_validator/minimum_maximum'
4
+ require_relative 'schema_validator/multiple_of'
4
5
  require_relative 'schema_validator/properties_number'
5
6
  require_relative 'schema_validator/base'
6
7
  require_relative 'schema_validator/string_validator'
@@ -37,6 +38,11 @@ class OpenAPIParser::SchemaValidator
37
38
 
38
39
  include Validatable
39
40
 
41
+ # Types to pick from for a schema without `type`. integer comes before
42
+ # number so that an Integer is checked as an integer.
43
+ JSON_VALUE_CLASSES = [Hash, Array, String, Numeric, TrueClass, FalseClass, NilClass].freeze
44
+ VALUE_TYPES = %w[object array string integer number boolean].freeze
45
+
40
46
  class << self
41
47
  # validate schema data
42
48
  # @param [Hash] value
@@ -89,18 +95,15 @@ class OpenAPIParser::SchemaValidator
89
95
  return [nil, OpenAPIParser::ValidateError.new(value, "const #{schema.const.inspect}", schema.object_reference)] if value != schema.const
90
96
  end
91
97
 
92
- return coerce_into_type_array(value, schema, **keyword_args) if coerce_into_type_array?(value, schema)
93
-
94
- if (v = validator(value, schema))
95
- if keyword_args.empty?
96
- return v.coerce_and_validate(value, schema)
97
- else
98
- return v.coerce_and_validate(value, schema, **keyword_args)
99
- end
98
+ # `not` rejects a value that is valid against its schema.
99
+ if schema.respond_to?(:not) && schema.not.is_a?(OpenAPIParser::Schemas::Schema)
100
+ _, not_error = validate_schema(value, schema.not)
101
+ return [nil, OpenAPIParser::NotSchemaMatched.new(value, schema.not.object_reference)] unless not_error
100
102
  end
101
103
 
102
- # unknown return error
103
- OpenAPIParser::ValidateError.build_error_result(value, schema)
104
+ return validate_composition(value, schema, **keyword_args) if composition_validators(schema).any?
105
+
106
+ validate_without_composition(value, schema, **keyword_args)
104
107
  end
105
108
 
106
109
  # validate integer value by schema
@@ -111,15 +114,78 @@ class OpenAPIParser::SchemaValidator
111
114
  integer_validator.coerce_and_validate(value, schema)
112
115
  end
113
116
 
117
+ # Run the steps in order, each with the value coerced by the steps before it.
118
+ # Keywords check JSON values. A coerced value of another class, like DateTime,
119
+ # is only the output form, so the later steps check the original value.
120
+ # @param [Object] value
121
+ # @param [Array<Proc>] steps each takes a value and returns coerced value and error
122
+ # @return [Array] coerced value and error
123
+ def validate_in_order(value, steps)
124
+ current = value
125
+ steps.each do |step|
126
+ returned, err = step.call(json_value?(current) ? current : value)
127
+ return [nil, err] if err
128
+
129
+ current = returned if json_value?(current)
130
+ end
131
+ [current, nil]
132
+ end
133
+
114
134
  private
115
135
 
136
+ def json_value?(value)
137
+ JSON_VALUE_CLASSES.any? { |klass| value.is_a?(klass) }
138
+ end
139
+
140
+ # @return [Array<OpenAPIParser::SchemaValidator::Base>]
141
+ def composition_validators(schema)
142
+ validators = []
143
+ validators << any_of_validator if schema.any_of
144
+ validators << all_of_validator if schema.all_of
145
+ validators << one_of_validator if schema.one_of
146
+ validators
147
+ end
148
+
149
+ # All the keywords in a schema apply to the value, so the composition
150
+ # keywords and the keywords next to them are all checked.
151
+ # `type` goes first, so that the other steps see the coerced value.
152
+ # @return [Array] coerced value and error
153
+ def validate_composition(value, schema, **keyword_args)
154
+ composition_steps = composition_validators(schema).map do |v|
155
+ ->(input) { v.coerce_and_validate(input, schema, **keyword_args) }
156
+ end
157
+ # the composition keyword already used discriminator
158
+ parents = (keyword_args[:parent_discriminator_schemas] || []) + [schema]
159
+ other_step = ->(input) { validate_without_composition(input, schema, **keyword_args, parent_discriminator_schemas: parents) }
160
+
161
+ validate_in_order(value, schema.type ? [other_step, *composition_steps] : [*composition_steps, other_step])
162
+ end
163
+
164
+ # @return [Array] coerced value and error
165
+ def validate_without_composition(value, schema, **keyword_args)
166
+ return coerce_into_type_array(value, schema, **keyword_args) if coerce_into_type_array?(value, schema)
167
+
168
+ if (v = validator(value, schema))
169
+ if keyword_args.empty?
170
+ return v.coerce_and_validate(value, schema)
171
+ else
172
+ return v.coerce_and_validate(value, schema, **keyword_args)
173
+ end
174
+ end
175
+
176
+ # unknown return error
177
+ OpenAPIParser::ValidateError.build_error_result(value, schema)
178
+ end
179
+
116
180
  # @return [OpenAPIParser::SchemaValidator::Base, nil]
117
181
  def validator(value, schema)
118
- return any_of_validator if schema.any_of
119
- return all_of_validator if schema.all_of
120
- return one_of_validator if schema.one_of
121
182
  return nil_validator if value.nil?
122
183
 
184
+ # 3.1: `format: binary` with `string` in an array-form `type` accepts a
185
+ # non-String object such as an uploaded file, the same way as a single
186
+ # `type: string`. The string validator skips the check for `binary`.
187
+ return string_validator if binary_string_in_type_array?(value, schema)
188
+
123
189
  # 3.1: pick the type from an Array whose primitive class matches the
124
190
  # value, then dispatch as if it were a single-type schema. If nothing
125
191
  # matches we still pick a candidate so the relevant validator emits
@@ -127,12 +193,16 @@ class OpenAPIParser::SchemaValidator
127
193
  # fallback.
128
194
  effective_type = schema.type
129
195
  if effective_type.is_a?(Array)
130
- matched = pick_array_type(value, effective_type)
196
+ matched = pick_type(value, effective_type, schema)
131
197
  return type_mismatch_validator if matched.nil?
132
198
 
133
199
  effective_type = matched
134
200
  end
135
201
 
202
+ # JSON Schema applies each keyword when the value has the type the
203
+ # keyword is for, whether or not `type` is set.
204
+ effective_type ||= pick_type(value, VALUE_TYPES, schema)
205
+
136
206
  validator_for_type(effective_type)
137
207
  end
138
208
 
@@ -161,6 +231,14 @@ class OpenAPIParser::SchemaValidator
161
231
  end
162
232
  end
163
233
 
234
+ # 3.1: a non-String value for an array-form `type` that lists `string`
235
+ # with `format: binary`, such as an uploaded file object.
236
+ def binary_string_in_type_array?(value, schema)
237
+ return false if value.is_a?(String)
238
+
239
+ schema.format == 'binary' && schema.type.is_a?(Array) && schema.type.include?('string')
240
+ end
241
+
164
242
  # 3.1: when coercing, a value whose class matches no listed type is tried
165
243
  # against each listed type, the same way a single `type` coerces it.
166
244
  # Request params arrive as Strings ("5" for `type: [integer, "null"]`),
@@ -169,9 +247,8 @@ class OpenAPIParser::SchemaValidator
169
247
  # @return [Boolean]
170
248
  def coerce_into_type_array?(value, schema)
171
249
  return false unless @coerce_value && !value.nil?
172
- return false if schema.any_of || schema.all_of || schema.one_of
173
250
 
174
- schema.type.is_a?(Array) && pick_array_type(value, schema.type).nil?
251
+ schema.type.is_a?(Array) && pick_type(value, schema.type, schema).nil?
175
252
  end
176
253
 
177
254
  # 3.1: returns the first listed type that coerces, or the first type's error.
@@ -198,11 +275,11 @@ class OpenAPIParser::SchemaValidator
198
275
  @type_mismatch_validator ||= OpenAPIParser::SchemaValidator::TypeMismatchValidator.new(self, @coerce_value)
199
276
  end
200
277
 
201
- def pick_array_type(value, types)
278
+ def pick_type(value, types, schema)
202
279
  types.find do |t|
203
280
  case t
204
281
  when 'string' then value.is_a?(String)
205
- when 'integer' then value.is_a?(Integer)
282
+ when 'integer' then integer_validator.integer?(value, schema)
206
283
  when 'number' then value.is_a?(Numeric)
207
284
  when 'boolean' then value == true || value == false
208
285
  when 'array' then value.is_a?(Array)
@@ -28,5 +28,9 @@ module OpenAPIParser::Schemas
28
28
  # @!attribute [r] path_items
29
29
  # @return [Hash{String => PathItem}, nil] path item objects (OpenAPI 3.1+)
30
30
  openapi_attr_hash_object :path_items, PathItem, reference: true, schema_key: :pathItems
31
+
32
+ # @!attribute [r] media_types
33
+ # @return [Hash{String => MediaType}, nil] media type objects (OpenAPI 3.2+)
34
+ openapi_attr_hash_object :media_types, MediaType, reference: true, schema_key: :mediaTypes
31
35
  end
32
36
  end
@@ -7,5 +7,9 @@ module OpenAPIParser::Schemas
7
7
  # @!attribute [r] mapping
8
8
  # @return [Hash{String => String]
9
9
  openapi_attr_value :mapping
10
+
11
+ # @!attribute [r] default_mapping
12
+ # @return [String, nil] fallback schema name or reference (OpenAPI 3.2+)
13
+ openapi_attr_value :default_mapping, schema_key: :defaultMapping
10
14
  end
11
15
  end
@@ -8,6 +8,10 @@ module OpenAPIParser::Schemas
8
8
  # @return [Schema, nil] OpenAPI3 Schema object
9
9
  openapi_attr_object :schema, Schema, reference: true
10
10
 
11
+ # @!attribute [r] item_schema
12
+ # @return [Schema, nil] schema for each item of a sequential media type (OpenAPI 3.2+); not yet used for validation
13
+ openapi_attr_object :item_schema, Schema, reference: true, schema_key: :itemSchema
14
+
11
15
  # validate params by schema definitions
12
16
  # @param [Hash] params
13
17
  # @param [OpenAPIParser::SchemaValidator::Options] options
@@ -5,11 +5,14 @@
5
5
 
6
6
  module OpenAPIParser::Schemas
7
7
  class OpenAPI < Base
8
- def initialize(raw_schema, config, uri: nil, schema_registry: {})
8
+ # @param [OpenAPIParser::Schemas::OpenAPI, nil] referrer the document whose $ref loads this one
9
+ def initialize(raw_schema, config, uri: nil, schema_registry: {}, referrer: nil)
10
+ # set before super: child objects consult use_3_2_features? while they're built
11
+ @config = config
12
+ @referrer = referrer
9
13
  super('#', nil, self, raw_schema)
10
14
  @find_object_cache = {}
11
15
  @path_item_finder = OpenAPIParser::PathItemFinder.new(paths) if paths # invalid definition
12
- @config = config
13
16
  @uri = uri
14
17
  @schema_registry = schema_registry
15
18
 
@@ -33,6 +36,33 @@ module OpenAPIParser::Schemas
33
36
  Gem::Version.new(openapi).release
34
37
  end
35
38
 
39
+ # Whether OpenAPI 3.1 runtime behavior applies where 3.0 and 3.1 define
40
+ # the same thing differently: the document declares 3.1 or later. A
41
+ # referenced file that declares no version follows the document that
42
+ # loaded it.
43
+ # @return [Boolean]
44
+ def use_3_1_features?
45
+ version = openapi_version
46
+ return version >= Gem::Version.new('3.1') if version
47
+ return @referrer.use_3_1_features? if openapi.nil? && @referrer
48
+
49
+ false
50
+ end
51
+
52
+ # Whether OpenAPI 3.2 runtime behavior applies: the document declares 3.2
53
+ # or later, or the allow_3_2_features config is set. A referenced file
54
+ # that declares no version follows the document that loaded it.
55
+ # @return [Boolean]
56
+ def use_3_2_features?
57
+ return true if @config.allow_3_2_features
58
+
59
+ version = openapi_version
60
+ return version >= Gem::Version.new('3.2') if version
61
+ return @referrer.use_3_2_features? if openapi.nil? && @referrer
62
+
63
+ false
64
+ end
65
+
36
66
  # @!attribute [r] paths
37
67
  # @return [Paths, nil]
38
68
  openapi_attr_object :paths, Paths, reference: false
@@ -59,6 +89,9 @@ module OpenAPIParser::Schemas
59
89
 
60
90
  # @return [OpenAPIParser::RequestOperation, nil]
61
91
  def request_operation(http_method, request_path)
92
+ # 3.1: `paths` is optional (a document may have only `webhooks`), so there is nothing to find
93
+ return nil unless @path_item_finder
94
+
62
95
  OpenAPIParser::RequestOperation.create(http_method, request_path, @path_item_finder, @config)
63
96
  end
64
97
 
@@ -71,7 +104,7 @@ module OpenAPIParser::Schemas
71
104
  loaded = @schema_registry[resolved_uri]
72
105
  return loaded if loaded
73
106
 
74
- OpenAPIParser.load_uri(resolved_uri, config: @config, schema_registry: @schema_registry)
107
+ OpenAPIParser.load_uri(resolved_uri, config: @config, schema_registry: @schema_registry, referrer: self)
75
108
  end
76
109
 
77
110
  private
@@ -3,20 +3,43 @@
3
3
 
4
4
  module OpenAPIParser::Schemas
5
5
  class PathItem < Base
6
+ # `query` is an OpenAPI 3.2 addition
7
+ STANDARD_METHODS = %w[get put post delete options head patch trace query].freeze
8
+ PRE_3_2_METHODS = (STANDARD_METHODS - %w[query]).freeze
9
+
6
10
  openapi_attr_values :summary, :description
7
11
 
8
- openapi_attr_objects :get, :put, :post, :delete, :options, :head, :patch, :trace, Operation
12
+ openapi_attr_objects :get, :put, :post, :delete, :options, :head, :patch, :trace, :query, Operation
9
13
  openapi_attr_list_object :parameters, Parameter, reference: true
10
14
 
11
- # @return [Operation]
15
+ # @!attribute [r] additional_operations
16
+ # @return [Hash{String => Operation}, nil] operations for non-standard HTTP methods (OpenAPI 3.2+)
17
+ openapi_attr_hash_object :additional_operations, Operation, reference: false, schema_key: :additionalOperations
18
+
19
+ # @return [Operation, nil]
12
20
  def operation(method)
13
- public_send(method)
14
- rescue NoMethodError
15
- nil
21
+ # before 3.2: exact-case standard methods only, as it always was
22
+ unless root.use_3_2_features?
23
+ return PRE_3_2_METHODS.include?(method.to_s) ? public_send(method.to_s) : nil
24
+ end
25
+
26
+ method_name = method.to_s.downcase
27
+ return public_send(method_name) if STANDARD_METHODS.include?(method_name)
28
+
29
+ additional_operation(method.to_s)
16
30
  end
17
31
 
18
32
  def set_path_item_to_operation
19
- [:get, :put, :post, :delete, :options, :head, :patch, :trace].each{ |method| operation(method)&.set_parent_path_item(self)}
33
+ STANDARD_METHODS.each { |method| operation(method)&.set_parent_path_item(self) }
34
+ additional_operations&.each_value { |op| op.set_parent_path_item(self) }
20
35
  end
36
+
37
+ private
38
+
39
+ # additionalOperations keys use the method's case as sent on the wire,
40
+ # and method names are case-sensitive (RFC 9110 §9.1)
41
+ def additional_operation(name)
42
+ additional_operations&.[](name)
43
+ end
21
44
  end
22
45
  end
@@ -12,7 +12,7 @@ module OpenAPIParser::Schemas
12
12
 
13
13
  # @!attribute [r] content
14
14
  # @return [Hash{String => MediaType}, nil] content type to MediaType object
15
- openapi_attr_hash_object :content, MediaType, reference: false
15
+ openapi_attr_hash_object :content, MediaType, reference: OpenAPIParser::MediaTypeSelectable::CONTENT_REFERENCE
16
16
 
17
17
  # @param [String] content_type
18
18
  # @param [Hash] params
@@ -6,9 +6,13 @@ module OpenAPIParser::Schemas
6
6
 
7
7
  openapi_attr_values :description
8
8
 
9
+ # @!attribute [r] summary
10
+ # @return [String, nil] short summary of the response (OpenAPI 3.2+)
11
+ openapi_attr_value :summary
12
+
9
13
  # @!attribute [r] content
10
14
  # @return [Hash{String => MediaType}, nil] content_type to MediaType hash
11
- openapi_attr_hash_object :content, MediaType, reference: false
15
+ openapi_attr_hash_object :content, MediaType, reference: OpenAPIParser::MediaTypeSelectable::CONTENT_REFERENCE
12
16
 
13
17
  # @!attribute [r] headers
14
18
  # @return [Hash{String => Header}, nil] header string to Header
@@ -1,4 +1,3 @@
1
- # TODO: support 'not' because I need check reference...
2
1
  # TODO: support 'xml', 'externalDocs'
3
2
  # TODO: support extended property
4
3
 
@@ -84,6 +83,10 @@ module OpenAPIParser::Schemas
84
83
  # @return [Boolean, nil]
85
84
  openapi_attr_value :write_only, schema_key: :writeOnly
86
85
 
86
+ # @!attribute [r] dependent_required
87
+ # @return [Hash{String => Array<String>}, nil]
88
+ openapi_attr_value :dependent_required, schema_key: :dependentRequired
89
+
87
90
  # @!attribute [r] all_of
88
91
  # @return [Array<Schema, Reference>, nil]
89
92
  openapi_attr_list_object :all_of, Schema, reference: true, schema_key: :allOf
@@ -96,6 +99,10 @@ module OpenAPIParser::Schemas
96
99
  # @return [Array<Schema, Reference>, nil]
97
100
  openapi_attr_list_object :any_of, Schema, reference: true, schema_key: :anyOf
98
101
 
102
+ # @!attribute [r] not
103
+ # @return [Schema, nil]
104
+ openapi_attr_object :not, Schema, reference: true
105
+
99
106
  # @!attribute [r] items
100
107
  # @return [Schema, nil]
101
108
  openapi_attr_object :items, Schema, reference: true
@@ -112,6 +119,10 @@ module OpenAPIParser::Schemas
112
119
  # @return [Hash{String => Schema}, nil]
113
120
  openapi_attr_hash_object :properties, Schema, reference: true
114
121
 
122
+ # @!attribute [r] defs
123
+ # @return [Hash{String => Schema}, nil] reusable schemas for `$ref` (OpenAPI 3.1+)
124
+ openapi_attr_hash_object :defs, Schema, reference: true, schema_key: :$defs
125
+
115
126
  # @!attribute [r] discriminator
116
127
  # @return [Discriminator, nil]
117
128
  openapi_attr_object :discriminator, Discriminator
@@ -46,12 +46,33 @@ module OpenAPIParser
46
46
  def each_schema(root, &block)
47
47
  return enum_for(:each_schema, root) unless block
48
48
 
49
- visited = {}
50
- walk(root, visited) do |node|
51
- yield node if node.is_a?(OpenAPIParser::Schemas::Schema)
49
+ each_node(root, OpenAPIParser::Schemas::Schema, &block)
50
+ end
51
+
52
+ # yields every parsed object reachable from root that is one of klasses
53
+ def each_node(root, *klasses)
54
+ walk(root, {}) do |node|
55
+ yield node if klasses.any? { |klass| node.is_a?(klass) }
56
+ end
57
+ end
58
+
59
+ # security schemes are not modeled by the parse layer, so rules
60
+ # about them read the raw `components.securitySchemes` map
61
+ def each_security_scheme(root)
62
+ components = root.raw_schema.is_a?(Hash) ? root.raw_schema['components'] : nil
63
+ schemes = components.is_a?(Hash) ? components['securitySchemes'] : nil
64
+ return unless schemes.is_a?(Hash)
65
+
66
+ schemes.each do |name, scheme|
67
+ yield(name, scheme) if scheme.is_a?(Hash)
52
68
  end
53
69
  end
54
70
 
71
+ # escapes a map key for use in a violation path, matching object_reference
72
+ def escape_reference(key)
73
+ key.to_s.gsub('~', '~0').gsub('/', '~1')
74
+ end
75
+
55
76
  def walk(node, visited, &block)
56
77
  return unless node.respond_to?(:_openapi_all_child_objects)
57
78
  return if visited[node.object_id]
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `additionalOperations` is a 3.2 Path Item addition for non-standard
5
+ # HTTP methods. The parse layer accepts it permissively; this rule
6
+ # reports the version mismatch.
7
+ class AdditionalOperationsBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::PathItem) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw.key?('additionalOperations')
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/additionalOperations",
18
+ message: '`additionalOperations` is a 3.2 Path Item addition; earlier documents have no such field',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 deprecates `allowEmptyValue` on the Parameter Object (3.1 only
5
+ # called it NOT RECOMMENDED). The field is still allowed but
6
+ # discouraged, so we report it as a violation on 3.2 documents.
7
+ class AllowEmptyValueDeprecation < Rule
8
+ def check(root)
9
+ return [] unless version_at_least?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::Parameter) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw.key?('allowEmptyValue')
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/allowEmptyValue",
18
+ message: '`allowEmptyValue` on a Parameter Object is deprecated in 3.2',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `style: cookie` is a 3.2 Parameter style for cookie serialization.
5
+ # Runtime validation ignores `style`; this rule reports the version
6
+ # mismatch.
7
+ class CookieStyleBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_node(root, OpenAPIParser::Schemas::Parameter) do |node|
13
+ raw = node.raw_schema
14
+ next unless raw.is_a?(Hash) && raw['style'] == 'cookie'
15
+
16
+ violations << violation(
17
+ path: "#{node.object_reference}/style",
18
+ message: '`style: cookie` is a 3.2 Parameter style; earlier documents have no such style',
19
+ )
20
+ end
21
+ violations
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `defaultMapping` on the Discriminator Object is a 3.2 addition. The
5
+ # parse layer accepts it permissively; this rule reports the version
6
+ # mismatch.
7
+ class DefaultMappingBefore32 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.2')
10
+
11
+ violations = []
12
+ each_schema(root) do |schema|
13
+ raw = schema.raw_schema
14
+ next unless raw.is_a?(Hash)
15
+
16
+ discriminator = raw['discriminator']
17
+ next unless discriminator.is_a?(Hash) && discriminator.key?('defaultMapping')
18
+
19
+ violations << violation(
20
+ path: "#{schema.object_reference}/discriminator/defaultMapping",
21
+ message: '`defaultMapping` on a Discriminator Object is a 3.2 addition; earlier documents have no such field',
22
+ )
23
+ end
24
+ violations
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `$defs` is a JSON Schema 2020-12 keyword that 3.1 adopted. 3.0
5
+ # Schema Objects do not define it. The parse layer resolves `$ref`s
6
+ # into it regardless of version; this rule reports the version mismatch.
7
+ class DefsIn30 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.1')
10
+
11
+ violations = []
12
+ each_schema(root) do |schema|
13
+ next unless schema.raw_schema.is_a?(Hash) && schema.raw_schema.key?('$defs')
14
+
15
+ violations << violation(
16
+ path: schema.object_reference,
17
+ message: '`$defs` is a 3.1 addition (from JSON Schema 2020-12); 3.0 Schema Objects do not define it',
18
+ )
19
+ end
20
+ violations
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `dependentRequired` is a JSON Schema 2020-12 keyword that 3.1 adopted.
5
+ # 3.0 Schema Objects do not define it. The validator checks it regardless
6
+ # of version; this rule reports the version mismatch.
7
+ class DependentRequiredIn30 < Rule
8
+ def check(root)
9
+ return [] unless version_before?('3.1')
10
+
11
+ violations = []
12
+ each_schema(root) do |schema|
13
+ next unless schema.raw_schema.is_a?(Hash) && schema.raw_schema.key?('dependentRequired')
14
+
15
+ violations << violation(
16
+ path: schema.object_reference,
17
+ message: '`dependentRequired` is a 3.1 addition (from JSON Schema 2020-12); 3.0 Schema Objects do not define it',
18
+ )
19
+ end
20
+ violations
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # 3.2 adds `dataValue` and `serializedValue` to the Example Object.
5
+ # Example Objects are not modeled by the parse layer, so this rule
6
+ # inspects the raw `examples` maps on Parameter, Media Type, and
7
+ # Header objects, plus `components.examples`. Media Types under a
8
+ # Parameter or Header `content` and anything under callbacks are not
9
+ # modeled, so examples there are not reached.
10
+ class ExampleValueFieldsBefore32 < Rule
11
+ NEW_FIELDS = %w[dataValue serializedValue].freeze
12
+
13
+ def check(root)
14
+ return [] unless version_before?('3.2')
15
+
16
+ violations = []
17
+ node_classes = [
18
+ OpenAPIParser::Schemas::Parameter,
19
+ OpenAPIParser::Schemas::MediaType,
20
+ OpenAPIParser::Schemas::Header,
21
+ OpenAPIParser::Schemas::Components,
22
+ ]
23
+ each_node(root, *node_classes) do |node|
24
+ raw = node.raw_schema
25
+ next unless raw.is_a?(Hash)
26
+
27
+ examples = raw['examples']
28
+ next unless examples.is_a?(Hash)
29
+
30
+ examples.each do |name, example|
31
+ next unless example.is_a?(Hash)
32
+
33
+ NEW_FIELDS.each do |field|
34
+ next unless example.key?(field)
35
+
36
+ violations << violation(
37
+ path: "#{node.object_reference}/examples/#{escape_reference(name)}/#{field}",
38
+ message: "`#{field}` on an Example Object is a 3.2 addition; earlier documents have no such field",
39
+ )
40
+ end
41
+ end
42
+ end
43
+ violations
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end