Skip to content

Commit 62967c7

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 62967c7

5 files changed

Lines changed: 2033 additions & 1 deletion

File tree

‎docs/validation.rst‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ 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
165+
`official OpenAPI 3.0 Schema Object schema
166+
<https://spec.openapis.org/oas/3.0/schema/2024-10-18>`__, which allows only the
167+
fields listed by the specification and ``x-`` extensions. JSON Schema keywords
168+
that OpenAPI 3.0 does not support, such as ``type`` arrays, ``definitions``,
169+
``id``, ``$schema``, ``patternProperties`` or ``const``, raise
170+
``SchemaError``:
171+
172+
.. code-block:: python
173+
174+
validate("John", {"type": ["string", "null"]}, cls=OAS30Validator)
175+
# SchemaError: ['string', 'null'] is not of type 'string'
176+
177+
Use ``nullable: true`` instead of a ``null`` type. An object with ``$ref`` is
178+
a Reference Object: any other keys next to ``$ref`` are ignored.
179+
180+
The schema passed in is checked as a Schema Object, so it cannot carry other
181+
keys such as ``definitions``, ``components`` or a whole OpenAPI document.
182+
Provide referenced schemas through ``registry=...`` (see :doc:`references`).
183+
164184
Default dialect resolution
165185
--------------------------
166186

‎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)