Skip to content

per-operation middleware in Echo#2353

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-518
Apr 30, 2026
Merged

per-operation middleware in Echo#2353
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-518

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #518

Add a RegisterHandlersWithOptions to both Echo backends, which takes a BaseUrl and a map of OperationMiddlewares, keyed by spec OperationId. Only Echo supports registering per-op middleware, so this change is only for Echo.

Closes: oapi-codegen#518

Add a RegisterHandlersWithOptions to both Echo backends, which takes
a BaseUrl and a map of OperationMiddlewares, keyed by spec
OperationId. Only Echo supports registering per-op middleware, so this
change is only for Echo.
@mromaszewicz mromaszewicz requested a review from a team as a code owner April 30, 2026 14:03
@mromaszewicz mromaszewicz added enhancement New feature or request server:echo labels Apr 30, 2026
@mromaszewicz mromaszewicz added this to the v2.7.0 milestone Apr 30, 2026
@greptile-apps

greptile-apps Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR implements per-operation middleware support for both Echo v4 and Echo v5 backends by introducing RegisterHandlersWithOptions (with a RegisterHandlersOptions struct containing BaseURL and OperationMiddlewares) and refactoring the existing RegisterHandlers/RegisterHandlersWithBaseURL helpers to delegate to it. The change is backward-compatible, correctly uses the raw spec operationId as the map key (with a normalized-identifier fallback when the spec omits one), and includes a solid regression test in internal/test/issues/issue-518/.

  • examples/petstore-expanded/echo/api/petstore-server.gen.go contains unrelated parameter-binding changes (BindQueryParameterBindQueryParameterWithOptions with added Type/Format fields; same for BindStyledParameterWithOptions) that do not appear in the echo-v5 equivalent — this file appears to have been regenerated with a toolchain that includes additional unreleased changes, and will likely cause a CI diff failure.

Confidence Score: 3/5

Mostly safe to merge, but the unrelated binding-change drift in the echo/v4 petstore example file needs to be resolved first.

The core template change and regression test are well-implemented, but a P1 finding — unrelated parameter-binding changes in examples/petstore-expanded/echo/api/petstore-server.gen.go absent from the echo-v5 counterpart — indicates the file was regenerated with a different toolchain and will likely cause a CI make generate diff failure.

examples/petstore-expanded/echo/api/petstore-server.gen.go — contains binding-call changes unrelated to this PR that are not present in the echo-v5 equivalent.

Important Files Changed

Filename Overview
examples/petstore-expanded/echo/api/petstore-server.gen.go Contains expected RegisterHandlersWithOptions changes plus unrelated binding-call changes absent from the echo-v5 equivalent; looks regenerated with a different toolchain.
examples/petstore-expanded/echo-v5/api/petstore-server.gen.go Only middleware-related changes; no unrelated drift; clean regeneration.
pkg/codegen/templates/echo/echo-register.tmpl Adds RegisterHandlersOptions with BaseURL+OperationMiddlewares and refactors registration into RegisterHandlersWithOptions; correct and nil-map-safe.
pkg/codegen/templates/echo/v5/echo-register.tmpl Identical change to the v4 template; clean and correct.
pkg/codegen/operations.go Adds MiddlewareKey() to OperationDefinition; correct spec-ID-first with normalized-ID fallback; no issues.
internal/test/issues/issue-518/issue518_test.go Good regression test covering per-op middleware, no-operationId fallback, and backward compat.
internal/test/issues/issue-518/issue518.gen.go New generated fixture; correctly uses raw spec IDs and normalized fallback as middleware keys.

Comments Outside Diff (1)

  1. examples/petstore-expanded/echo/api/petstore-server.gen.go, line 63-73 (link)

    P1 Unrelated drift in regenerated file

    examples/petstore-expanded/echo/api/petstore-server.gen.go contains parameter-binding changes unrelated to this PR's stated goal of per-operation middleware. Specifically, calls to BindQueryParameter have been replaced with BindQueryParameterWithOptions (gaining Type/Format fields), and BindStyledParameterWithOptions call sites gain the same extra fields. These changes do not appear in the otherwise-analogous echo-v5 petstore file, suggesting this file was regenerated with a local toolchain that includes additional unreleased changes. Per the project policy, CI fails when make generate produces a diff — if the intended generator does not yet emit these binding changes, CI will fail on this file.

