Skip to content

Commit 964a916

Browse files
committed
tests: Add test for nullable refs
This is a known bug. Add a test for it so we can work on fixing it. Signed-off-by: Stephen Finucane <[email protected]> Related-bug: python-openapi#20
1 parent 00232d5 commit 964a916

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

tests/integration/test_validators.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,54 @@ def test_string_uuid(self, value):
201201

202202
assert result is None
203203

204+
def test_ref(self):
205+
schema = {
206+
"$ref": "#/$defs/Pet",
207+
"$defs": {
208+
"Pet": {
209+
"required": ["id", "name"],
210+
"properties": {
211+
"id": {"type": "integer", "format": "int64"},
212+
"name": {"type": "string"},
213+
"tag": {"type": "string"},
214+
},
215+
}
216+
},
217+
}
218+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
219+
220+
result = validator.validate({"id": 1, "name": "John"})
221+
assert result is None
222+
223+
with pytest.raises(ValidationError) as excinfo:
224+
validator.validate({"name": "John"})
225+
226+
error = "'id' is a required property"
227+
assert error in str(excinfo.value)
228+
229+
@pytest.mark.xfail(reason="Issue #20")
230+
def test_ref_nullable(self):
231+
schema = {
232+
"nullable": True,
233+
"allOf": [
234+
{
235+
"$ref": "#/$defs/Pet",
236+
},
237+
],
238+
"$defs": {
239+
"Pet": {
240+
"required": ["id", "name"],
241+
"properties": {
242+
"id": {"type": "integer", "format": "int64"},
243+
"name": {"type": "string"},
244+
"tag": {"type": "string"},
245+
},
246+
}
247+
},
248+
}
249+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
250+
validator.validate(None)
251+
204252
def test_allof_required(self):
205253
schema = {
206254
"allOf": [
@@ -217,6 +265,20 @@ def test_allof_required(self):
217265
):
218266
validator.validate({"another_prop": "bla"})
219267

268+
@pytest.mark.xfail(reason="Issue #20")
269+
def test_allof_nullable(self):
270+
schema = {
271+
"allOf": [
272+
{
273+
"type": "object",
274+
"properties": {"some_prop": {"type": "string"}},
275+
},
276+
{"type": "object", "nullable": True},
277+
]
278+
}
279+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
280+
validator.validate(None)
281+
220282
def test_required(self):
221283
schema = {
222284
"type": "object",
@@ -665,7 +727,7 @@ def test_schema_validation(self):
665727
error = "'-12' is not a 'date'"
666728
assert error in str(excinfo.value)
667729

668-
def test_schema_ref(self):
730+
def test_ref(self):
669731
schema = {
670732
"$ref": "#/$defs/Pet",
671733
"$defs": {
@@ -693,6 +755,28 @@ def test_schema_ref(self):
693755
error = "'id' is a required property"
694756
assert error in str(excinfo.value)
695757

758+
def test_ref_nullable(self):
759+
# specifying an array for type only works with primitive types
760+
schema = {
761+
"oneOf": [
762+
{"type": "null"},
763+
{"$ref": "#/$defs/Pet"},
764+
],
765+
"$defs": {
766+
"Pet": {
767+
"type": "object",
768+
"required": ["id", "name"],
769+
"properties": {
770+
"id": {"type": "integer", "format": "int64"},
771+
"name": {"type": "string"},
772+
"tag": {"type": "string"},
773+
},
774+
}
775+
},
776+
}
777+
validator = OAS31Validator(schema, format_checker=oas30_format_checker)
778+
validator.validate(None)
779+
696780
@pytest.mark.parametrize(
697781
"value",
698782
[

0 commit comments

Comments
 (0)