Skip to content

Commit 62f5544

Browse files
committed
Fix malformed schema traversal to report validation errors instead of internal exceptions
1 parent 1b3e90a commit 62f5544

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

openapi_spec_validator/validation/keywords.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DuplicateOperationIDError,
2020
)
2121
from openapi_spec_validator.validation.exceptions import ExtraParametersError
22+
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError
2223
from openapi_spec_validator.validation.exceptions import (
2324
ParameterDuplicateError,
2425
)
@@ -98,7 +99,14 @@ def __call__(
9899
self, schema: SchemaPath, require_properties: bool = True
99100
) -> Iterator[ValidationError]:
100101
schema_value = schema.read_value()
101-
if not hasattr(schema_value, "__getitem__"):
102+
103+
try:
104+
self.default_validator.value_validator_cls.check_schema(schema_value)
105+
except Exception as err:
106+
yield OpenAPIValidationError.create_from(err)
107+
return
108+
109+
if isinstance(schema_value, bool):
102110
return
103111

104112
assert self.schema_ids_registry is not None

tests/integration/test_main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,34 @@ def test_schema_stdin(capsys):
193193
assert "stdin: OK\n" in out
194194

195195

196+
def test_malformed_schema_stdin(capsys):
197+
"""Malformed schema from STDIN reports validation error."""
198+
spec_io = StringIO(
199+
"""
200+
openapi: 3.1.0
201+
info:
202+
version: "1"
203+
title: "Title"
204+
components:
205+
schemas:
206+
Component:
207+
type: object
208+
properties:
209+
name: string
210+
"""
211+
)
212+
213+
testargs = ["--schema", "3.1.0", "-"]
214+
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
215+
with pytest.raises(SystemExit):
216+
main(testargs)
217+
218+
out, err = capsys.readouterr()
219+
assert not err
220+
assert "stdin: Validation Error:" in out
221+
assert "stdin: OK" not in out
222+
223+
196224
def test_version(capsys):
197225
"""Test --version flag outputs correct version."""
198226
testargs = ["--version"]

tests/integration/validation/test_exceptions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from openapi_spec_validator import OpenAPIV2SpecValidator
22
from openapi_spec_validator import OpenAPIV30SpecValidator
3+
from openapi_spec_validator import OpenAPIV31SpecValidator
34
from openapi_spec_validator.validation.exceptions import (
45
DuplicateOperationIDError,
56
)
@@ -495,3 +496,29 @@ def validate(to_validate) -> bool:
495496
assert len(errors_list) == 1
496497
assert errors_list[0].__class__ == OpenAPIValidationError
497498
assert errors_list[0].message == ("'invalid' is not a 'custom'")
499+
500+
def test_malformed_property_schema(self):
501+
spec = {
502+
"openapi": "3.1.0",
503+
"info": {
504+
"title": "Test Api",
505+
"version": "0.0.1",
506+
},
507+
"components": {
508+
"schemas": {
509+
"Component": {
510+
"type": "object",
511+
"properties": {
512+
"name": "string",
513+
},
514+
}
515+
},
516+
},
517+
}
518+
519+
errors = OpenAPIV31SpecValidator(spec).iter_errors()
520+
521+
errors_list = list(errors)
522+
assert len(errors_list) == 1
523+
assert errors_list[0].__class__ == OpenAPIValidationError
524+
assert "'string' is not of type 'object'" in errors_list[0].message

0 commit comments

Comments
 (0)