From 593b9b5b458f1f1856cfafdd94fb43c2a20f1b5c Mon Sep 17 00:00:00 2001 From: Wim De Clercq Date: Fri, 16 Aug 2024 11:26:31 +0200 Subject: [PATCH] Fix resolvers not updating properly when referencing other files. Issue #893 --- openapi_core/validation/schemas/validators.py | 7 +++++-- .../validation/test_parent_reference.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/openapi_core/validation/schemas/validators.py b/openapi_core/validation/schemas/validators.py index 7a764e31..6ae1b2eb 100644 --- a/openapi_core/validation/schemas/validators.py +++ b/openapi_core/validation/schemas/validators.py @@ -38,8 +38,11 @@ def validate(self, value: Any) -> None: def evolve(self, schema: SchemaPath) -> "SchemaValidator": cls = self.__class__ - with schema.open() as schema_dict: - return cls(schema, self.validator.evolve(schema=schema_dict)) + with schema.resolve() as resolved: + validator = self.validator.evolve( + schema=resolved.contents, _resolver=resolved.resolver + ) + return cls(schema, validator) def type_validator( self, value: Any, type_override: Optional[str] = None diff --git a/tests/integration/validation/test_parent_reference.py b/tests/integration/validation/test_parent_reference.py index df526835..21e37351 100644 --- a/tests/integration/validation/test_parent_reference.py +++ b/tests/integration/validation/test_parent_reference.py @@ -5,6 +5,7 @@ from openapi_core import Config from openapi_core import OpenAPI +from openapi_core import V30ResponseUnmarshaller from openapi_core.testing import MockRequest from openapi_core.testing import MockResponse @@ -13,6 +14,13 @@ class TestParentReference: spec_path = "data/v3.0/parent-reference/openapi.yaml" + @pytest.fixture + def unmarshaller(self, content_factory): + content, base_uri = content_factory.from_file(self.spec_path) + return V30ResponseUnmarshaller( + spec=SchemaPath.from_dict(content, base_uri=base_uri) + ) + @pytest.fixture def openapi(self, content_factory): content, base_uri = content_factory.from_file(self.spec_path) @@ -27,3 +35,11 @@ def test_valid(self, openapi): ) openapi.validate_response(request, response) + + def test_unmarshal(self, unmarshaller): + request = MockRequest(host_url="", method="GET", path="/books") + response = MockResponse( + data=json.dumps([{"id": "BOOK:01", "title": "Test Book"}]).encode() + ) + + unmarshaller.unmarshal(request, response)