Skip to content

Add support for path aliases#2312

Open
mromaszewicz wants to merge 2 commits intooapi-codegen:mainfrom
mromaszewicz:fix/issue-223
Open

Add support for path aliases#2312
mromaszewicz wants to merge 2 commits intooapi-codegen:mainfrom
mromaszewicz:fix/issue-223

Conversation

@mromaszewicz
Copy link
Copy Markdown
Member

Closes #223

When an OpenAPI spec uses $ref to alias one path to another (e.g. /test2: $ref: '#/paths/~1test'), kin-openapi inlines the referenced path item into both entries. Previously this produced duplicate ServerInterface methods, wrapper functions, and type definitions — generating Go code that never compiled.

Detect internal path aliases via PathItem.Ref (only when it starts with "#/paths/", to distinguish from external file references). For alias operations:

  • Server: skip interface method, wrapper, and strict handler generation. Route registration still emits both paths, pointing to the canonical wrapper via the new HandlerName() method on OperationDefinition.

  • Client: generate methods with a suffixed OperationId (e.g. GetTestAlias0) so each aliased path gets its own request builder targeting the correct URL.

@mromaszewicz mromaszewicz requested a review from a team as a code owner April 3, 2026 04:24
@mromaszewicz
Copy link
Copy Markdown
Member Author

@greptileai

Closes oapi-codegen#223

When an OpenAPI spec uses $ref to alias one path to another (e.g.
/test2: $ref: '#/paths/~1test'), kin-openapi inlines the referenced
path item into both entries. Previously this produced duplicate
ServerInterface methods, wrapper functions, and type definitions —
generating Go code that never compiled.

Detect internal path aliases via PathItem.Ref (only when it starts
with "#/paths/", to distinguish from external file references). For
alias operations:

- Server: skip interface method, wrapper, and strict handler generation.
  Route registration still emits both paths, pointing to the canonical
  wrapper via the new HandlerName() method on OperationDefinition.

- Client: generate methods with a suffixed OperationId (e.g.
  GetTestAlias0) so each aliased path gets its own request builder
  targeting the correct URL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mromaszewicz mromaszewicz added the enhancement New feature or request label Apr 3, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 3, 2026

Greptile Summary

This PR fixes a long-standing code-generation crash when an OpenAPI spec uses $ref to alias one path to another (e.g. /test2: $ref: '#/paths/~1test'). It detects internal path aliases via pathItem.Ref, adds IsAlias/AliasTarget fields and a HandlerName() method to OperationDefinition, and gates all server-side code generation (interface methods, wrappers, strict handlers, type definitions) on {{if not .IsAlias}} across all supported router templates, while still registering both URL paths. Client generation correctly produces separately-named methods targeting the aliased URL.

Confidence Score: 5/5

Safe to merge; all findings are P2 style/cleanup suggestions with no blocking correctness issues.

The core alias-detection and naming logic is correct across all ordering scenarios (alias before or after canonical, multiple aliases to the same path). All template changes consistently apply the {{if not .IsAlias}} guard. Remaining findings are an edge-case theoretical risk with the importMapping lookup and a missing runtime test — neither blocks correctness for the described use case.

pkg/codegen/operations.go — internal ref passed to ensureExternalRefs* functions; alias counter collision edge case.

Important Files Changed

Filename Overview
pkg/codegen/operations.go Core change: detects internal path aliases via pathItem.Ref, adds IsAlias/AliasTarget fields to OperationDefinition, and adds HandlerName() method. Minor concern: ensureExternalRefs* functions are called with internal #/paths/… refs they weren't designed for.
internal/test/pathalias/spec.yaml New test OpenAPI spec with a simple /test endpoint and /test2 aliased via $ref. Covers the basic case described in the PR.
internal/test/pathalias/doc.go Standard go:generate directives for the pathalias test package; no runtime test file present.
pkg/codegen/templates/chi/chi-handler.tmpl Route registration now uses .HandlerName instead of .OperationId, routing aliases to the canonical wrapper method correctly.
pkg/codegen/templates/chi/chi-interface.tmpl ServerInterface and Unimplemented type now skip alias operations with {{if not .IsAlias}}, preventing duplicate method declarations.
pkg/codegen/templates/strict/strict-interface.tmpl RequestObject structs and StrictServerInterface entries are correctly skipped for alias operations.

Reviews (2): Last reviewed commit: "fix: skip operationId write-back for ali..." | Re-trigger Greptile

Alias paths share the same *openapi3.Operation pointer as the canonical
path. Writing the suffixed alias name (e.g. GetTestAlias0) back to
op.OperationID would corrupt the canonical operation's ID in the
embedded spec.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mromaszewicz
Copy link
Copy Markdown
Member Author

@greptileai

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for path aliases

1 participant