Support array-form type for OpenAPI 3.1 schemas (runtime + TypeArrayIn30 rule) - #198
Draft
takayamaki wants to merge 8 commits into
Draft
Support array-form type for OpenAPI 3.1 schemas (runtime + TypeArrayIn30 rule)#198takayamaki wants to merge 8 commits into
takayamaki wants to merge 8 commits into
Conversation
- nil_validator now passes when schema declares `type: \"null\"` even without `nullable: true` - NullTypeValidator handles non-nil values against `type: \"null\"` with a ValidateError so 3.1 enforces the singleton-null semantic - Rules::TypeNullIn30 flags `type: \"null\"` on 3.0 documents
type: "null" on a 3.0 document warns and raises (3.0 has no such primitive); the same type on a 3.1 document stays clean.
- Rules::TypeArrayIn30 flags array-form `type` on 3.0 documents - nil_validator also passes when `type` is an Array containing "null" - schema_validator picks the primitive in the Array that matches the value's class and dispatches to the existing per-primitive validator; values that match no primitive fall to NullTypeValidator which is now general-purpose for "no applicable type"
An Array-valued type on a 3.0 document warns and raises (3.0 expects a single type String); the same form on a 3.1 document stays clean.
The validator unconditionally reports a type mismatch and now serves two dispatch paths: non-nil values against `type: "null"`, and values matching none of an array-form type's primitives. Name it for what it does rather than its original null-only use case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Continuing the OpenAPI 3.1 work from #197 / #152.
The diff will shrink to the array-form changes once #197 is merged and this branch is rebased.
takayamaki/openapi_parser@pr8-type-null-in-31...pr9-type-array-in-31-stacked
Review #197 first.
OpenAPI 3.1 (via JSON Schema) allows
typeto be an array of primitive type names, e.g.type: ["string", "null"].In 3.0,
typewas always a single string.This PR adds both runtime validation and version-mismatch detection for the array form.
Runtime validation
Dispatch works in two steps inside
SchemaValidator#validator:Accepting
nilAs with
type: "null"(#197), nil values are routed toNilValidatorbefore the type dispatch.It now also accepts nil when the array form includes
"null".Dispatching non-nil values
pick_array_typematches the value's Ruby class against each declared primitive(
String→"string",Integer→"integer",Numeric→"number", …)and dispatches to the corresponding single-type validator, so all existing per-type
validation (format, enum, coercion, …) applies unchanged.
If the value matches none of the declared types, the schema-level type mismatch is
reported via the validator introduced in #197 for non-nil values against
type: "null".Since it now serves both dispatch paths as an unconditional type-mismatch reporter,
this PR renames it from
NullTypeValidatortoTypeMismatchValidator.Without this, array-form types would fall through to
UnspecifiedTypeValidatorand accept any value.SpecValidator rule
TypeArrayIn30walks all schemas and reports a violation when a 3.0 document uses the array form.