fix(zod-openapi): preserve onError/notFound bindings set after basePath()#2031
Merged
yusukebe merged 2 commits intoJul 14, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: b3b6619 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2031 +/- ##
==========================================
+ Coverage 92.07% 92.08% +0.01%
==========================================
Files 115 115
Lines 4074 4081 +7
Branches 1063 1063
==========================================
+ Hits 3751 3758 +7
Misses 287 287
Partials 36 36
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jul 14, 2026
Merged
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.
Issue: #2021
Summary
OpenAPIHono.basePath()dropped any.onError()/.notFound()registered on the returned instanceafter the
basePath()call. The handler silently had no effect, and the parent app's handlerran instead. This surfaced while migrating a plain Hono app to OpenAPIHono: a route()-mounted
sub-app quietly changed which onError handled its thrown errors.
Reproduction
Root cause
OpenAPIHono.basePath()rebuilt the instance by spreading the plain Hono clone returned bysuper.basePath():onError,notFound,request, andfetchare arrow-function instance fields, so they are own-enumerable properties permanently bound to the instance they were created on.super.basePath()returns a throwaway clone, and spreading it copies those clone-bound methodsinto the constructor options — where Object.assign(this, options) then overwrites the freshly constructed (and correctly bound) methods on the returned instance.
As a result,
sub.onError(h)mutated errorHandler on the discarded clone, not on the returnedsub (sub.errorHandler stayed the built-in default). Route registration only appeared to work
because the returned instance and the clone share the same underlying router / routes. When
parent.route('/', sub) reads sub.errorHandler as a plain property, it still sees the default,
so it does not wrap the sub-app's handlers — and the parent's onError ends up handling the thrown error.
Fix
Construct a properly initialized OpenAPIHono (so its own arrow-field methods stay bound to it) and transplant only the routing state — router, routes, getPath, errorHandler, _basePath — from the clone, instead of spreading the clone into the constructor. Carrying errorHandler over preserves the "onError() before basePath()" ordering that core Hono's clone already captured.
Tests
Verified locally: full @hono/zod-openapi suite passes (81 tests, type-check clean), ESLint clean, Prettier clean.
The author should do the following, if applicable
pnpm changesetat the top of this repo and push the changeset