Skip to content

fix(codegen): traverse references in all supported OpenAPI fields - #2554

Open
abramchikd wants to merge 2 commits into
oapi-codegen:mainfrom
abramchikd:fix/complete-openapi-ref-traversal
Open

abramchikd wants to merge 2 commits into
oapi-codegen:mainfrom
abramchikd:fix/complete-openapi-ref-traversal

Conversation

@abramchikd

@abramchikd abramchikd commented Sep 15, 2026 •

Copy link
Copy Markdown

fix(codegen): traverse references in all supported OpenAPI fields

Summary

Extend the codegen reference walker so component pruning discovers references
from all SchemaRef-bearing fields supported by the current kin-openapi model,
as well as from webhooks and nested media-type content.

Previously, a component referenced only from an OpenAPI 3.1 JSON Schema keyword,
an OpenAPI 3.2 itemSchema, a webhook, header content, or an encoding header
could be treated as unused and removed from components.

Problem

pruneUnusedComponents determines which components are reachable through
findComponentRefs, which in turn uses the shared walkSwagger traversal.
That traversal covered the original OpenAPI 3.0 schema locations but did not
keep pace with fields added to kin-openapi for OpenAPI 3.1 and 3.2.

For example, the reference in this OpenAPI 3.1 schema was not visited:

components:
  schemas:
    Text:
      type: string
    Container:
      type: object
      propertyNames:
        $ref: '#/components/schemas/Text'

The same problem affected references under prefixItems, contains,
patternProperties, dependentSchemas, unevaluatedItems,
unevaluatedProperties, if/then/else, $defs, and contentSchema.

Root-level webhooks were also skipped. In addition, media-type traversal did not
visit OpenAPI 3.2 itemSchema, schemas inside Header.Content, or headers nested
under MediaType.Encoding.

Changes

  • Visit root-level webhooks using the same path-item traversal as normal paths.
  • Continue walking components when an OpenAPI 3.1 document omits paths.
  • Centralize traversal of child schema references in schemaChildRefs.
  • Cover every SchemaRef-bearing field currently exposed by
    openapi3.Schema, including the OpenAPI 3.1 JSON Schema keywords.
  • Centralize media-type traversal in walkContent.
  • Visit both schema and OpenAPI 3.2 itemSchema in media types.
  • Visit schemas and examples nested in parameter, request-body, response, and
    header content.
  • Visit headers nested under media-type encodings.
  • Reuse path-item traversal for callbacks.

This pull request was created with the assistance of OpenAI Codex.

@abramchikd
abramchikd requested a review from a team as a code owner September 15, 2026 08:52
@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable issue remains in the changes since the previous review.

Summary

Extends component-reference traversal so pruning preserves schemas referenced through OpenAPI 3.1/3.2 fields, webhooks, nested content, and encoding headers.

  • Centralizes schema-child and media-type traversal.
  • Adds cycle protection for resolved recursive schema references.
  • Adds focused regression coverage for the newly traversed locations.

Reviews (2) · Last reviewed commit: "Merge branch 'main' into fix/complete-op..." · Reviewed by Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant