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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e570c33d05a24e9b11a3f5b86f5321f895088a672d404df68f3d14b4d3f9fb8f
4
- data.tar.gz: fadcedd0e7b7c5ce057f2b0fa36f313218f151ae524dd2290c1242fbdfca02da
3
+ metadata.gz: 6332e2e24a629533ff6ae7159cff7b51db53a2e451bd527743610122ed3bb994
4
+ data.tar.gz: 88b477bdea9749ebe10b0bf7214529152cbed08e53290ea3657ca17e6d1970d6
5
5
  SHA512:
6
- metadata.gz: 680325ae11c7dce38425d23c89918d3ce5cb768261dbd48f6d21c166ca0d89b2dbf9304297b2f754830f47cdf8d45dd2501371b396fe972957474e171f4b3b49
7
- data.tar.gz: 27b757f70c102671ff1f2891918f5e7abb044796e280027b097214f4f0bbb2ff538b7f7c84d7d1bb7f1fc8b46b63591ad62e37985dab50ebb35c0603b854e92e
6
+ metadata.gz: 69fb1000151935a4636ba3bd98edf9f1e1e221ebea86e4a3cf23348b7452ecf49e2047e9be42322713f438ed56855a20cf52647f0cc08077efc95631839d8e49
7
+ data.tar.gz: 72287aa48edfe2dae9894006b2da9e90c14c5b552bb5cbc598bd166f931ab25e6283167c0b6afda3d7270761487edc83739f73686c3c9e739724049ac23f6d10
@@ -11,13 +11,11 @@ jobs:
11
11
  - ubuntu-latest
12
12
  - macos-latest
13
13
  ruby:
14
- - "2.7"
15
- - "3.0"
16
- - "3.1"
17
14
  - "3.2"
18
15
  - "3.3"
19
16
  - "3.4"
20
17
  - "4.0"
18
+ - "4.1"
21
19
  - ruby-head
22
20
  runs-on: ${{ matrix.os }}
23
21
  steps:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 2.7.0 (2026-11-06)
