fix(hono): preserve symbol-keyed metadata on wrapped middleware handlers#20953
Open
ahmadio wants to merge 1 commit into
Open
fix(hono): preserve symbol-keyed metadata on wrapped middleware handlers#20953ahmadio wants to merge 1 commit into
ahmadio wants to merge 1 commit into
Conversation
`wrapMiddlewareWithSpan` returned a new closure without copying own-symbol
properties from the original handler, so frameworks that attach metadata via
`Symbol` keys (e.g. `hono-openapi`'s `Symbol("openapi")` set by `describeRoute`)
lost it as soon as Sentry wrapped the handler. Downstream consumers that walk
`app.routes[i].handler` then saw no metadata, producing empty OpenAPI specs.
Use `Object.defineProperty` with the source descriptor so we set an own
property on the wrapper without tripping over read-only well-known symbols on
the function's prototype (e.g. `Symbol.toStringTag` on `AsyncFunction`).
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
@sentry/hono'swrapMiddlewareWithSpanreplaces a Hono middleware handler with asentryTracedMiddlewareclosure but does not copy own-symbol properties from the original handler. Frameworks that attach metadata to a handler via aSymbolkey (the immediate motivating case ishono-openapi'sdescribeRoute, which setsSymbol("openapi")) lose that metadata at the point Sentry wraps the handler.Downstream effect for
rhinobase/hono-openapi:generateSpecs(app)walksapp.routes, findssentryTracedMiddlewareclosures with noSymbol("openapi")set, and returns{ paths: {} }. The OpenAPI JSON / Scalar UI renders empty.markFunctionWrapped(wrapped, handler)already sets__sentry_original__so the wrapper is unwrappable in principle, but the wrapper itself doesn't carry the framework's metadata, which is what every other consumer reads.Reproduction (before this fix)
Change
Copy own-symbol properties from
handlertowrappedaftermarkFunctionWrapped, viaObject.definePropertywith the source descriptor:Notes on the choice:
displayNamecould collide with intentional fields on the wrapper, and no framework I'm aware of attaches handler metadata via string keys. Happy to expand if maintainers prefer.Object.definePropertyrather than direct assignment so we set an own property on the wrapper instead of going through the prototype's accessors. This avoidsTypeError: Cannot assign to read only propertyon well-known symbols inherited fromAsyncFunction.prototype(e.g.Symbol.toStringTag), which surface in practice on mocked handlers and Proxy-based handlers.Test plan
packages/hono/test/shared/patchAppUse.test.ts: registers a handler carryingSymbol("test-meta")viaapp.use('/test', handler)and asserts the symbol is readable onapp.routes[i].handlerafter wrapping.yarn testinpackages/honopasses (109/109)yarn build:dev:filter @sentry/honosucceedsyarn format:checkcleanReproduced against
@sentry/hono: 10.53.1@sentry/node: 10.53.1hono: 4.12.18 / 4.12.19Notes
Related issue surface but distinct from #20449 (route groups dropping
app.usepatches). This one is about the patchedapp.usesucceeding but the wrapper losing handler-level symbol metadata.