Skip to content

[Bug]: _validate_reference cannot resolve $ref with a relative file path #852

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
chantera opened this issue Jun 12, 2024 · 0 comments · Fixed by #853
Closed

[Bug]: _validate_reference cannot resolve $ref with a relative file path #852

chantera opened this issue Jun 12, 2024 · 0 comments · Fixed by #853
Labels
kind/bug Indicates an issue

Comments

@chantera
Copy link
Contributor

Actual Behavior

openapi.validate_response 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 validator should not raise the error for $ref.
I ensured no spec-related error occured when openapi.check_spec() called.

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

SPEC_PATH = os.path.join(os.path.dirname(__file__), "openapi.yaml")

openapi = openapi_core.OpenAPI.from_file_path(
    SPEC_PATH,
    config=openapi_core.Config(spec_base_uri=f"file://{SPEC_PATH}"),
)
openapi.check_spec()  # ensure the spec is valid

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()
)
openapi.validate_request(request)
openapi.validate_response(request, response)  # raises error

Run:

$ python validate.py

OpenAPI Core Version

3.0.0,3.1.0

OpenAPI Core Integration

Nothing

Affected Area(s)

validation

References

No response

Anything else we need to know?

No response

Would you like to implement a fix?

Yes

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

Successfully merging a pull request may close this issue.

1 participant