Skip to content

Commit b3dfdb7

Browse files
authored
Merge pull request #91 from python-openapi/feature/references-documentation-update
references documentation update
2 parents 43a9448 + 2f5a67e commit b3dfdb7

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

docs/references.rst

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
References
22
==========
33

4-
You can resolve JSON references by passing custon reference resolver
4+
You can resolve JSON Schema references by passing registry
55

66
.. code-block:: python
77
8-
from jsonschema.validators import RefResolver
8+
from openapi_schema_validator import validate
9+
from referencing import Registry, Resource
10+
from referencing.jsonschema import DRAFT202012
911
1012
# A schema with reference
1113
schema = {
@@ -27,28 +29,31 @@ You can resolve JSON references by passing custon reference resolver
2729
"additionalProperties": False,
2830
}
2931
# Referenced schemas
30-
schemas = {
31-
"components": {
32-
"schemas": {
33-
"Name": {
34-
"type": "string"
35-
},
36-
"Age": {
37-
"type": "integer",
38-
"format": "int32",
39-
"minimum": 0,
40-
"nullable": True,
41-
},
42-
"BirthDate": {
43-
"type": "string",
44-
"format": "date",
45-
}
46-
},
47-
},
48-
}
49-
50-
ref_resolver = RefResolver.from_schema(schemas)
32+
# In-schema identifier
33+
name_schema = Resource.from_contents({
34+
"$schema": "https://json-schema.org/draft/2020-12/schema",
35+
"type": "string",
36+
})
37+
# Explicit identifier
38+
age_schema = DRAFT202012.create_resource({
39+
"type": "integer",
40+
"format": "int32",
41+
"minimum": 0,
42+
"maximum": 120,
43+
})
44+
# Default identifier
45+
birth_date_schema = Resource.from_contents({
46+
"type": "string",
47+
"format": "date",
48+
}, default_specification=DRAFT202012)
49+
registry = Registry().with_resources(
50+
[
51+
("urn:name-schema", name_schema),
52+
("urn:age-schema", age_schema),
53+
("urn:birth-date-schema", birth_date_schema),
54+
],
55+
)
5156
52-
validate({"name": "John", "age": 23}, schema, resolver=ref_resolver)
57+
validate({"name": "John", "age": 23}, schema, registry=registry)
5358
54-
For more information about reference resolver see `Resolving JSON References <https://python-jsonschema.readthedocs.io/en/stable/references/>`__
59+
For more information about resolving references see `JSON (Schema) Referencing <https://python-jsonschema.readthedocs.io/en/latest/referencing/>`__

tests/integration/test_validators.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import pytest
44
from jsonschema import ValidationError
5+
from referencing import Registry
6+
from referencing import Resource
7+
from referencing.jsonschema import DRAFT202012
58

69
from openapi_schema_validator import OAS30ReadValidator
710
from openapi_schema_validator import OAS30Validator
@@ -98,6 +101,51 @@ def test_string_invalid(self, validator_class, value):
98101
with pytest.raises(ValidationError):
99102
validator.validate(value)
100103

104+
def test_referencing(self, validator_class):
105+
name_schema = Resource.from_contents(
106+
{
107+
"$schema": "https://json-schema.org/draft/2020-12/schema",
108+
"type": "string",
109+
}
110+
)
111+
age_schema = DRAFT202012.create_resource(
112+
{
113+
"type": "integer",
114+
"format": "int32",
115+
"minimum": 0,
116+
"maximum": 120,
117+
}
118+
)
119+
birth_date_schema = Resource.from_contents(
120+
{
121+
"type": "string",
122+
"format": "date",
123+
},
124+
default_specification=DRAFT202012,
125+
)
126+
registry = Registry().with_resources(
127+
[
128+
("urn:name-schema", name_schema),
129+
("urn:age-schema", age_schema),
130+
("urn:birth-date-schema", birth_date_schema),
131+
],
132+
)
133+
schema = {
134+
"type": "object",
135+
"required": ["name"],
136+
"properties": {
137+
"name": {"$ref": "urn:name-schema"},
138+
"age": {"$ref": "urn:age-schema"},
139+
"birth-date": {"$ref": "urn:birth-date-schema"},
140+
},
141+
"additionalProperties": False,
142+
}
143+
144+
validator = validator_class(schema, registry=registry)
145+
result = validator.validate({"name": "John", "age": 23}, schema)
146+
147+
assert result is None
148+
101149

102150
class TestOAS30ValidatorValidate(BaseTestOASValidatorValidate):
103151
@pytest.fixture

0 commit comments

Comments
 (0)