openapi icon indicating copy to clipboard operation
openapi copied to clipboard

`Bundle` leaves relative `$ref`s inside bundled path items pointing at the original files

Open BogdanYarotsky opened this issue 1 month ago • 0 comments

Summary

openapi.Bundle (and openapi spec bundle) moves a path item that lives in another file into components.pathItems, but the $refs inside its operations keep their original relative form. The schema they point to is added to components.schemas; the refs are just not rewritten to it, so the bundled document still depends on files it no longer sits next to. The same happens to a $ref nested inside an inline schema of a bundled response.

redocly split writes this layout (one file per path, refs to ../components/schemas/*.yaml), so bundling a split spec leaves the refs in its operations dangling.

Reproduction

openapi.yaml:

openapi: 3.1.0
info: {title: t, version: "1"}
paths:
  /items:
    $ref: ./paths/items.yaml

paths/items.yaml:

get:
  responses:
    "200":
      description: ok
      content:
        application/json:
          schema: {$ref: ../schemas/Item.yaml}

schemas/Item.yaml:

type: object
properties:
  id: {type: string}
$ openapi spec bundle openapi.yaml
openapi: 3.1.0
info: {title: t, version: "1"}
paths:
  /items:
    $ref: '#/components/pathItems/items'
components:
  schemas:
    Item:
      type: object
      properties:
        id:
          type: string
  pathItems:
    items:
      get:
        responses:
          "200":
            description: ok
            content:
              application/json:
                schema:
                  $ref: ../schemas/Item.yaml

The second shape: a response file whose schema is inline and holds the ref one level down.

nested.yaml:

openapi: 3.1.0
info: {title: t, version: "1"}
paths:
  /items:
    get:
      responses:
        "200": {$ref: ./responses/list.yaml}

responses/list.yaml:

description: ok
content:
  application/json:
    schema:
      type: array
      items: {$ref: ../schemas/Item.yaml}

openapi spec bundle nested.yaml adds Item to components.schemas but writes components.responses.list with items: {$ref: ../schemas/Item.yaml}.

Calling the library directly (openapi.Unmarshal, then openapi.Bundle with ResolveOptions{RootDocument: doc, TargetLocation: <absolute path of openapi.yaml>}) gives the same output.

Expected result

$ref: '#/components/schemas/Item' in both places. That is what happens when the response file has the ref directly under the media type (schema: {$ref: ../schemas/Item.yaml}): it is rewritten to #/components/schemas/Item.

Actual result

$ref: ../schemas/Item.yaml is kept, relative to a file the bundled document does not sit next to. The command reports success.

Version

v1.26.0 (also main, d269643). CLI from go install github.com/speakeasy-api/openapi/cmd/openapi@latest (v0.0.0-20261016011534-d2696438ec74).

Cause

rewriteRefsInBundledComponents updates only a fixed set of direct ref fields: walkAndUpdateRefsInPathItem (openapi/bundle.go:530) only the path-level parameters, and walkAndUpdateRefsInResponse (openapi/bundle.go:436) only each media type's top-level schema/itemSchema and the response's headers, so refs inside operations and inside inline schemas are left as written.

BogdanYarotsky avatar Oct 28 '26 17:10 BogdanYarotsky