fix: run middleware added by group modifiers - #1092
Open
stareezy-1 wants to merge 1 commit into
Open
Conversation
PrefixModifier copies the operation for fan-out, so operation modifiers mutate a copy while the runtime handler chain was built from the original operation's middlewares. Modifier-added middleware therefore never ran, even though it appeared in the OpenAPI document. Wrap the final post-modifier operation's middlewares in the group adapter instead, and skip the pre-wrap for groups so directly-set operation middlewares do not run twice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #804
Problem
Middleware appended to
op.Middlewaresinside a group modifier never runs at request time — even though it does appear in the generated OpenAPI document.Root cause:
PrefixModifiercopies the operation for fan-out (so multiple prefixes can be documented and registered), which means operation modifiers mutate a copy.huma.Registerbuilds the runtime handler chain from the original operation'sop.Middlewares:The original op never sees the modifier's changes, so the middleware is missing from the chain. Tags/Security/Errors worked because they only matter for the document, which uses the modified copy.
Reproduction: register an operation on a group whose
UseSimpleModifierappends a middleware, send a request — the middleware never runs.Fix
Register(group path): skip theop.Middlewarespre-wrap — the original op's list is stale by definition; the group adapter handles it.groupAdapter.Handle: wrap the handler with the final post-modifier operation'sop.Middlewaresbefore delegating to the underlying adapter.This runs modifier-added middleware exactly once and avoids double-running directly-set operation middlewares (which would happen if both the original and the copy were wrapped).
Tests
TestGroupModifierMiddlewareRunsregisters an operation in a group whose modifier appends a middleware setting a response header, then asserts:X-Modifier-Middleware: ranheader presentAll existing tests continue to pass (
go test -race ./...).