Support type: "null" for OpenAPI 3.1 schemas (runtime + TypeNullIn30 rule) - #197
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.
|
@takayamaki broadly I think this looks good. I guess my question would be around things like |
|
@geemus Thank you for review! This PR does not change behavior for array-form types like Actually, I have the array-type support ready and opened it as a stacked PR: Let me know if you'd still prefer the snapshot-test approach in this PR instead. |
|
No, that's probably fine. Thanks for the update and sorry for my delay in reviewing. |
Continuing the OpenAPI 3.1 work from #195 / #152.
OpenAPI 3.1 (via JSON Schema) adds
"null"as a primitive type name.In 3.0, nullability was expressed via the
nullablekeyword;type: "null"had no meaning.This PR adds both runtime validation and version-mismatch detection for
type: "null".Runtime validation
The accept side and the reject side live in two different validators,
because of how
SchemaValidator#validatordispatches:Accepting
nilhappens in the existing
NilValidator, which handles all nil values regardless of schema type. It now treatsschema.type == "null"as valid, exactly like the existingnullable: truepath.Rejecting everything else
happens in the new
NullTypeValidator. Since nil values are routed toNilValidatorbefore the type dispatch, any value reaching thewhen 'null'branch is by construction non-nil.So
NullTypeValidatorunconditionally returns a validation error.Without the new branch,
type: "null"would fall through toUnspecifiedTypeValidatorand accept any value.SpecValidator rule
TypeNullIn30walks all schemas and reports a violation when a 3.0 document usestype: "null".Array-form types like
["string", "null"]are covered by a separate rule in a later PR.