Skip to content

[Bug]: unmarshal cannot resolve $ref with a relative file path #893

Closed
@Wim-De-Clercq

Description

@Wim-De-Clercq

Actual Behavior

V30ResponseUnmarshaller.unmarshal raises jsonschema.exceptions._WrappedReferencingError when the response schema defined in a different file includes $ref that refers to a schema in another file.

Expected Behavior

The unmarshaller should not raise the error for $ref.

Steps to Reproduce

openapi.yaml

openapi: "3.0.0"
info:
  title: sample
  version: "0.1"
paths:
  /books:
    $ref: "./paths/books.yaml"

paths/books.yaml

get:
  responses:
    "200":
      description: OK
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: "../schemas/book.yaml#/Book"

schemas/book.yaml

Book:
  type: object
  properties:
    id:
      $ref: "#/BookId"
    title:
      type: string
BookId:
  type: string

validate.py

import json
import os

import openapi_core
import openapi_core.testing
from openapi_core import V30ResponseUnmarshaller

SPEC_PATH = os.path.join(os.path.dirname(__file__), "openapi.yaml")
content, base_uri = content_factory.from_file(SPEC_PATH)
return V30ResponseUnmarshaller(
    spec=SchemaPath.from_dict(content, base_uri=base_uri)
)

request = openapi_core.testing.MockRequest(
    host_url="", method="GET", path="/books"
)
response = openapi_core.testing.MockResponse(
    data=json.dumps([{"id": "BOOK:01", "title": "Test Book"}]).encode()
)
unmarshaller.unmarshal(request, response)  # raises error

OpenAPI Core Version

0.19.3

OpenAPI Core Integration

NA

Affected Area(s)

unmarshalling, schema

References

Almost a full copy of the previous issue.
#852

Anything else we need to know?

Similar fix to be done here:

def evolve(self, schema: SchemaPath) -> "SchemaValidator":
cls = self.__class__
with schema.open() as schema_dict:
return cls(schema, self.validator.evolve(schema=schema_dict))

Would you like to implement a fix?

Yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions