|
5 | 5 | oas30_write_schema_validators_factory, |
6 | 6 | ) |
7 | 7 | from openapi_core.validation.schemas.exceptions import InvalidSchemaValue |
| 8 | +from openapi_core.validation.schemas.validators import SchemaValidator |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestSchemaValidate: |
@@ -356,3 +357,144 @@ def test_enforce_properties_required_applies_to_nested_composed_schemas( |
356 | 357 | schema, |
357 | 358 | enforce_properties_required=True, |
358 | 359 | ).validate({"name": "openapi-core", "meta": {}}) |
| 360 | + |
| 361 | + |
| 362 | +class TestSchemaValidateState: |
| 363 | + SCHEMA_DICT = { |
| 364 | + "type": "object", |
| 365 | + "properties": { |
| 366 | + "x": {"oneOf": [{"type": "string"}, {"type": "integer"}]} |
| 367 | + }, |
| 368 | + } |
| 369 | + VALUE = {"x": "hi"} |
| 370 | + |
| 371 | + @pytest.fixture(autouse=True) |
| 372 | + def clear_cache(self): |
| 373 | + # Keep this class's cache observations isolated from other tests. |
| 374 | + SchemaValidator._needs_state_cache.clear() |
| 375 | + yield |
| 376 | + SchemaValidator._needs_state_cache.clear() |
| 377 | + |
| 378 | + @pytest.fixture |
| 379 | + def cache(self): |
| 380 | + return SchemaValidator._needs_state_cache |
| 381 | + |
| 382 | + @pytest.fixture |
| 383 | + def validator_and_prop_factory(self): |
| 384 | + # Build a validator over a freshly loaded spec and return it |
| 385 | + # alongside the SchemaPath the cache keys on for property "x". |
| 386 | + root = SchemaPath.from_dict({}) |
| 387 | + |
| 388 | + def _build(schema_dict): |
| 389 | + spec = SchemaPath.from_dict(schema_dict) |
| 390 | + validator = oas30_write_schema_validators_factory.create( |
| 391 | + root, spec |
| 392 | + ) |
| 393 | + prop = spec / "properties" / "x" |
| 394 | + return validator, prop |
| 395 | + |
| 396 | + return _build |
| 397 | + |
| 398 | + def test_cold_pass_populates_cache( |
| 399 | + self, cache, validator_and_prop_factory |
| 400 | + ): |
| 401 | + validator, prop = validator_and_prop_factory(self.SCHEMA_DICT) |
| 402 | + assert prop not in cache |
| 403 | + |
| 404 | + validator.validate_state(self.VALUE) |
| 405 | + |
| 406 | + # oneOf under "x" -> a ValidationState is worthwhile. |
| 407 | + assert cache[prop] is True |
| 408 | + |
| 409 | + def test_warm_pass_reads_cached_answer( |
| 410 | + self, cache, validator_and_prop_factory |
| 411 | + ): |
| 412 | + validator, prop = validator_and_prop_factory(self.SCHEMA_DICT) |
| 413 | + validator.validate_state(self.VALUE) # prime |
| 414 | + # Poison the entry: a genuine cache hit returns this value |
| 415 | + # unchanged, whereas a recompute would overwrite it back to True. |
| 416 | + cache[prop] = False |
| 417 | + |
| 418 | + validator.validate_state(self.VALUE) |
| 419 | + |
| 420 | + assert cache[prop] is False |
| 421 | + |
| 422 | + def test_distinct_spec_does_not_collide( |
| 423 | + self, cache, validator_and_prop_factory |
| 424 | + ): |
| 425 | + # Two separately loaded specs with identical contents have |
| 426 | + # distinct identity, so their equally-pathed property schemas |
| 427 | + # occupy separate cache slots instead of colliding. |
| 428 | + validator_a, prop_a = validator_and_prop_factory(self.SCHEMA_DICT) |
| 429 | + validator_b, prop_b = validator_and_prop_factory(self.SCHEMA_DICT) |
| 430 | + |
| 431 | + validator_a.validate_state(self.VALUE) |
| 432 | + assert prop_a in cache |
| 433 | + assert prop_b not in cache |
| 434 | + |
| 435 | + validator_b.validate_state(self.VALUE) |
| 436 | + assert cache[prop_a] is True |
| 437 | + assert cache[prop_b] is True |
| 438 | + |
| 439 | + |
| 440 | +class TestSchemaValidateStateRefDedup: |
| 441 | + # A single composed schema reached through two different $ref aliases. |
| 442 | + SCHEMA_DICT = { |
| 443 | + "type": "object", |
| 444 | + "properties": { |
| 445 | + "a": {"$ref": "#/$defs/Composed"}, |
| 446 | + "b": {"$ref": "#/$defs/Composed"}, |
| 447 | + }, |
| 448 | + "$defs": { |
| 449 | + "Composed": {"oneOf": [{"type": "string"}, {"type": "integer"}]}, |
| 450 | + }, |
| 451 | + } |
| 452 | + VALUE = {"a": "hi", "b": 1} |
| 453 | + |
| 454 | + @pytest.fixture(autouse=True) |
| 455 | + def clear_cache(self): |
| 456 | + SchemaValidator._needs_state_cache.clear() |
| 457 | + yield |
| 458 | + SchemaValidator._needs_state_cache.clear() |
| 459 | + |
| 460 | + @pytest.fixture |
| 461 | + def cache(self): |
| 462 | + return SchemaValidator._needs_state_cache |
| 463 | + |
| 464 | + @pytest.fixture |
| 465 | + def validator_and_props_factory(self): |
| 466 | + root = SchemaPath.from_dict({}) |
| 467 | + |
| 468 | + def _build(schema_dict): |
| 469 | + spec = SchemaPath.from_dict(schema_dict) |
| 470 | + validator = oas30_write_schema_validators_factory.create( |
| 471 | + root, spec |
| 472 | + ) |
| 473 | + prop_a = spec / "properties" / "a" |
| 474 | + prop_b = spec / "properties" / "b" |
| 475 | + canonical = spec / "$defs" / "Composed" |
| 476 | + return validator, prop_a, prop_b, canonical |
| 477 | + |
| 478 | + return _build |
| 479 | + |
| 480 | + @pytest.mark.xfail( |
| 481 | + strict=True, |
| 482 | + reason=( |
| 483 | + "The cache keys on the navigation path, so each $ref " |
| 484 | + "alias gets its own slot. Once the cache keys on canonical " |
| 485 | + "the aliases collapse to a single entry." |
| 486 | + ), |
| 487 | + ) |
| 488 | + def test_aliases_to_same_node_share_one_cache_slot( |
| 489 | + self, cache, validator_and_props_factory |
| 490 | + ): |
| 491 | + validator, prop_a, prop_b, canonical = validator_and_props_factory( |
| 492 | + self.SCHEMA_DICT |
| 493 | + ) |
| 494 | + |
| 495 | + validator.validate_state(self.VALUE) |
| 496 | + |
| 497 | + assert len(cache) == 1 |
| 498 | + assert prop_a not in cache |
| 499 | + assert prop_b not in cache |
| 500 | + assert cache[canonical] is True |
0 commit comments