Skip to content

Commit e75701b

Browse files
committed
Add OpenAPI 3.2 support
1 parent 625bebd commit e75701b

File tree

9 files changed

+47
-2
lines changed

9 files changed

+47
-2
lines changed

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Use this as the default operating playbook when making changes.
1717
## Source Layout
1818

1919
- `openapi_schema_validator/__init__.py`: public exports + package metadata.
20-
- `openapi_schema_validator/validators.py`: validator class setup for OAS 3.0/3.1.
20+
- `openapi_schema_validator/validators.py`: validator class setup for OAS 3.0/3.1/3.2.
2121
- `openapi_schema_validator/_keywords.py`: custom keyword handlers and ValidationError generation.
2222
- `openapi_schema_validator/_format.py`: format checker functions and registrations.
2323
- `openapi_schema_validator/_types.py`: custom type checker setup.
@@ -128,7 +128,7 @@ Run commands from repository root.
128128
### Behavioral constraints to preserve
129129

130130
- Do not mutate incoming schema objects in helper APIs.
131-
- Maintain compatibility for both OAS 3.0 and OAS 3.1 validators.
131+
- Maintain compatibility for OAS 3.0, OAS 3.1, and OAS 3.2 validators.
132132
- Keep existing read/write behavior around `readOnly` and `writeOnly`.
133133
- Keep format and type checker semantics aligned with current tests.
134134

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Openapi-schema-validator is a Python library that validates schema against:
2222

2323
* `OpenAPI Schema Specification v3.0 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject>`__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 <http://json-schema.org/>`__.
2424
* `OpenAPI Schema Specification v3.1 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schemaObject>`__ which is an extended superset of the `JSON Schema Specification Draft 2020-12 <http://json-schema.org/>`__.
25+
* `OpenAPI Schema Specification v3.2 <https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#schemaObject>`__ which uses the same JSON Schema dialect as v3.1.
2526

2627

2728
Documentation

docs/format.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ You can check format for predefined OAS primitive types
1212
Traceback (most recent call last):
1313
...
1414
ValidationError: '-12' is not a 'date'
15+
16+
For OpenAPI 3.2, use ``oas32_format_checker`` (behaves identically to ``oas31_format_checker``, since 3.2 uses the same JSON Schema dialect).

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Openapi-schema-validator is a Python library that validates schema against:
1414

1515
* `OpenAPI Schema Specification v3.0 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject>`__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 <http://json-schema.org/>`__.
1616
* `OpenAPI Schema Specification v3.1 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schemaObject>`__ which is an extended superset of the `JSON Schema Specification Draft 2020-12 <http://json-schema.org/>`__.
17+
* `OpenAPI Schema Specification v3.2 <https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#schemaObject>`__ which uses the same JSON Schema dialect as v3.1.
1718

1819
Installation
1920
------------

docs/validation.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ if you want to disambiguate the expected schema version, import and use ``OAS31V
6767
6868
validate({"name": "John", "age": 23}, schema, cls=OAS31Validator)
6969
70+
For OpenAPI 3.2, use ``OAS32Validator`` (behaves identically to ``OAS31Validator``, since 3.2 uses the same JSON Schema dialect).
71+
7072
In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` instead of ``OAS31Validator``.
7173

7274
.. code-block:: python

openapi_schema_validator/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from openapi_schema_validator._format import oas30_format_checker
22
from openapi_schema_validator._format import oas30_strict_format_checker
33
from openapi_schema_validator._format import oas31_format_checker
4+
from openapi_schema_validator._format import oas32_format_checker
45
from openapi_schema_validator.shortcuts import validate
56
from openapi_schema_validator.validators import OAS30ReadValidator
67
from openapi_schema_validator.validators import OAS30StrictValidator
78
from openapi_schema_validator.validators import OAS30Validator
89
from openapi_schema_validator.validators import OAS30WriteValidator
910
from openapi_schema_validator.validators import OAS31Validator
11+
from openapi_schema_validator.validators import OAS32Validator
1012

1113
__author__ = "Artur Maciag"
1214
__email__ = "maciag.artur@gmail.com"
@@ -24,4 +26,6 @@
2426
"oas30_strict_format_checker",
2527
"OAS31Validator",
2628
"oas31_format_checker",
29+
"OAS32Validator",
30+
"oas32_format_checker",
2731
]

openapi_schema_validator/_format.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ def is_password(instance: object) -> bool:
105105
oas31_format_checker.checks("float")(is_float)
106106
oas31_format_checker.checks("double")(is_double)
107107
oas31_format_checker.checks("password")(is_password)
108+
109+
# OAS 3.2 uses the same format checks as OAS 3.1
110+
oas32_format_checker = oas31_format_checker

openapi_schema_validator/validators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,13 @@ def _oas30_id_of(schema: Any) -> str:
121121
type_checker=oas31_type_checker,
122122
format_checker=oas_format.oas31_format_checker,
123123
)
124+
125+
# OAS 3.2 uses JSON Schema Draft 2020-12 as its base dialect, same as
126+
# OAS 3.1. The OAS-specific vocabulary differs slightly (e.g. xml keyword
127+
# changes), but since xml is not_implemented in the current validators,
128+
# the behavior is equivalent.
129+
OAS32Validator = extend(
130+
OAS31Validator,
131+
{},
132+
format_checker=oas_format.oas32_format_checker,
133+
)

tests/integration/test_validators.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from openapi_schema_validator import OAS30Validator
2020
from openapi_schema_validator import OAS30WriteValidator
2121
from openapi_schema_validator import OAS31Validator
22+
from openapi_schema_validator import OAS32Validator
2223
from openapi_schema_validator import oas30_format_checker
2324
from openapi_schema_validator import oas30_strict_format_checker
2425
from openapi_schema_validator import oas31_format_checker
26+
from openapi_schema_validator import oas32_format_checker
2527

2628

2729
class TestOAS30ValidatorFormatChecker:
@@ -1003,6 +1005,26 @@ def test_array_prefixitems_invalid(self, validator_class, value):
10031005
assert any(error in str(excinfo.value) for error in errors)
10041006

10051007

1008+
class TestOAS32ValidatorValidate(TestOAS31ValidatorValidate):
1009+
"""OAS 3.2 uses the same JSON Schema dialect as 3.1."""
1010+
1011+
@pytest.fixture
1012+
def validator_class(self):
1013+
return OAS32Validator
1014+
1015+
@pytest.fixture
1016+
def format_checker(self):
1017+
return oas32_format_checker
1018+
1019+
def test_validator_is_distinct_from_oas31(self):
1020+
assert OAS32Validator is not OAS31Validator
1021+
1022+
def test_validator_shares_oas31_behavior(self):
1023+
assert (
1024+
OAS32Validator.VALIDATORS == OAS31Validator.VALIDATORS
1025+
)
1026+
1027+
10061028
class TestOAS30StrictValidator:
10071029
"""
10081030
Tests for OAS30StrictValidator which follows OAS spec strictly:

0 commit comments

Comments
 (0)