Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Wim-De-Clercq opened this issue Aug 16, 2024 · 3 comments
Closed
Labels
area/unmarshalling Indicates an issue on unmarshalling area. kind/bug/confirmed kind/bug Indicates an issue

Comments

@Wim-De-Clercq
Copy link
Contributor

Wim-De-Clercq commented Aug 16, 2024

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

@Wim-De-Clercq Wim-De-Clercq added the kind/bug Indicates an issue label Aug 16, 2024
Wim-De-Clercq added a commit to Wim-De-Clercq/openapi-core that referenced this issue Aug 16, 2024
Wim-De-Clercq added a commit to Wim-De-Clercq/openapi-core that referenced this issue Aug 16, 2024
@p1c2u p1c2u added kind/bug/confirmed area/unmarshalling Indicates an issue on unmarshalling area. labels Aug 16, 2024
@p1c2u
Copy link
Collaborator

p1c2u commented Aug 16, 2024

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

@Wim-De-Clercq
Copy link
Contributor Author

That would be nice :).

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

@Wim-De-Clercq
Copy link
Contributor Author

Wim-De-Clercq commented Sep 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/unmarshalling Indicates an issue on unmarshalling area. kind/bug/confirmed kind/bug Indicates an issue
Projects
None yet
Development

No branches or pull requests

2 participants