4
+ * 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
5
+ * 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
6
+ * return the coerced value from `allOf` and `oneOf` when `coerce_value` is on. They returned the original value before
7
+ * 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
8
+ * add `SpecValidator` rule `DependentRequiredIn30`: detect `dependentRequired` in 3.0 documents (3.1 addition)
9
+
10
+ ## 2.6.0 (2026-11-04)
11
+ * 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
12
+ * 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
13
+ * add OpenAPI 3.2 `SpecValidator` rules
14
+ * `CookieStyleBefore32`: detect Parameter `style: cookie` usage in pre-3.2 documents (3.2 addition)
15
+ * `DefaultMappingBefore32`: detect Discriminator `defaultMapping` usage in pre-3.2 documents (3.2 addition)
16
+ * `MediaTypesBefore32`: detect `components.mediaTypes` usage in pre-3.2 documents (3.2 addition)
17
+ * `XmlNodeTypeBefore32`: detect XML Object `nodeType` usage in pre-3.2 documents (3.2 addition)
18
+ * `XmlAttributeDeprecation` / `XmlWrappedDeprecation`: detect XML Object `attribute: true` / `wrapped: true` in 3.2 documents (deprecated in 3.2 in favor of `nodeType`)
19
+ * `QueryMethodBefore32`: detect Path Item `query` usage in pre-3.2 documents (3.2 addition)
20
+ * `AdditionalOperationsBefore32`: detect Path Item `additionalOperations` usage in pre-3.2 documents (3.2 addition)
21
+ * `ResponseSummaryBefore32`: detect Response Object `summary` usage in pre-3.2 documents (3.2 addition)
22
+ * `TagFieldsBefore32`: detect Tag Object `summary` / `parent` / `kind` usage in pre-3.2 documents (3.2 additions)
23
+ * `ServerNameBefore32`: detect Server Object `name` usage in pre-3.2 documents (3.2 addition)
24
+ * `AllowEmptyValueDeprecation`: detect Parameter `allowEmptyValue` usage in 3.2 documents (deprecated in 3.2)
25
+ * `ExampleValueFieldsBefore32`: detect Example Object `dataValue` / `serializedValue` usage in pre-3.2 documents (3.2 additions)
26
+ * `SecuritySchemeFieldsBefore32`: detect Security Scheme `deprecated` / `oauth2MetadataUrl` and the `deviceAuthorization` OAuth flow in pre-3.2 documents (3.2 additions)
27
+ * `StreamingFieldsBefore32`: detect Media Type `itemSchema` / `itemEncoding` / `prefixEncoding` and nested Encoding Object `encoding` / `itemEncoding` / `prefixEncoding` usage in pre-3.2 documents (3.2 additions)
28
+ * `QuerystringBefore32`: detect Parameter `in: querystring` usage in pre-3.2 documents (3.2 addition; not validated at runtime)
29
+ * return nil from `request_operation` for a document without `paths` (3.1 allows a document with only `webhooks`), instead of raising `NoMethodError`
30
+ * support `$defs` (OpenAPI 3.1) in Schema Objects so `$ref`s into it, such as `#/components/schemas/Pet/$defs/Id`, resolve
31
+ * add `SpecValidator` rule `DefsIn30`: detect `$defs` in 3.0 documents (3.1 addition)
32
+ * validate a value against a schema without `type`: keywords such as `properties`, `required`, `items`, `maxLength`, `enum` and `minimum` now apply when the value has the type the keyword is for, as in JSON Schema. They were all skipped before, so a value that breaks them may now fail validation
33
+ * support Discriminator `defaultMapping` (OpenAPI 3.2): fallback schema when the discriminator property is absent or its value has no explicit or implicit mapping; honored in 3.2 documents
34
+ * add `allow_3_2_features` config to apply OpenAPI 3.2 runtime behavior to documents that declare an earlier version (default `false`: pre-3.2 documents behave as before)
35
+ * accept `null` for a schema without `type`, as in JSON Schema. `enum` still applies, so `null` must be listed there when `enum` is set. A schema without `type` used to reject `null` with `NotNullError`, so a value that failed before may now pass
36
+ * accept a Float with no fractional part, such as `1.0`, for `type: integer` in OpenAPI 3.1 documents, as in JSON Schema. OpenAPI 3.0 defines an integer as a number without a fraction, so 3.0 documents still reject it. In 3.1 documents a value that failed before may now pass, and `oneOf` with both `integer` and `number` now rejects `1.0` because it matches both
37
+ * support `components.mediaTypes` (OpenAPI 3.2), resolving `$ref`s in request body and response `content` in 3.2 documents; an unresolved one accepts any body unless `strict_reference_validation` is set
38
+ * support the `query` HTTP method and `additionalOperations` (OpenAPI 3.2) on Path Items, including request-operation lookup, in 3.2 documents
39
+ * match HTTP methods case-insensitively in request-operation lookup in 3.2 documents (`request_operation("GET", path)` still returns nil before 3.2)
40
+ * support Response Object `summary` (OpenAPI 3.2) in the parse layer
41
+ * accept a file object for array-form `type` with `format: binary` (3.1) when `coerce_value` is off, the same way as a single `type: string`
42
+ * support `itemSchema` (OpenAPI 3.2) in the parse layer; it is not yet used to validate sequential media type bodies
43
+
3
44
  ## 2.5.0 (2026-10-21)
4
45
  * add OpenAPI 3.2 `SpecValidator` rules
