python-openapi/openapi-core

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

Wim-De-Clercq opened this issue · 3 comments

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

p1c2u commented

Hi @Wim-De-Clercq do you need the fix to be released soon?

That would be nice :).

But also, if it takes a couple days I don't mind either.

I've just tested the 0.19.4 release in my project. All my tests are passing. So it looks good ✔️