Reviews (1): Last reviewed commit: "per-operation middleware in Echo" | Re-trigger Greptile

@mromaszewicz

Copy link
Copy Markdown
Member Author

Greptile Summary

This PR implements per-operation middleware support for both Echo v4 and Echo v5 backends by introducing RegisterHandlersWithOptions (with a RegisterHandlersOptions struct containing BaseURL and OperationMiddlewares) and refactoring the existing RegisterHandlers/RegisterHandlersWithBaseURL helpers to delegate to it. The change is backward-compatible, correctly uses the raw spec operationId as the map key (with a normalized-identifier fallback when the spec omits one), and includes a solid regression test in internal/test/issues/issue-518/.

* `examples/petstore-expanded/echo/api/petstore-server.gen.go` contains unrelated parameter-binding changes (`BindQueryParameter` → `BindQueryParameterWithOptions` with added `Type`/`Format` fields; same for `BindStyledParameterWithOptions`) that do not appear in the echo-v5 equivalent — this file appears to have been regenerated with a toolchain that includes additional unreleased changes, and will likely cause a CI diff failure.

Confidence Score: 3/5

Mostly safe to merge, but the unrelated binding-change drift in the echo/v4 petstore example file needs to be resolved first.

The core template change and regression test are well-implemented, but a P1 finding — unrelated parameter-binding changes in examples/petstore-expanded/echo/api/petstore-server.gen.go absent from the echo-v5 counterpart — indicates the file was regenerated with a different toolchain and will likely cause a CI make generate diff failure.

examples/petstore-expanded/echo/api/petstore-server.gen.go — contains binding-call changes unrelated to this PR that are not present in the echo-v5 equivalent.

Important Files Changed

Comments Outside Diff (1)

1. `examples/petstore-expanded/echo/api/petstore-server.gen.go`, line 63-73 ([link](https://github.com/oapi-codegen/oapi-codegen/blob/f4a74a25e165303380af81ef55b6f54d17b8c948/examples/petstore-expanded/echo/api/petstore-server.gen.go#L63-L73))
   [![P1](https://camo.githubusercontent.com/4e5a362f3ef5d2d58ede5ed80de15a55078df1b6f7dec5850902b2cf5bce3eb8/68747470733a2f2f6772657074696c652d7374617469632d6173736574732e73332e616d617a6f6e6177732e636f6d2f6261646765732f70312e7376673f763d37)](#) **Unrelated drift in regenerated file**
   `examples/petstore-expanded/echo/api/petstore-server.gen.go` contains parameter-binding changes unrelated to this PR's stated goal of per-operation middleware. Specifically, calls to `BindQueryParameter` have been replaced with `BindQueryParameterWithOptions` (gaining `Type`/`Format` fields), and `BindStyledParameterWithOptions` call sites gain the same extra fields. These changes do not appear in the otherwise-analogous echo-v5 petstore file, suggesting this file was regenerated with a local toolchain that includes additional unreleased changes. Per the project policy, CI fails when `make generate` produces a diff — if the intended generator does not yet emit these binding changes, CI will fail on this file.

Reviews (1): Last reviewed commit: "per-operation middleware in Echo" | Re-trigger Greptile

@greptileai - this is because for some reason, the petstore boilerplate hasn't successfully regenerated into a while. Our "make generate" target bypasses it. I will fix that in a separate commit. This is correct.

@greptile-apps

greptile-apps Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Got it, thanks for the context. If the petstore example is intentionally excluded from make generate and will be fixed separately, the drift is expected and not a blocker for this PR.

Tip: You can give me more context with .greptile/rules.md so I get this right next time.

@mromaszewicz mromaszewicz merged commit 036a54b into oapi-codegen:main Apr 30, 2026
20 checks passed
@mromaszewicz mromaszewicz deleted the fix/issue-518 branch April 30, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request server:echo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using per-endpoint middleware

1 participant