Skip to content

Commit 012416b

Browse files
authored
Merge pull request #853 from chantera/fix/response-validate-ref
Fix resolver for jsonschema validator created by `SchemaValidatorsFactory`
2 parents bfbd728 + 7d0f325 commit 012416b

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

openapi_core/validation/schemas/factories.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ def create(
5757
format_checker = self.get_format_checker(
5858
format_validators, extra_format_validators
5959
)
60-
resolver = schema.accessor.resolver # type: ignore
61-
with schema.open() as schema_dict:
60+
with schema.resolve() as resolved:
6261
jsonschema_validator = self.schema_validator_class(
63-
schema_dict,
64-
_resolver=resolver,
62+
resolved.contents,
63+
_resolver=resolved.resolver,
6564
format_checker=format_checker,
6665
)
6766

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: sample
4+
version: "0.1"
5+
paths:
6+
/books:
7+
$ref: "./paths/books.yaml"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
get:
2+
responses:
3+
"200":
4+
description: OK
5+
content:
6+
application/json:
7+
schema:
8+
type: array
9+
items:
10+
$ref: "../schemas/book.yaml#/Book"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Book:
2+
type: object
3+
properties:
4+
id:
5+
$ref: "#/BookId"
6+
title:
7+
type: string
8+
BookId:
9+
type: string
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
import pytest
4+
from jsonschema_path import SchemaPath
5+
6+
from openapi_core import Config
7+
from openapi_core import OpenAPI
8+
from openapi_core.testing import MockRequest
9+
from openapi_core.testing import MockResponse
10+
11+
12+
class TestParentReference:
13+
14+
spec_path = "data/v3.0/parent-reference/openapi.yaml"
15+
16+
@pytest.fixture
17+
def openapi(self, content_factory):
18+
content, base_uri = content_factory.from_file(self.spec_path)
19+
spec = SchemaPath.from_dict(content, base_uri=base_uri)
20+
config = Config(spec_base_uri=base_uri)
21+
return OpenAPI(spec, config=config)
22+
23+
def test_valid(self, openapi):
24+
request = MockRequest(host_url="", method="GET", path="/books")
25+
response = MockResponse(
26+
data=json.dumps([{"id": "BOOK:01", "title": "Test Book"}]).encode()
27+
)
28+
29+
openapi.validate_response(request, response)

0 commit comments

Comments
 (0)