5
46
  * `SelfBefore32`: detect `$self` usage in pre-3.2 documents (3.2 addition)
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Yard Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/openapi_parser)
5
5
  [![Inch CI](https://inch-ci.org/github/ota42y/openapi_parser.svg?branch=master)](https://inch-ci.org/github/ota42y/openapi_parser)
6
6
 
7
- This is OpenAPI3 parser and validator.
7
+ This is OpenAPI3 parser and validator (OpenAPI 3.0, 3.1 and 3.2).
8
8
 
9
9
  ## Usage
10
10
 
@@ -89,6 +89,49 @@ options = {
89
89
  OpenAPIParser.parse(yaml_file, options)
90
90
  ```
91
91
 
92
+ ### Supported OpenAPI versions
93
+
94
+ The parser accepts OpenAPI 3.0, 3.1, and 3.2 documents. Parsing and runtime request/response validation are version-permissive: supported keywords are honored regardless of the document's declared `openapi:` version (for example, both `nullable` and `type: [..., "null"]` work everywhere).
95
+
96
+ The exception is 3.2 behavior that would change how an existing 3.0 or 3.1 document validates. It applies only to documents that declare 3.2 or later:
97
+
98
+ - `$ref`s in request body and response `content` (e.g. into `components.mediaTypes`)
99
+ - the `query` operation, `additionalOperations`, and case-insensitive method matching in `request_operation`
100
+ - Discriminator `defaultMapping`
101
+
102
+ A file loaded through a `$ref` that declares no `openapi:` version follows the document that loaded it. To opt earlier documents in, pass `allow_3_2_features: true`:
103
+
104
+ ```ruby
105
+ OpenAPIParser.parse(schema, allow_3_2_features: true)
106
+ ```
107
+
108
+ Some 3.1 and 3.2 features are parsed or detected but do not affect runtime validation:
109
+
110
+ - `contentEncoding`, `contentMediaType`, and `contentSchema` (3.1) are parsed but not validated
111
+ - `$dynamicRef` and `$dynamicAnchor` (3.1) are not resolved
112
+ - `$self` (3.2) is parsed (`OpenAPI#self_uri`) but not used as the base URI for `$ref` resolution
113
+ - `itemSchema` (3.2) is parsed but not used to validate sequential media type bodies (SSE, JSON Lines, JSON Sequences)
114
+ - parameters with `in: querystring` (3.2) are not validated
115
+ - Tag, Server, Example, XML, and Security Scheme objects are not modeled; they are only checked by the `SpecValidator`
116
+
117
+ Version-specific correctness is checked separately by the `SpecValidator`, driven by the `strict_specification_version` option:
118
+
119
+ - `:silent` (default) — no version checks
120
+ - `:warn` — print a warning for each mismatch (e.g. a 3.1-only keyword in a 3.0 document)
121
+ - `:raise` — raise `OpenAPIParser::SpecViolationError` listing all mismatches
122
+
123
+ ```ruby
124
+ OpenAPIParser.parse(schema, strict_specification_version: :raise)
125
+ # => raises OpenAPIParser::SpecViolationError if e.g. a 3.0 document uses `prefixItems`
126
+ ```
127
+
128
+ Detected mismatches:
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`, `dependentRequired`, boolean schemas
131
+ - the 3.0 Boolean vs 3.1 numeric forms of `exclusiveMinimum` / `exclusiveMaximum`
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
+ - 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
134
+
92
135
  ## ToDo
93
136
  - correct schema checker
94
137
  - more detailed validator
@@ -1,4 +1,22 @@
1
1
  module OpenAPIParser::MediaTypeSelectable
2
+ # `content` accepts $refs only where 3.2 runtime behavior applies; before
3
+ # 3.2 a `$ref` there is parsed as a MediaType with no schema, as it always was
4
+ CONTENT_REFERENCE = ->(target) { target.root.use_3_2_features? }
5
+
6
+ # A `content` $ref left unresolved (strict_reference_validation off) falls
7
+ # back to a schemaless MediaType instead of staying a Reference
8
+ def expand_reference(root, validate_references)
9
+ super
10
+
11
+ content&.each do |key, media_type|
12
+ next unless media_type.kind_of?(OpenAPIParser::Schemas::Reference)
13
+
14
+ fallback = OpenAPIParser::Schemas::MediaType.new(media_type.object_reference, self, root, media_type.raw_schema)
15
+ _update_child_object(media_type, fallback)
16
+ content[key] = fallback
17
+ end
18
+ end
19
+
2
20
  private
3
21
 
4
22
  # select media type by content_type (consider wild card definition)
@@ -26,12 +26,17 @@ class OpenAPIParser::SchemaLoader::Creator < OpenAPIParser::SchemaLoader::Base
26
26
  check_object_schema?(check_schema) && !check_schema['$ref'].nil?
27
27
  end
28
28
 
29
+ # `reference:` is a Boolean, or a Proc taking the owning object
30
+ def allow_reference?(target_object)
31
+ @allow_reference.respond_to?(:call) ? @allow_reference.call(target_object) : @allow_reference
32
+ end
33
+
29
34
  def check_object_schema?(check_schema)
30
35
  check_schema.kind_of?(::Hash)
31
36
  end
32
37
 
33
38
  def escape_reference(str)
34
- str.to_s.gsub('/', '~1')
39
+ str.to_s.gsub('~', '~0').gsub('/', '~1')
35
40
  end
36
41
 
37
42
  def build_openapi_object_from_option(target_object, ref, schema)
@@ -39,7 +44,7 @@ class OpenAPIParser::SchemaLoader::Creator < OpenAPIParser::SchemaLoader::Base
39
44
 
40
45
  if @allow_data_type && !check_object_schema?(schema)
41
46
  schema
42
- elsif @allow_reference && check_reference_schema?(schema)
47
+ elsif allow_reference?(target_object) && check_reference_schema?(schema)
43
48
  OpenAPIParser::Schemas::Reference.new(ref, target_object, target_object.root, schema)
44
49
  else
45
50
  @klass.new(ref, target_object, target_object.root, schema)
@@ -21,7 +21,7 @@ class OpenAPIParser::SchemaLoader::HashBodyLoader < OpenAPIParser::SchemaLoader:
21
21
  # for responses and paths object
22
22
  def create_hash_body_objects(target_object, raw_schema)
23
23
  object_list = raw_schema.reject { |k, _| reject_keys.include?(k) }.map do |child_name, child_schema|
24
- ref = build_object_reference_from_base(target_object.object_reference, escape_reference(child_name))
24
+ ref = build_object_reference_from_base(target_object.object_reference, child_name)
25
25
  [
26
26
  child_name.to_s, # support string key only in OpenAPI3
27
27
  build_openapi_object_from_option(target_object, ref, child_schema),
@@ -15,6 +15,11 @@ class OpenAPIParser::Config
15
15
  @config.fetch(:allow_empty_date_and_datetime, false)
16
16
  end
17
17
 
18
+ # apply OpenAPI 3.2 runtime behavior to documents that declare an earlier version
19
+ def allow_3_2_features
20
+ @config.fetch(:allow_3_2_features, false)
21
+ end
22
+
18
23
  def datetime_coerce_class
19
24
  @config[:datetime_coerce_class]
20
25
  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)
@@ -112,6 +124,18 @@ module OpenAPIParser
112
124
  end
113
125
  end
114
126
 
127
+ # raised for a value that is valid against the `not` schema
128
+ class NotSchemaMatched < OpenAPIError
129
+ def initialize(value, reference)
130
+ super(reference)
131
+ @value = value
132
+ end
133
+
134
+ def message
135
+ "#{@value.inspect} must not be valid against #{@reference}"
136
+ end
137
+ end
138
+
115
139
  class NotEnumInclude < OpenAPIError
116
140
  def initialize(value, reference)
117
141
  super(reference)
@@ -123,6 +147,18 @@ module OpenAPIParser
123
147
  end
124
148
  end
125
149
 
150
+ class NotMultipleOf < OpenAPIError
151
+ def initialize(value, multiple_of, reference)
152
+ super(reference)
153
+ @value = value
154
+ @multiple_of = multiple_of
155
+ end
156
+
157
+ def message
158
+ "#{@value.inspect} isn't a multiple of #{@multiple_of.inspect} in #{@reference}"
159
+ end
160
+ end
161
+
126
162
  class LessThanMinimum < OpenAPIError
127
163
  def initialize(value, reference)
128
164
  super(reference)
@@ -10,17 +10,24 @@ 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
26
33
  nested_additional_properties = true if s.additional_properties
@@ -28,8 +35,6 @@ class OpenAPIParser::SchemaValidator
28
35
  # If this is not allOf having array of objects inside, but e.g. having another anyOf/oneOf nested
29
36
  remaining_keys.clear
30
37
  end
31
-
32
- return [nil, err] if err
33
38
  end
34
39
 
35
40
  # If there are nested additionalProperties, we allow not defined extra properties and lean on the specific
@@ -38,7 +43,7 @@ class OpenAPIParser::SchemaValidator
38
43
  return [nil, OpenAPIParser::NotExistPropertyDefinition.new(remaining_keys, schema.object_reference)]
39
44
  end
40
45
 
41
- [value, nil]
46
+ [coerced, nil]
42
47
  end
43
48
  end
44
49
  end
@@ -7,7 +7,7 @@ class OpenAPIParser::SchemaValidator
7
7
  return [value, nil]
8
8
  end
9
9
  if schema.discriminator
10
- return validate_discriminator_schema(schema.discriminator, value)
10
+ return validate_discriminator_schema(schema.discriminator, value, candidate_schemas: schema.any_of)
11
11
  end
12
12
 
13
13
  # in all schema return error (=true) not any of data
@@ -12,19 +12,32 @@ class OpenAPIParser::SchemaValidator
12
12
  raise 'need implement'
13
13
  end
14
14
 
15
- def validate_discriminator_schema(discriminator, value, parent_discriminator_schemas: [])
15
+ def validate_discriminator_schema(discriminator, value, candidate_schemas: nil, parent_discriminator_schemas: [])
16
16
  property_name = discriminator.property_name
17
- if property_name.nil? || !value.key?(property_name)
18
- return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
17
+ if property_name && value.key?(property_name)
18
+ mapping_key = value[property_name]
19
+ explicit_target = discriminator.mapping&.[](mapping_key)
20
+ if explicit_target
21
+ mapping_target = explicit_target
22
+ resolved_schema = discriminator.root.find_object(mapping_target)
23
+ else
24
+ mapping_target = "#/components/schemas/#{mapping_key}"
25
+ # Implicit values are component names, not reference paths.
26
+ resolved_schema = discriminator.root.components&.schemas&.[](mapping_key.to_s)
27
+ resolved_schema = nil if candidate_schemas && resolved_schema && !candidate_schemas.include?(resolved_schema)
28
+ end
19
29
  end
20
- mapping_key = value[property_name]
21
30
 
22
- # it's allowed to have discriminator without mapping, then we need to lookup discriminator.property_name
23
- # but the format is not the full path, just model name in the components
24
- mapping_target = discriminator.mapping&.[](mapping_key) || "#/components/schemas/#{mapping_key}"
31
+ # defaultMapping (3.2) applies when the property is absent or its value
32
+ # has neither an explicit mapping nor an implicit schema match
33
+ if resolved_schema.nil? && explicit_target.nil? && (default_target = default_mapping_target(discriminator))
34
+ mapping_target = default_target
35
+ resolved_schema = discriminator.root.find_object(default_target)
36
+ end
25
37
 
26
- # Find object does O(n) search at worst, then caches the result, so this is ok for repeated search
27
- resolved_schema = discriminator.root.find_object(mapping_target)
38
+ unless mapping_target
39
+ return [nil, OpenAPIParser::NotExistDiscriminatorPropertyName.new(discriminator.property_name, value, discriminator.object_reference)]
40
+ end
28
41
 
29
42
  unless resolved_schema
30
43
  return [nil, OpenAPIParser::NotExistDiscriminatorMappedSchema.new(mapping_target, discriminator.object_reference)]
@@ -35,5 +48,18 @@ class OpenAPIParser::SchemaValidator
35
48
  **{discriminator_property_name: discriminator.property_name, parent_discriminator_schemas: parent_discriminator_schemas}
36
49
  )
37
50
  end
51
+
52
+ private
53
+
54
+ # defaultMapping holds a schema name or a URI reference; only
55
+ # same-document references resolve. Ignored where 3.2 behavior doesn't apply
56
+ def default_mapping_target(discriminator)
57
+ return nil unless discriminator.root.use_3_2_features?
58
+
59
+ target = discriminator.default_mapping
60
+ return nil unless target.is_a?(String)
61
+
62
+ target.start_with?('#') || target.include?('/') ? target : "#/components/schemas/#{target}"
63
+ end
38
64
  end
39
65
  end
@@ -2,6 +2,7 @@ class OpenAPIParser::SchemaValidator
2
2
  class FloatValidator < Base
3
3
  include ::OpenAPIParser::SchemaValidator::Enumable
4
4
  include ::OpenAPIParser::SchemaValidator::MinimumMaximum
5
+ include ::OpenAPIParser::SchemaValidator::MultipleOf
5
6
 
6
7
  # validate float value by schema
7
8
  # @param [Object] value
@@ -22,6 +23,9 @@ class OpenAPIParser::SchemaValidator
22
23
  value, err = check_enum_include(value, schema)
23
24
  return [nil, err] if err
24
25
 
26
+ value, err = check_multiple_of(value, schema)
27
+ return [nil, err] if err
28
+
25
29
  check_minimum_maximum(value, schema)
26
30
  end
27
31
 
@@ -2,6 +2,7 @@ class OpenAPIParser::SchemaValidator
2
2
  class IntegerValidator < Base
3
3
  include ::OpenAPIParser::SchemaValidator::Enumable
4
4
  include ::OpenAPIParser::SchemaValidator::MinimumMaximum
5
+ include ::OpenAPIParser::SchemaValidator::MultipleOf
5
6
 
6
7
  # validate integer value by schema
7
8
  # @param [Object] value
@@ -9,14 +10,29 @@ class OpenAPIParser::SchemaValidator
9
10
  def coerce_and_validate(value, schema, **_keyword_args)
10
11
  value = coerce(value) if @coerce_value
11
12
 
12
- return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Integer)
13
+ return OpenAPIParser::ValidateError.build_error_result(value, schema) unless integer?(value, schema)
13
14
 
14
15
  value, err = check_enum_include(value, schema)
15
16
  return [nil, err] if err
16
17
 
18
+ value, err = check_multiple_of(value, schema)
19
+ return [nil, err] if err
20
+
17
21
  check_minimum_maximum(value, schema)
18
22
  end
19
23
 
24
+ # 3.1 follows JSON Schema, where 1.0 is an integer. 3.0 defines an
25
+ # integer as a number without a fraction, so 1.0 is not one there.
26
+ # @param [Object] value
27
+ # @param [OpenAPIParser::Schemas::Schema] schema
28
+ # @return [Boolean]
29
+ def integer?(value, schema)
30
+ return true if value.kind_of?(Integer)
31
+ return false unless value.kind_of?(Float) && value.finite? && value == value.floor
32
+
33
+ schema.root.use_3_1_features?
34
+ end
35
+
20
36
  private
21
37
 
22
38
  def coerce(value)
@@ -0,0 +1,26 @@
1
+ class OpenAPIParser::SchemaValidator
2
+ module MultipleOf
3
+ # check multipleOf by schema
4
+ # @param [Numeric] value
5
+ # @param [OpenAPIParser::Schemas::Schema] schema
6
+ def check_multiple_of(value, schema)
7
+ multiple_of = schema.multipleOf
8
+ # multipleOf MUST be a number strictly greater than 0; ignore an invalid one.
9
+ return [value, nil] unless multiple_of.is_a?(Numeric) && multiple_of.positive?
10
+ return [value, nil] if (exact(value) % exact(multiple_of)).zero?
11
+
12
+ [nil, OpenAPIParser::NotMultipleOf.new(value, multiple_of, schema.object_reference)]
13
+ end
14
+
15
+ private
16
+
17
+ # A Float such as 0.01 is not exact in binary, so 19.99 % 0.01 is not zero.
18
+ # Both numbers come from decimal text in JSON or YAML, so divide them as
19
+ # the decimals that Float#to_s gives back.
20
+ def exact(number)
21
+ return Rational(number.to_s) if number.is_a?(Float) && number.finite?
22
+
23
+ number
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,7 @@
1
1
  class OpenAPIParser::SchemaValidator
2
2
  class NilValidator < Base
3
+ include ::OpenAPIParser::SchemaValidator::Enumable
4
+
3
5
  # @param [Object] value
4
6
  # @param [OpenAPIParser::Schemas::Schema] schema
5
7
  def coerce_and_validate(value, schema, **_keyword_args)
@@ -8,6 +10,8 @@ class OpenAPIParser::SchemaValidator
8
10
  return [value, nil] if schema.type == 'null'
9
11
  # 3.1: `type: [..., "null"]` lets nil coexist with another primitive.
10
12
  return [value, nil] if schema.type.is_a?(Array) && schema.type.include?('null')
13
+ # A schema without `type` accepts null, as in JSON Schema. `enum` still applies.
14
+ return check_enum_include(value, schema) if schema.type.nil?
11
15
 
12
16
  [nil, OpenAPIParser::NotNullError.new(schema.object_reference)]
13
17
  end
@@ -53,9 +53,30 @@ class OpenAPIParser::SchemaValidator
53
53
  end
54
54
  return [nil, OpenAPIParser::NotExistRequiredKey.new(required_set.to_a, schema.object_reference)] unless required_set.empty?
55
55
 
56
+ dependent_required_error = check_dependent_required(value, schema)
57
+ return [nil, dependent_required_error] if dependent_required_error
58
+
56
59
  value.merge!(coerced_values.to_h) if @coerce_value
57
60
 
58
61
  check_properties_number(value, schema)
59
62
  end
63
+
64
+ private
65
+
66
+ # @return [OpenAPIParser::NotExistDependentRequiredKey, nil]
67
+ def check_dependent_required(value, schema)
68
+ dependent_required = schema.dependent_required
69
+ return unless dependent_required.is_a?(Hash)
70
+
71
+ dependent_required.each do |property, keys|
72
+ next unless value.key?(property) && keys.is_a?(Array)
73
+
74
+ missing_keys = keys.reject { |key| value.key?(key) }
75
+ next if missing_keys.empty?
76
+
77
+ return OpenAPIParser::NotExistDependentRequiredKey.new(property, missing_keys, schema.object_reference)
78
+ end
79
+ nil
80
+ end
60
81
  end
61
82
  end
@@ -7,16 +7,13 @@ class OpenAPIParser::SchemaValidator
7
7
  return [value, nil]
8
8
  end
9
9
  if schema.discriminator
10
- return validate_discriminator_schema(schema.discriminator, value)
10
+ return validate_discriminator_schema(schema.discriminator, value, candidate_schemas: schema.one_of)
11
11
  end
12
12
 
13
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?
17
- end
18
- if result
19
- [value, nil]
14
+ matched = schema.one_of.map { |s| validatable.validate_schema(value, s) }.select { |_coerced, err| err.nil? }
15
+ if matched.one?
16
+ [matched.first.first, nil]
20
17
  else
21
18
  [nil, OpenAPIParser::NotOneOf.new(value, schema.object_reference)]
22
19
  end