Skip to content

Commit 60b074f

Browse files
committed
Check OpenAPI 3.0 schemas against the OAS 3.0 Schema Object schema
OpenAPI 3.0 schemas were checked against the JSON Schema Draft 4 metaschema, which accepts keywords OpenAPI 3.0 does not support (type arrays, definitions, id, $schema, patternProperties, const, unknown keywords, ...). Vendor the official OAS 3.0 schema (2024-10-18) unmodified and check OAS 3.0 schemas against its Schema, Reference, Discriminator, XML and ExternalDocumentation definitions. Schema or Reference is chosen by the presence of $ref instead of the official oneOf, so errors point at the offending keyword and keys next to $ref are ignored as the specification requires. Apply the check to OAS30Validator and the Strict, Read and Write variants; the variants previously used jsonschema's default Draft 4 check. This is a breaking change: schemas using unsupported keywords, and roots carrying non-Schema Object keys such as components or a whole OpenAPI document, now raise SchemaError. Referenced schemas can be provided via registry, or the check skipped with check_schema=False.
1 parent 867fea5 commit 60b074f

5 files changed

Lines changed: 2020 additions & 1 deletion

File tree

‎docs/validation.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` inste
161161
162162
validate({"name": "John", "age": None}, schema, cls=OAS30Validator)
163163
164+
OpenAPI 3.0 schemas are checked against the `official Schema Object schema
165+
<https://spec.openapis.org/oas/3.0/schema/2024-10-18>`__: only fields listed by
166+
the specification and ``x-`` extensions are allowed, and keys next to ``$ref``
167+
are ignored. Other keywords, such as ``type`` arrays, ``definitions`` or
168+
``components``, raise ``SchemaError``; provide referenced schemas through
169+
``registry=...`` instead (see :doc:`references`).
170+
164171
Default dialect resolution
165172
--------------------------
166173

‎openapi_schema_validator/_dialects.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
)
88

99
__all__ = [
10+
"OAS30_SCHEMA_ID",
1011
"OAS31_BASE_DIALECT_ID",
1112
"OAS31_BASE_DIALECT_METASCHEMA",
1213
"OAS32_BASE_DIALECT_ID",
1314
"OAS32_BASE_DIALECT_METASCHEMA",
1415
"register_openapi_dialect",
1516
]
1617

18+
OAS30_SCHEMA_ID = "https://spec.openapis.org/oas/3.0/schema/2024-10-18"
1719
OAS31_BASE_DIALECT_ID = "https://spec.openapis.org/oas/3.1/dialect/base"
1820
OAS31_BASE_DIALECT_METASCHEMA = OPENAPI_SPECIFICATIONS.contents(
1921
OAS31_BASE_DIALECT_ID,

0 commit comments

Comments
 (0)