Skip to content

Commit 5979dd4

Browse files
patchwrightruvnet
andcommitted
fix: wrap unresolvable $ref in ValidationError
When a $ref points to a nonexistent target, the referencing library's Unresolvable (PointerToNowhere) exception escaped uncaught instead of being wrapped in a ValidationError. This leaked the internal library exception (and, as a side effect, dumped the entire spec into the error message). Catch Unresolvable in SpecValidator.validate() and is_valid(), wrap in jsonschema.exceptions.ValidationError. Updated existing test_ref_failed tests to expect ValidationError (the documented behavior) instead of Unresolvable (the leak). Fixes #511. Assisted-by: Claude (Anthropic). Co-Authored-By: claude-flow <ruv@ruv.net>
1 parent 2121137 commit 5979dd4

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

openapi_spec_validator/validation/validators.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from functools import lru_cache
88
from typing import cast
99

10+
from referencing.exceptions import Unresolvable
11+
1012
from jsonschema.exceptions import ValidationError
1113
from jsonschema.protocols import Validator
1214
from jsonschema_path.handlers import default_handlers
@@ -69,11 +71,17 @@ def __init__(
6971
)
7072

7173
def validate(self) -> None:
72-
for err in self.iter_errors():
73-
raise err
74+
try:
75+
for err in self.iter_errors():
76+
raise err
77+
except Unresolvable as exc:
78+
raise ValidationError(str(exc)) from exc
7479

7580
def is_valid(self) -> bool:
76-
error = next(self.iter_errors(), None)
81+
try:
82+
error = next(self.iter_errors(), None)
83+
except Unresolvable:
84+
return False
7785
return error is None
7886

7987
@property

tests/integration/validation/test_validators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from jsonschema.exceptions import ValidationError
23
from jsonschema_path import SchemaPath
34
from referencing.exceptions import Unresolvable
45

@@ -65,7 +66,7 @@ def test_ref_failed(self, factory, spec_file):
6566
spec = factory.spec_from_file(spec_path)
6667
spec_url = factory.spec_file_url(spec_path)
6768

68-
with pytest.raises(Unresolvable):
69+
with pytest.raises(ValidationError):
6970
OpenAPIV2SpecValidator(spec, base_uri=spec_url).validate()
7071

7172

@@ -177,7 +178,7 @@ def test_ref_failed(self, factory, spec_file):
177178
spec = factory.spec_from_file(spec_path)
178179
spec_url = factory.spec_file_url(spec_path)
179180

180-
with pytest.raises(Unresolvable):
181+
with pytest.raises(ValidationError):
181182
OpenAPIV30SpecValidator(spec, base_uri=spec_url).validate()
182183

183184

0 commit comments

Comments
 (0)