Skip to content

fix: run middleware added by group modifiers - #1092

Open
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/804-group-modifier-middleware
Open

fix: run middleware added by group modifiers#1092
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/804-group-modifier-middleware

Conversation

@stareezy-1

@stareezy-1 stareezy-1 commented Aug 5, 2026

Copy link
Copy Markdown

Summary

Fixes #804

Problem

Middleware appended to op.Middlewares inside a group modifier never runs at request time — even though it does appear in the generated OpenAPI document.

Root cause: PrefixModifier copies the operation for fan-out (so multiple prefixes can be documented and registered), which means operation modifiers mutate a copy. huma.Register builds the runtime handler chain from the original operation's op.Middlewares:

a.Handle(&op, api.Middlewares().Handler(op.Middlewares.Handler(...)))

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 UseSimpleModifier appends a middleware, send a request — the middleware never runs.

Fix

  • Register (group path): skip the op.Middlewares pre-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's op.Middlewares before 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

TestGroupModifierMiddlewareRuns registers an operation in a group whose modifier appends a middleware setting a response header, then asserts:

Assertion Verifies
Request returns 200 handler still executes
X-Modifier-Middleware: ran header present modifier middleware actually ran

All existing tests continue to pass (go test -race ./...).

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.
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.

[Group] Use modifier in group to add middleware not working

1 participant