|
1 | 1 | from base64 import b64encode |
| 2 | +from typing import Any |
| 3 | +from typing import cast |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 | from jsonschema import ValidationError |
@@ -353,6 +355,57 @@ def test_oneof_required(self, validator_class): |
353 | 355 | result = validator.validate(instance) |
354 | 356 | assert result is None |
355 | 357 |
|
| 358 | + def test_discriminator_does_not_swallow_unexpected_ref_errors( |
| 359 | + self, monkeypatch |
| 360 | + ): |
| 361 | + schema = { |
| 362 | + "$ref": "#/components/schemas/Route", |
| 363 | + "components": { |
| 364 | + "schemas": { |
| 365 | + "Route": { |
| 366 | + "oneOf": [ |
| 367 | + {"$ref": "#/components/schemas/MountainHiking"} |
| 368 | + ], |
| 369 | + "discriminator": { |
| 370 | + "propertyName": "discipline", |
| 371 | + "mapping": { |
| 372 | + "mountain_hiking": "#/components/schemas/MountainHiking" |
| 373 | + }, |
| 374 | + }, |
| 375 | + }, |
| 376 | + "MountainHiking": { |
| 377 | + "type": "object", |
| 378 | + "properties": { |
| 379 | + "discipline": {"type": "string"}, |
| 380 | + "length": {"type": "integer"}, |
| 381 | + }, |
| 382 | + "required": ["discipline", "length"], |
| 383 | + }, |
| 384 | + }, |
| 385 | + }, |
| 386 | + } |
| 387 | + |
| 388 | + def fail_validate_reference(*args: Any, **kwargs: Any) -> None: |
| 389 | + raise RuntimeError("boom") |
| 390 | + |
| 391 | + monkeypatch.setattr( |
| 392 | + cast(Any, OAS30Validator), |
| 393 | + "_validate_reference", |
| 394 | + fail_validate_reference, |
| 395 | + ) |
| 396 | + |
| 397 | + validator = OAS30Validator( |
| 398 | + schema, |
| 399 | + format_checker=oas30_format_checker, |
| 400 | + ) |
| 401 | + with pytest.raises(RuntimeError, match="boom"): |
| 402 | + validator.validate( |
| 403 | + { |
| 404 | + "discipline": "mountain_hiking", |
| 405 | + "length": 10, |
| 406 | + } |
| 407 | + ) |
| 408 | + |
356 | 409 | @pytest.mark.parametrize( |
357 | 410 | "schema_type", |
358 | 411 | [ |
|
0 commit comments