libopenapi icon indicating copy to clipboard operation
libopenapi copied to clipboard

`$ref`s are not resolved correctly w/r/t JSON Schema

Open hdonnay opened this issue 2 months ago • 3 comments
trafficstars

In an OpenAPI doc like in the details, the $ref from one type should resolve to the other.

As I understand the OpenAPI and JSON Schema specs, the relative URI $ref should be resolved to an absolute URI using the context of the schema's $id if present, and that value should be checked against other already-resolved $ids. It seems that $ids are not checked, currently.

openapi: 3.1.0
components:
  schemas:
    s1:  # This object should be resolvable at "https://example.com/a.json" from anywhere, and "#/components/schemas/s1" from a schema object without a "$schema" field.
      $schema: "https://json-schema.org/draft/2020-12/schema"
      $id: "https://example.com/a.json"
      type: "string"
    s2:
      $schema: "https://json-schema.org/draft/2020-12/schema"
      $id: "https://example.com/b.json"
      $ref: "a.json"  # becomes "https://example.com/a.json", which should already be resolved.

This is freehand, so there may be some problem; sorry. I can try to bust down my real-world OpenAPI spec if needed.

hdonnay avatar Sep 19 '25 20:09 hdonnay