Add support for path aliases#2312
Conversation
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>
cf4dc40 to
aee9d31
Compare
Greptile SummaryThis PR fixes a long-standing code-generation crash when an OpenAPI spec uses Confidence Score: 5/5Safe 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.
|
| 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>
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.