Skip to content

Fix ignored keywords in schemas without type - #223

Merged
geemus merged 13 commits into
ota42y:masterfrom
takayamaki:untyped-schema-keywords
Oct 27, 2026
Merged

geemus merged 13 commits into
ota42y:masterfrom
takayamaki:untyped-schema-keywords

Conversation

@takayamaki

@takayamaki takayamaki commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Part of #215 (item 9).

A schema without type skipped every keyword.
For example, this schema accepted { "count": "x" } and {}:

required: [count]
properties:
  count:
    type: integer

In JSON Schema, which OpenAPI 3.1 follows, a keyword applies when the value has the type the keyword is for.
type does not need to be set.
So properties and required apply to an object, and maxLength applies to a string.

Changes

  • When type is not set, the validator picks the type from the value, and then validates the value in the same way as a schema with that type.
    It reuses pick_array_type, renamed to pick_type, which already does this for array-form type.
  • A keyword for another type is still ignored.
    For example, properties does not reject a string.
  • A value of any other class, like a Date, is still accepted without checks.

I checked the same cases with three well-known JSON Schema validators.
They all give the same results, for both Draft 2020-12 and Draft 4:
ajv 8.20.0, python-jsonschema 4.26.0 and json_schemer 2.5.0.

schema value the three validators before after
type: object with properties 1 invalid invalid invalid
properties without type 1 valid valid valid
properties without type { "count": 1 } valid valid valid
properties without type { "count": "x" } invalid valid invalid

properties here is { count: { type: integer } }.
Only the last row differed from the three validators before this change.

I did not check the declared version.
The OpenAPI 3.0 Schema Object also follows JSON Schema here, so the same fix is right for 3.0 documents.

Compatibility

This makes validation stricter.
A value that breaks a keyword in a schema without type used to pass, and now fails.
I added a note in the CHANGELOG.
I did not add a config option, but I can add one if you prefer.

The committee test suite passes with this branch.

null for a schema without type is not changed here.
It is still rejected, as described in #215 item 5.

@takayamaki
takayamaki marked this pull request as ready for review October 23, 2026 04:09
takayamaki and others added 12 commits October 24, 2026 21:04
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A schema without type skipped every keyword, so properties did not check the value.
JSON Schema, which OpenAPI 3.1 follows, applies properties to an object whether or not type is set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
These already pass with the previous change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also pin enum for a string, which already passes with the previous change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…type

These already pass with the previous changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It now also picks the type for a schema without type, not only from array-form type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@takayamaki
takayamaki force-pushed the untyped-schema-keywords branch from 159195a to 8474b3b Compare October 24, 2026 12:15

@geemus geemus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@geemus geemus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@geemus
geemus merged commit 13a76ca into ota42y:master Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants