python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
297 stars 132 forks source link

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

Closed Wim-De-Clercq closed 1 week ago

Wim-De-Clercq commented 4 weeks ago

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. https://github.com/python-openapi/openapi-core/issues/852

Anything else we need to know?

Similar fix to be done here: https://github.com/python-openapi/openapi-core/blob/2e98965c219abffefb3419f54cc25b5153de4b9f/openapi_core/validation/schemas/validators.py#L38-L42

Would you like to implement a fix?

Yes

p1c2u commented 4 weeks ago

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

Wim-De-Clercq commented 4 weeks ago

That would be nice :).

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

Wim-De-Clercq commented 1 week ago

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