Repository navigation
fix(zod-openapi): allow an async hook to return nothing on success - #2276
Merged
yusukebe merged 1 commit intoNov 26, 2026
Merged
Conversation
The hook of `app.openapi()` was typed as `MaybePromise<TypedResponse> | undefined`, so an async hook that only returns a response on failure (a `Promise<TypedResponse | undefined>`) or never returns (a `Promise<void>`) was a type error, although it works at runtime. The same applies to the hook of `defineOpenAPIRoute()` and to the `RouteHook` type. Fixes honojs#1479 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 463a049 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 #2276 +/- ##
=======================================
Coverage 92.74% 92.74%
=======================================
Files 119 119
Lines 4385 4385
Branches 1152 1153 +1
=======================================
Hits 4067 4067
Misses 282 282
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:
|
Member
|
@shivamlaur022 Thanks! |
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.
Fixes #1479
Problem
The validation hook of
app.openapi()is typed to returnMaybePromise<TypedResponse> | undefined. A sync hook can returnundefined, but the Promise branch cannot, so an async hook that only returns a response on failure is a type error even though it works at runtime (returning nothing lets the handler run):An async hook that never returns (
Promise<void>) fails the same way, and so does thehookofdefineOpenAPIRoute()and an async function annotated asRouteHook.Changes
packages/zod-openapi/src/index.ts: add an internalHookResponse<T> = MaybePromise<Awaited<T> | void>and use it for the hook ofapp.openapi()anddefineOpenAPIRoute()(HookFromRoute), so an async hook may resolve toundefined.RouteHookkeeps the responses it allowed before and additionally accepts aPromisethat resolves to one of them orundefined.packages/zod-openapi/src/index.test-d.ts: type tests for sync/async hooks that return only on failure or never return,defineOpenAPIRoute,RouteHook, a route without content, and that a response not defined in the route (422) is still rejected.@hono/zod-openapi(patch).Verification
main(5 of 21) and pass with the change.pnpm testinpackages/zod-openapi: 152 tests pass, no type errors.tsc -bandeslintpass (no new lint errors).Notes
RouteHookwas left otherwise unchanged to avoid a breaking change. It still allows anyResponse, so aRouteHookfor a route with typed responses is not assignable to the hook ofapp.openapi(); aligning it withRouteHandlercould be a follow-up.The author should do the following, if applicable
pnpm changesetat the top of this repo and push the changeset🤖 Generated with Claude Code