openapi_parser 2.6.0 → 2.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 265685ba8a1e648f9b4611356022227b5d80917f46d2450dae55747ffec40a23
4
- data.tar.gz: 9ba7d7d561d7ec84a5ce6a8c42cb13beffa09e1250699b206f6e4ec639350575
3
+ metadata.gz: 692fe438d163494e486fd8f95df4346f1bc2bbc83fd1ecbfd751da1c946f41b4
4
+ data.tar.gz: cd650f7d43d6c3911a11e10387778199ec9ab09f563c978a1989839a11124693
5
5
  SHA512:
6
- metadata.gz: 4168f6e1dcedcd2c8be552937692215f8214511df0979f7bcc3d4ec00181980e5d397bc5cb0288cf9fc498eafc3a1322f0f1579bad89fb4915221b6b62a4f667
7
- data.tar.gz: 6fe2b4ae0877312fcdd0f6fc56643c7658e9e6325cffaa066caec1b263e6657c88db5a7233d6bd24ce7d8270ff8d5e1d7d8c7b38188e4ac237ca86cb5f374a4c
6
+ metadata.gz: 06be112cf0f5e793510d57dbf851a79375f9df3f6bb5f5dd7cba0e4ffd6ef3f0ee44dcb4d4be6487d3f4c9fb30da65ab3815f97278f1314ceb0327e260ba4899
7
+ data.tar.gz: f65c447ee885a7b758d01b7ee65409b12ee4f8b3090f7f1dfe75ae4d520fe843493c79f2af2588fef209590d744198d7f16dc165da0dd8077f4c58ac390a854a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 2.8.0 (2026-11-16)
4
+ * match an exact `additionalOperations` key before a standard method in 3.2 documents, so a `Get` request finds `additionalOperations.Get` rather than `get` ([OpenAPI 3.2](https://spec.openapis.org/oas/v3.2.0.html#path-item-object), [RFC 9110 §9.1](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.1))
5
+ * check the keywords next to `$ref` in Schema Objects of OpenAPI 3.1 documents, such as `{ $ref: ..., maxLength: 3 }`. They were ignored before, so a value that passed may now fail. A Reference Object, and any object in a 3.0 document, still ignores them. Readers such as `Schema#type` still return the values of the referenced schema
6
+ * accept a value that matches exactly one `oneOf` member as it is when `coerce_value` is on, and keep it uncoerced. `"1"` for `oneOf: [integer, string]` failed with `NotOneOf` before, because coercion made it match both
7
+ * read strings as decimal numbers when `coerce_value` is on. For `type: integer`, `"010"` is now `10`, not octal `8`, and `"0x10"`, `"0b10"` and `"0o10"` are rejected. For `type: number`, hexadecimal such as `"0x10"` is rejected. This changes `"010"` from `8` to `10` without an error, so check any client that sends zero-padded integers (#251)
8
+ * support the `propertyNames` keyword (OpenAPI 3.1): every property name of an object is checked against its schema, and the first name that fails returns the error from that schema, such as `InvalidPattern` or `MoreThanMaxLength`. Names are checked as strings even when `coerce_value` is on. It was ignored before, so a value that passed may now fail
9
+ * add `SpecValidator` rule `PropertyNamesIn30`: detect `propertyNames` in 3.0 documents (3.1 addition)
10
+ * check `enum` on objects and arrays (OpenAPI 3.0 and 3.1). A value now has to equal one of the members, as in JSON Schema: objects are compared by keys and values, and arrays element by element in order. It was ignored before, so a value that passed may now fail. In particular, `enum` written next to `items`, such as `{ type: array, items: { type: string }, enum: [a, b] }`, now rejects every array. To limit the elements, move `enum` into `items`
11
+ * support the `patternProperties` keyword (OpenAPI 3.1): a property whose name matches a pattern is now checked against that pattern's schema, as well as its `properties` schema. Such a property is no longer an additional property, so `additionalProperties` does not apply to it. It was ignored before, so a value that passed may now fail, and a value that failed on `additionalProperties` may now pass
12
+ * add `SpecValidator` rule `PatternPropertiesIn30`: detect `patternProperties` in 3.0 documents (3.1 addition)
13
+
14
+ ## 2.7.0 (2026-11-06)
15
+ * match `additionalOperations` method names case-sensitively ([OpenAPI 3.2](https://spec.openapis.org/oas/v3.2.0.html#path-item-object), [RFC 9110 §9.1](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.1)). Standard methods still match their fixed fields in any case
16
+ * check the keywords next to `allOf`, `anyOf` and `oneOf` (OpenAPI 3.0 and 3.1), such as `{ allOf: [...], required: [a] }`, and all of these keywords when a schema has more than one. They were ignored before, so a value that passed may now fail. In particular, `additionalProperties: false` next to `allOf` now rejects the properties defined only in `allOf`, as in JSON Schema
17
+ * return the coerced value from `allOf` and `oneOf` when `coerce_value` is on. They returned the original value before
18
+ * support the `dependentRequired` keyword (OpenAPI 3.1): an object that has a property but not the properties it depends on now fails with `NotExistDependentRequiredKey`. It was ignored before, so a value that passed may now fail
19
+ * add `SpecValidator` rule `DependentRequiredIn30`: detect `dependentRequired` in 3.0 documents (3.1 addition)
20
+
3
21
  ## 2.6.0 (2026-11-04)
4
22
  * support the `multipleOf` keyword (OpenAPI 3.0 and 3.1): a number that is not a multiple of it now fails with `NotMultipleOf`. Decimals are compared exactly, so `19.99` is a multiple of `0.01`. A `multipleOf` that is not a positive number is still ignored. It was ignored before, so a value that passed may now fail
5
23
  * support the `not` keyword (OpenAPI 3.0 and 3.1): a value that is valid against the `not` schema now fails with `NotSchemaMatched`. It was ignored before, so a value that passed may now fail
data/README.md CHANGED
@@ -127,7 +127,7 @@ OpenAPIParser.parse(schema, strict_specification_version: :raise)
127
127
 
128
128
  Detected mismatches:
129
129
 
130
- - 3.1 additions in 3.0 documents: `const`, `prefixItems`, array-form `type`, `type: "null"`, `webhooks`, `jsonSchemaDialect`, `components.pathItems`, `$dynamicRef`, `$dynamicAnchor`, `contentEncoding`, `contentMediaType`, `contentSchema`, `$defs`, boolean schemas
130
+ - 3.1 additions in 3.0 documents: `const`, `prefixItems`, array-form `type`, `type: "null"`, `webhooks`, `jsonSchemaDialect`, `components.pathItems`, `$dynamicRef`, `$dynamicAnchor`, `contentEncoding`, `contentMediaType`, `contentSchema`, `$defs`, `dependentRequired`, `propertyNames`, `patternProperties`, boolean schemas
131
131
  - the 3.0 Boolean vs 3.1 numeric forms of `exclusiveMinimum` / `exclusiveMaximum`
132
132
  - 3.2 additions in earlier documents: `$self`, the `query` operation, `additionalOperations`, `components.mediaTypes`, Tag `summary` / `parent` / `kind`, Server `name`, Example `dataValue` / `serializedValue`, XML `nodeType`, Security Scheme `deprecated` / `oauth2MetadataUrl` and the `deviceAuthorization` flow, Media Type and Encoding `itemSchema` / `itemEncoding` / `prefixEncoding`, Discriminator `defaultMapping`, Parameter `in: querystring` and `style: cookie`, Response `summary`
133
133
  - keywords removed or deprecated by the declared version: `nullable` and singular schema `example` in 3.1+, XML `attribute: true` / `wrapped: true` and Parameter `allowEmptyValue` in 3.2
@@ -1,5 +1,8 @@
1
1
  # loader base class for create OpenAPI::Schemas::Base object
2
2
  class OpenAPIParser::SchemaLoader::Creator < OpenAPIParser::SchemaLoader::Base
3
+ # these do not change validation, so a `$ref` with only these stays a Reference
4
+ REF_ANNOTATIONS = %w[$ref $comment title description summary deprecated default example examples readOnly writeOnly].freeze
5
+
3
6
  # @param [String] variable_name
4
7
  # @param [Hash] options
5
8
  def initialize(variable_name, options)
@@ -35,6 +38,15 @@ class OpenAPIParser::SchemaLoader::Creator < OpenAPIParser::SchemaLoader::Base
35
38
  check_schema.kind_of?(::Hash)
36
39
  end
37
40
 
41
+ # 3.1: a Schema Object applies the keywords next to `$ref`. A Reference Object,
42
+ # and any object in 3.0, ignores them.
43
+ def reference_class(target_object, schema)
44
+ return OpenAPIParser::Schemas::Reference unless @klass == OpenAPIParser::Schemas::Schema && target_object.root.use_3_1_features?
45
+ return OpenAPIParser::Schemas::Reference unless schema.keys.any? { |key| !REF_ANNOTATIONS.include?(key) && !key.start_with?('x-') }
46
+
47
+ OpenAPIParser::Schemas::SchemaWithRef
48
+ end
49
+
38
50
  def escape_reference(str)
39
51
  str.to_s.gsub('~', '~0').gsub('/', '~1')
40
52
  end
@@ -45,7 +57,7 @@ class OpenAPIParser::SchemaLoader::Creator < OpenAPIParser::SchemaLoader::Base
45
57
  if @allow_data_type && !check_object_schema?(schema)
46
58
  schema
47
59
  elsif allow_reference?(target_object) && check_reference_schema?(schema)
48
- OpenAPIParser::Schemas::Reference.new(ref, target_object, target_object.root, schema)
60
+ reference_class(target_object, schema).new(ref, target_object, target_object.root, schema)
49
61
  else
50
62
  @klass.new(ref, target_object, target_object.root, schema)
51
63
  end
@@ -56,6 +56,18 @@ module OpenAPIParser
56
56
  end
57
57
  end
58
58
 
59
+ class NotExistDependentRequiredKey < OpenAPIError
60
+ def initialize(property, keys, reference)
61
+ super(reference)
62
+ @property = property
63
+ @keys = keys
64
+ end
65
+
66
+ def message
67
+ "#{@reference} missing parameters required by #{@property}: #{@keys.join(", ")}"
68
+ end
69
+ end
70
+
59
71
  class NotExistPropertyDefinition < OpenAPIError
60
72
  def initialize(keys, reference)
61
73
  super(reference)
@@ -10,26 +10,33 @@ class OpenAPIParser::SchemaValidator
10
10
  end
11
11
 
12
12
  # if any schema return error, it's not a valid all_of
13
+ # Members with `type` go first, so that the other members see the coerced value.
14
+ steps = schema.all_of.partition(&:type).flatten.map do |s|
15
+ lambda do |input|
16
+ # We need to store the reference to all of, so we can perform strict check on allowed properties
17
+ validatable.validate_schema(
18
+ input,
19
+ s,
20
+ :parent_all_of => true,
21
+ parent_discriminator_schemas: keyword_args[:parent_discriminator_schemas] || []
22
+ )
23
+ end
24
+ end
25
+ coerced, err = validatable.validate_in_order(value, steps)
26
+ return [nil, err] if err
27
+
13
28
  remaining_keys = value.kind_of?(Hash) ? value.keys : []
14
29
  nested_additional_properties = false
15
30
  schema.all_of.each do |s|
16
- # We need to store the reference to all of, so we can perform strict check on allowed properties
17
- _coerced, err = validatable.validate_schema(
18
- value,
19
- s,
20
- :parent_all_of => true,
21
- parent_discriminator_schemas: keyword_args[:parent_discriminator_schemas] || []
22
- )
23
-
24
31
  if s.type == "object"
25
32
  remaining_keys -= (s.properties || {}).keys
33
+ patterns = (s.pattern_properties || {}).keys
34
+ remaining_keys = remaining_keys.reject { |key| patterns.any? { |pattern| key =~ /#{pattern}/ } }
26
35
  nested_additional_properties = true if s.additional_properties
27
36
  else
28
37
  # If this is not allOf having array of objects inside, but e.g. having another anyOf/oneOf nested
29
38
  remaining_keys.clear
30
39
  end
31
-
32
- return [nil, err] if err
33
40
  end
34
41
 
35
42
  # If there are nested additionalProperties, we allow not defined extra properties and lean on the specific
@@ -38,7 +45,7 @@ class OpenAPIParser::SchemaValidator
38
45
  return [nil, OpenAPIParser::NotExistPropertyDefinition.new(remaining_keys, schema.object_reference)]
39
46
  end
40
47
 
41
- [value, nil]
48
+ [coerced, nil]
42
49
  end
43
50
  end
44
51
  end
@@ -1,5 +1,7 @@
1
1
  class OpenAPIParser::SchemaValidator
2
2
  class ArrayValidator < Base
3
+ include ::OpenAPIParser::SchemaValidator::Enumable
4
+
3
5
  # @param [Array] value
4
6
  # @param [OpenAPIParser::Schemas::Schema] schema
5
7
  def coerce_and_validate(value, schema, **_keyword_args)
@@ -26,7 +28,7 @@ class OpenAPIParser::SchemaValidator
26
28
 
27
29
  value.each_index { |idx| value[idx] = coerced_values[idx] } if @coerce_value
28
30
 
29
- [value, nil]
31
+ check_enum_include(value, schema)
30
32
  end
31
33
 
32
34
  def validate_max_min_items(value, schema)
@@ -4,6 +4,9 @@ class OpenAPIParser::SchemaValidator
4
4
  include ::OpenAPIParser::SchemaValidator::MinimumMaximum
5
5
  include ::OpenAPIParser::SchemaValidator::MultipleOf
6
6
 
7
+ # Float() has no base argument and reads hexadecimal such as "0x10" and "0x1p3".
8
+ HEXADECIMAL = /\A\s*[+-]?0x/i.freeze
9
+
7
10
  # validate float value by schema
8
11
  # @param [Object] value
9
12
  # @param [OpenAPIParser::Schemas::Schema] schema
@@ -30,6 +33,8 @@ class OpenAPIParser::SchemaValidator
30
33
  end
31
34
 
32
35
  def coerce(value)
36
+ return value if value.kind_of?(String) && value.match?(HEXADECIMAL)
37
+
33
38
  Float(value)
34
39
  rescue ArgumentError, TypeError
35
40
  value
@@ -41,7 +41,8 @@ class OpenAPIParser::SchemaValidator
41
41
  return value.finite? && value == value.floor ? value.to_i : value if value.kind_of?(Float)
42
42
 
43
43
  begin
44
- Integer(value)
44
+ # Base 10, so "010" is 10 rather than octal 8, and 0x, 0b and 0o are rejected.
45
+ Integer(value, 10)
45
46
  rescue ArgumentError, TypeError
46
47
  value
47
48
  end
@@ -1,5 +1,6 @@
1
1
  class OpenAPIParser::SchemaValidator
2
2
  class ObjectValidator < Base
3
+ include ::OpenAPIParser::SchemaValidator::Enumable
3
4
  include ::OpenAPIParser::SchemaValidator::PropertiesNumber
4
5
 
5
6
  # @param [Hash] value
@@ -10,6 +11,7 @@ class OpenAPIParser::SchemaValidator
10
11
  return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Hash)
11
12
 
12
13
  properties = schema.properties || {}
14
+ pattern_properties = schema.pattern_properties || {}
13
15
  additional_properties = schema.additional_properties
14
16
 
15
17
  required_set = schema.required ? schema.required.to_set : Set.new
@@ -25,11 +27,14 @@ class OpenAPIParser::SchemaValidator
25
27
  remaining_keys.delete('discriminator')
26
28
  end
27
29
 
30
+ property_names_error = check_property_names(value, schema)
31
+ return [nil, property_names_error] if property_names_error
32
+
28
33
  coerced_values = value.map do |name, v|
29
- s = properties[name]
30
- coerced, err = if s
34
+ property_schemas = [properties[name], *matching_pattern_schemas(name, pattern_properties)].compact
35
+ coerced, err = if !property_schemas.empty?
31
36
  remaining_keys.delete(name)
32
- validatable.validate_schema(v, s)
37
+ validate_property(v, property_schemas)
33
38
  # TODO: better handling for parent_all_of with additional_properties
34
39
  elsif !parent_all_of && additional_properties.is_a?(OpenAPIParser::Schemas::Schema)
35
40
  remaining_keys.delete(name)
@@ -53,9 +58,63 @@ class OpenAPIParser::SchemaValidator
53
58
  end
54
59
  return [nil, OpenAPIParser::NotExistRequiredKey.new(required_set.to_a, schema.object_reference)] unless required_set.empty?
55
60
 
61
+ dependent_required_error = check_dependent_required(value, schema)
62
+ return [nil, dependent_required_error] if dependent_required_error
63
+
56
64
  value.merge!(coerced_values.to_h) if @coerce_value
57
65
 
66
+ value, err = check_enum_include(value, schema)
67
+ return [nil, err] if err
68
+
58
69
  check_properties_number(value, schema)
59
70
  end
71
+
72
+ private
73
+
74
+ # Property names are always strings, so they are checked without coercion.
75
+ # A name like "5" does not become 5 for `propertyNames: { type: integer }`.
76
+ # @return [OpenAPIParser::OpenAPIError, nil]
77
+ def check_property_names(value, schema)
78
+ property_names = schema.property_names
79
+ return unless property_names
80
+
81
+ validator = OpenAPIParser::SchemaValidator.new(nil, property_names, OpenAPIParser::SchemaValidator::Options.new)
82
+ value.each_key do |name|
83
+ _, err = validator.validate_schema(name, property_names)
84
+ return err if err
85
+ end
86
+ nil
87
+ end
88
+
89
+ # @param [String] name
90
+ # @param [Hash{String => OpenAPIParser::Schemas::Schema}] pattern_properties
91
+ # @return [Array<OpenAPIParser::Schemas::Schema>] the schemas whose pattern matches the name
92
+ def matching_pattern_schemas(name, pattern_properties)
93
+ # Same regular expression handling as the pattern keyword (StringValidator#pattern_validate)
94
+ pattern_properties.select { |pattern, _| name =~ /#{pattern}/ }.values
95
+ end
96
+
97
+ # Check the value against each schema in turn, passing on the coerced value.
98
+ # @return [Array] coerced value and error
99
+ def validate_property(value, schemas)
100
+ steps = schemas.map { |s| ->(current) { validatable.validate_schema(current, s) } }
101
+ validatable.validate_in_order(value, steps)
102
+ end
103
+
104
+ # @return [OpenAPIParser::NotExistDependentRequiredKey, nil]
105
+ def check_dependent_required(value, schema)
106
+ dependent_required = schema.dependent_required
107
+ return unless dependent_required.is_a?(Hash)
108
+
109
+ dependent_required.each do |property, keys|
110
+ next unless value.key?(property) && keys.is_a?(Array)
111
+
112
+ missing_keys = keys.reject { |key| value.key?(key) }
113
+ next if missing_keys.empty?
114
+
115
+ return OpenAPIParser::NotExistDependentRequiredKey.new(property, missing_keys, schema.object_reference)
116
+ end
117
+ nil
118
+ end
60
119
  end
61
120
  end
@@ -10,13 +10,18 @@ class OpenAPIParser::SchemaValidator
10
10
  return validate_discriminator_schema(schema.discriminator, value, candidate_schemas: schema.one_of)
11
11
  end
12
12
 
13
- # if multiple schemas are satisfied, it's not valid
14
- result = schema.one_of.one? do |s|
15
- _coerced, err = validatable.validate_schema(value, s)
16
- err.nil?
13
+ # A value that matches a member as it is does not need coercion. Coercion
14
+ # could make it match other members too, like "1" for [integer, string].
15
+ if @coerce_value
16
+ literal_matches = schema.one_of.select { |s| validatable.valid_without_coercion?(value, s) }
17
+ return validatable.validate_schema(value, literal_matches.first) if literal_matches.one?
18
+ return [nil, OpenAPIParser::NotOneOf.new(value, schema.object_reference)] if literal_matches.size > 1
17
19
  end
18
- if result
19
- [value, nil]
20
+
21
+ # if multiple schemas are satisfied, it's not valid
22
+ matched = schema.one_of.map { |s| validatable.validate_schema(value, s) }.select { |_coerced, err| err.nil? }
23
+ if matched.one?
24
+ [matched.first.first, nil]
20
25
  else
21
26
  [nil, OpenAPIParser::NotOneOf.new(value, schema.object_reference)]
22
27
  end
@@ -40,6 +40,7 @@ class OpenAPIParser::SchemaValidator
40
40
 
41
41
  # Types to pick from for a schema without `type`. integer comes before
42
42
  # number so that an Integer is checked as an integer.
43
+ JSON_VALUE_CLASSES = [Hash, Array, String, Numeric, TrueClass, FalseClass, NilClass].freeze
43
44
  VALUE_TYPES = %w[object array string integer number boolean].freeze
44
45
 
45
46
  class << self
@@ -79,6 +80,7 @@ class OpenAPIParser::SchemaValidator
79
80
  # @param [OpenAPIParser::Schemas::Schema] schema
80
81
  def validate_schema(value, schema, **keyword_args)
81
82
  return [value, nil] unless schema
83
+ return validate_schema_with_ref(value, schema, **keyword_args) if schema.is_a?(OpenAPIParser::Schemas::SchemaWithRef)
82
84
 
83
85
  # 3.1: a boolean schema accepts (`true`) or rejects (`false`) any value,
84
86
  # including null.
@@ -100,18 +102,9 @@ class OpenAPIParser::SchemaValidator
100
102
  return [nil, OpenAPIParser::NotSchemaMatched.new(value, schema.not.object_reference)] unless not_error
101
103
  end
102
104
 
103
- return coerce_into_type_array(value, schema, **keyword_args) if coerce_into_type_array?(value, schema)
105
+ return validate_composition(value, schema, **keyword_args) if composition_validators(schema).any?
104
106
 
105
- if (v = validator(value, schema))
106
- if keyword_args.empty?
107
- return v.coerce_and_validate(value, schema)
108
- else
109
- return v.coerce_and_validate(value, schema, **keyword_args)
110
- end
111
- end
112
-
113
- # unknown return error
114
- OpenAPIParser::ValidateError.build_error_result(value, schema)
107
+ validate_without_composition(value, schema, **keyword_args)
115
108
  end
116
109
 
117
110
  # validate integer value by schema
@@ -122,13 +115,93 @@ class OpenAPIParser::SchemaValidator
122
115
  integer_validator.coerce_and_validate(value, schema)
123
116
  end
124
117
 
118
+ # @param [Object] value
119
+ # @param [OpenAPIParser::Schemas::Schema] schema
120
+ # @return [Boolean] whether the value is valid as it is, without coerce_value
121
+ def valid_without_coercion?(value, schema)
122
+ @validator_without_coercion ||= self.class.new(value, schema, Options.new(allow_empty_date_and_datetime: @allow_empty_date_and_datetime))
123
+ _value, err = @validator_without_coercion.validate_schema(value, schema)
124
+ err.nil?
125
+ end
126
+
127
+ # Run the steps in order, each with the value coerced by the steps before it.
128
+ # Keywords check JSON values. A coerced value of another class, like DateTime,
129
+ # is only the output form, so the later steps check the original value.
130
+ # @param [Object] value
131
+ # @param [Array<Proc>] steps each takes a value and returns coerced value and error
132
+ # @return [Array] coerced value and error
133
+ def validate_in_order(value, steps)
134
+ current = value
135
+ steps.each do |step|
136
+ returned, err = step.call(json_value?(current) ? current : value)
137
+ return [nil, err] if err
138
+
139
+ current = returned if json_value?(current)
140
+ end
141
+ [current, nil]
142
+ end
143
+
125
144
  private
126
145
 
146
+ def json_value?(value)
147
+ JSON_VALUE_CLASSES.any? { |klass| value.is_a?(klass) }
148
+ end
149
+
150
+ # `$ref` and the keywords next to it both apply. The one with `type` goes first,
151
+ # so that the other sees the coerced value.
152
+ # @return [Array] coerced value and error
153
+ def validate_schema_with_ref(value, schema, **keyword_args)
154
+ target = schema.ref_target
155
+ return validate_schema(value, schema.ref_siblings, **keyword_args) unless target.is_a?(OpenAPIParser::Schemas::Schema)
156
+
157
+ steps = [target, schema.ref_siblings].partition(&:type).flatten.map do |s|
158
+ ->(input) { validate_schema(input, s, **keyword_args) }
159
+ end
160
+ validate_in_order(value, steps)
161
+ end
162
+
163
+ # @return [Array<OpenAPIParser::SchemaValidator::Base>]
164
+ def composition_validators(schema)
165
+ validators = []
166
+ validators << any_of_validator if schema.any_of
167
+ validators << all_of_validator if schema.all_of
168
+ validators << one_of_validator if schema.one_of
169
+ validators
170
+ end
171
+
172
+ # All the keywords in a schema apply to the value, so the composition
173
+ # keywords and the keywords next to them are all checked.
174
+ # `type` goes first, so that the other steps see the coerced value.
175
+ # @return [Array] coerced value and error
176
+ def validate_composition(value, schema, **keyword_args)
177
+ composition_steps = composition_validators(schema).map do |v|
178
+ ->(input) { v.coerce_and_validate(input, schema, **keyword_args) }
179
+ end
180
+ # the composition keyword already used discriminator
181
+ parents = (keyword_args[:parent_discriminator_schemas] || []) + [schema]
182
+ other_step = ->(input) { validate_without_composition(input, schema, **keyword_args, parent_discriminator_schemas: parents) }
183
+
184
+ validate_in_order(value, schema.type ? [other_step, *composition_steps] : [*composition_steps, other_step])
185
+ end
186
+
187
+ # @return [Array] coerced value and error
188
+ def validate_without_composition(value, schema, **keyword_args)
189
+ return coerce_into_type_array(value, schema, **keyword_args) if coerce_into_type_array?(value, schema)
190
+
191
+ if (v = validator(value, schema))
192
+ if keyword_args.empty?
193
+ return v.coerce_and_validate(value, schema)
194
+ else
195
+ return v.coerce_and_validate(value, schema, **keyword_args)
196
+ end
197
+ end
198
+
199
+ # unknown return error
200
+ OpenAPIParser::ValidateError.build_error_result(value, schema)
201
+ end
202
+
127
203
  # @return [OpenAPIParser::SchemaValidator::Base, nil]
128
204
  def validator(value, schema)
129
- return any_of_validator if schema.any_of
130
- return all_of_validator if schema.all_of
131
- return one_of_validator if schema.one_of
132
205
  return nil_validator if value.nil?
133
206
 
134
207
  # 3.1: `format: binary` with `string` in an array-form `type` accepts a
@@ -197,7 +270,6 @@ class OpenAPIParser::SchemaValidator
197
270
  # @return [Boolean]
198
271
  def coerce_into_type_array?(value, schema)
199
272
  return false unless @coerce_value && !value.nil?
200
- return false if schema.any_of || schema.all_of || schema.one_of
201
273
 
202
274
  schema.type.is_a?(Array) && pick_type(value, schema.type, schema).nil?
203
275
  end
@@ -23,26 +23,25 @@ module OpenAPIParser::Schemas
23
23
  return PRE_3_2_METHODS.include?(method.to_s) ? public_send(method.to_s) : nil
24
24
  end
25
25
 
26
- method_name = method.to_s.downcase
27
- return public_send(method_name) if STANDARD_METHODS.include?(method_name)
26
+ # an exact additionalOperations key wins, so `Get` isn't routed to `get`
27
+ additional = additional_operation(method.to_s)
28
+ return additional if additional
28
29
 
29
- additional_operation(method.to_s)
30
+ method_name = method.to_s.downcase
31
+ STANDARD_METHODS.include?(method_name) ? public_send(method_name) : nil
30
32
  end
31
33
 
32
34
  def set_path_item_to_operation
33
- STANDARD_METHODS.each { |method| operation(method)&.set_parent_path_item(self) }
35
+ STANDARD_METHODS.each { |method| public_send(method)&.set_parent_path_item(self) }
34
36
  additional_operations&.each_value { |op| op.set_parent_path_item(self) }
35
37
  end
36
38
 
37
39
  private
38
40
 
39
- # additionalOperations keys are method names as sent on the wire
40
- # (conventionally uppercase); prefer an exact match, else ignore case
41
+ # additionalOperations keys use the method's case as sent on the wire,
42
+ # and method names are case-sensitive (RFC 9110 §9.1)
41
43
  def additional_operation(name)
42
- operations = additional_operations
43
- return nil unless operations
44
-
45
- operations.fetch(name) { operations.find { |key, _| key.casecmp?(name) }&.last }
44
+ additional_operations&.[](name)
46
45
  end
47
46
  end
48
47
  end
@@ -83,6 +83,10 @@ module OpenAPIParser::Schemas
83
83
  # @return [Boolean, nil]
84
84
  openapi_attr_value :write_only, schema_key: :writeOnly
85
85
 
86
+ # @!attribute [r] dependent_required
87
+ # @return [Hash{String => Array<String>}, nil]
88
+ openapi_attr_value :dependent_required, schema_key: :dependentRequired
89
+
86
90
  # @!attribute [r] all_of
87
91
  # @return [Array<Schema, Reference>, nil]
88
92
  openapi_attr_list_object :all_of, Schema, reference: true, schema_key: :allOf
@@ -99,6 +103,10 @@ module OpenAPIParser::Schemas
99
103
  # @return [Schema, nil]
100
104
  openapi_attr_object :not, Schema, reference: true
101
105
 
106
+ # @!attribute [r] property_names
107
+ # @return [Schema, nil]
108
+ openapi_attr_object :property_names, Schema, reference: true, schema_key: :propertyNames
109
+
102
110
  # @!attribute [r] items
103
111
  # @return [Schema, nil]
104
112
  openapi_attr_object :items, Schema, reference: true
@@ -115,6 +123,10 @@ module OpenAPIParser::Schemas
115
123
  # @return [Hash{String => Schema}, nil]
116
124
  openapi_attr_hash_object :properties, Schema, reference: true
117
125
 
126
+ # @!attribute [r] pattern_properties
127
+ # @return [Hash{String => Schema}, nil] schemas for properties whose names match the regular expression keys (OpenAPI 3.1+)
128
+ openapi_attr_hash_object :pattern_properties, Schema, reference: true, schema_key: :patternProperties
129
+
118
130
  # @!attribute [r] defs
119
131
  # @return [Hash{String => Schema}, nil] reusable schemas for `$ref` (OpenAPI 3.1+)
120
132
  openapi_attr_hash_object :defs, Schema, reference: true, schema_key: :$defs
@@ -0,0 +1,39 @@
1
+ module OpenAPIParser::Schemas
2
+ # A Schema Object with `$ref` and other keywords next to it (OpenAPI 3.1).
3
+ #
4
+ # The referenced schema is shared, so the keywords cannot be merged into it.
5
+ # The `$ref` is the base of this schema, so readers see the referenced schema,
6
+ # as they do with a plain `$ref`. The validator checks both schemas.
7
+ class SchemaWithRef < Schema
8
+ # @return [Schema, Reference] Reference only when it cannot be resolved
9
+ attr_reader :ref_target
10
+
11
+ # @return [Schema] the keywords next to `$ref`
12
+ attr_reader :ref_siblings
13
+
14
+ (Schema.public_instance_methods(false) - [:load_data]).each do |name|
15
+ define_method(name) do
16
+ ref_target.public_send(name) if ref_target.respond_to?(name)
17
+ end
18
+ end
19
+
20
+ def load_data
21
+ siblings = raw_schema.reject { |key, _| key == '$ref' }
22
+ @raw_schema = raw_schema.slice('$ref')
23
+ @ref_target = Reference.new("#{object_reference}/$ref", self, root, raw_schema)
24
+ @ref_siblings = Schema.new(object_reference, self, root, siblings)
25
+ @_openapi_all_child_objects = { @ref_target.object_reference => @ref_target, "#{object_reference}/siblings" => @ref_siblings }
26
+ nil
27
+ end
28
+
29
+ def expand_reference(root, validate_references)
30
+ new_object = expand_object(root, @ref_target, validate_references)
31
+ if new_object
32
+ _update_child_object(@ref_target, new_object)
33
+ @ref_target = new_object
34
+ end
35
+ @ref_siblings.expand_reference(root, validate_references)
36
+ nil
37
+ end
38
+ end
39
+ end
@@ -14,5 +14,6 @@ require_relative 'schemas/responses'
14
14
  require_relative 'schemas/components'
15
15
  require_relative 'schemas/media_type'
16
16
  require_relative 'schemas/schema'
17
+ require_relative 'schemas/schema_with_ref'
17
18
  require_relative 'schemas/header'
18
19
  require_relative 'schemas/info'
@@ -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,25 @@
1
+ module OpenAPIParser
2
+ class SpecValidator
3
+ module Rules
4
+ # `patternProperties` 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 PatternPropertiesIn30 < 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?('patternProperties')
14
+
15
+ violations << violation(
16
+ path: schema.object_reference,
17
+ message: '`patternProperties` 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
+ # `propertyNames` 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 PropertyNamesIn30 < 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?('propertyNames')
14
+
15
+ violations << violation(
16
+ path: schema.object_reference,
17
+ message: '`propertyNames` 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
@@ -18,6 +18,9 @@ require_relative 'spec_validator/rules/dynamic_ref_in_30'
18
18
  require_relative 'spec_validator/rules/dynamic_anchor_in_30'
19
19
  require_relative 'spec_validator/rules/content_schema_in_30'
20
20
  require_relative 'spec_validator/rules/defs_in_30'
21
+ require_relative 'spec_validator/rules/dependent_required_in_30'
22
+ require_relative 'spec_validator/rules/property_names_in_30'
23
+ require_relative 'spec_validator/rules/pattern_properties_in_30'
21
24
  require_relative 'spec_validator/rules/allow_empty_value_deprecation'
22
25
  require_relative 'spec_validator/rules/xml_node_type_before_32'
23
26
  require_relative 'spec_validator/rules/xml_attribute_deprecation'
@@ -100,6 +103,9 @@ module OpenAPIParser
100
103
  Rules::DynamicAnchorIn30,
101
104
  Rules::ContentSchemaIn30,
102
105
  Rules::DefsIn30,
106
+ Rules::DependentRequiredIn30,
107
+ Rules::PropertyNamesIn30,
108
+ Rules::PatternPropertiesIn30,
103
109
  Rules::AllowEmptyValueDeprecation,
104
110
  Rules::XmlNodeTypeBefore32,
105
111
  Rules::XmlAttributeDeprecation,
@@ -1,3 +1,3 @@
1
1
  module OpenAPIParser
2
- VERSION = '2.6.0'.freeze
2
+ VERSION = '2.8.0'.freeze
3
3
  end
@@ -25,6 +25,8 @@ module OpenAPIParser
25
25
  def validate_data: -> Object
26
26
  def validate_schema: (Object value, OpenAPIParser::Schemas::Schema schema, **bot) -> [Object, OpenAPIParser::validate_error]
27
27
  def validate_integer: (Object value, OpenAPIParser::Schemas::Schema schema) -> [Object, OpenAPIParser::validate_error]
28
+ def valid_without_coercion?: (Object value, OpenAPIParser::Schemas::Schema schema) -> bool
29
+ def validate_in_order: (Object value, Array[untyped] steps) -> [Object, OpenAPIParser::validate_error]
28
30
 
29
31
  private
30
32
  def validator: (Object value, OpenAPIParser::Schemas::Schema schema) -> [OpenAPIParser::SchemaValidator::Base, OpenAPIParser::validate_error]
@@ -114,6 +114,18 @@ module OpenAPIParser
114
114
  def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
115
115
  end
116
116
 
117
+ class DependentRequiredIn30 < Rule
118
+ def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
119
+ end
120
+
121
+ class PropertyNamesIn30 < Rule
122
+ def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
123
+ end
124
+
125
+ class PatternPropertiesIn30 < Rule
126
+ def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
127
+ end
128
+
117
129
  class AllowEmptyValueDeprecation < Rule
118
130
  def check: (OpenAPIParser::Schemas::OpenAPI root) -> Array[SpecValidator::SpecViolation]
119
131
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ota42y
@@ -209,6 +209,7 @@ files:
209
209
  - lib/openapi_parser/schemas/response.rb
210
210
  - lib/openapi_parser/schemas/responses.rb
211
211
  - lib/openapi_parser/schemas/schema.rb
212
+ - lib/openapi_parser/schemas/schema_with_ref.rb
212
213
  - lib/openapi_parser/spec_validator.rb
213
214
  - lib/openapi_parser/spec_validator/rule.rb
214
215
  - lib/openapi_parser/spec_validator/rules/additional_operations_before_32.rb
@@ -221,6 +222,7 @@ files:
221
222
  - lib/openapi_parser/spec_validator/rules/cookie_style_before_32.rb
222
223
  - lib/openapi_parser/spec_validator/rules/default_mapping_before_32.rb
223
224
  - lib/openapi_parser/spec_validator/rules/defs_in_30.rb
225
+ - lib/openapi_parser/spec_validator/rules/dependent_required_in_30.rb
224
226
  - lib/openapi_parser/spec_validator/rules/dynamic_anchor_in_30.rb
225
227
  - lib/openapi_parser/spec_validator/rules/dynamic_ref_in_30.rb
226
228
  - lib/openapi_parser/spec_validator/rules/example_singular_deprecation.rb
@@ -231,7 +233,9 @@ files:
231
233
  - lib/openapi_parser/spec_validator/rules/media_types_before_32.rb
232
234
  - lib/openapi_parser/spec_validator/rules/nullable_deprecation.rb
233
235
  - lib/openapi_parser/spec_validator/rules/path_items_in_30.rb
236
+ - lib/openapi_parser/spec_validator/rules/pattern_properties_in_30.rb
234
237
  - lib/openapi_parser/spec_validator/rules/prefix_items_in_30.rb
238
+ - lib/openapi_parser/spec_validator/rules/property_names_in_30.rb
235
239
  - lib/openapi_parser/spec_validator/rules/query_method_before_32.rb
236
240
  - lib/openapi_parser/spec_validator/rules/querystring_before_32.rb
237
241
  - lib/openapi_parser/spec_validator/rules/response_summary_before_32.rb