Skip to content

fix(zod-openapi): allow an async hook to return nothing on success - #2276

Merged
yusukebe merged 1 commit into
honojs:mainfrom
shivamlaur022:fix/1479-async-hook-return-type
Nov 26, 2026
Merged

yusukebe merged 1 commit into
honojs:mainfrom
shivamlaur022:fix/1479-async-hook-return-type

Conversation

@shivamlaur022

Copy link
Copy Markdown
Contributor

Fixes #1479

Problem

The validation hook of app.openapi() is typed to return MaybePromise<TypedResponse> | undefined. A sync hook can return undefined, 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):

app.openapi(route, handler, async (result, c) => {
  if (!result.success) {
    return c.json({ ok: false }, 400)
  }
})
// Type 'Promise<JSONRespondReturn<..., 400> | undefined>' is not assignable to ...
//   Type 'undefined' is not assignable to type 'RouteConfigToTypedResponse<...>'

An async hook that never returns (Promise<void>) fails the same way, and so does the hook of defineOpenAPIRoute() and an async function annotated as RouteHook.

Changes

  • packages/zod-openapi/src/index.ts: add an internal HookResponse<T> = MaybePromise<Awaited<T> | void> and use it for the hook of app.openapi() and defineOpenAPIRoute() (HookFromRoute), so an async hook may resolve to undefined. RouteHook keeps the responses it allowed before and additionally accepts a Promise that resolves to one of them or undefined.
  • 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.
  • A changeset for @hono/zod-openapi (patch).

Verification

  • The new type tests fail on main (5 of 21) and pass with the change.
  • pnpm test in packages/zod-openapi: 152 tests pass, no type errors.
  • tsc -b and eslint pass (no new lint errors).

Notes

  • RouteHook was left otherwise unchanged to avoid a breaking change. It still allows any Response, so a RouteHook for a route with typed responses is not assignable to the hook of app.openapi(); aligning it with RouteHandler could be a follow-up.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • pnpm changeset at the top of this repo and push the changeset
  • Follow the contribution guide

🤖 Generated with Claude Code

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

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 463a049

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/zod-openapi Patch

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

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.74%. Comparing base (48eaec8) to head (463a049).
⚠️ Report is 11 commits behind head on main.

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           
Flag Coverage Δ
arktype-validator 93.54% <ø> (ø)
capnweb 60.71% <ø> (ø)
cloudflare-access 100.00% <ø> (ø)
mcp 91.10% <ø> (ø)
otel 95.20% <ø> (ø)
ssg-plugins-essential 98.16% <ø> (ø)
standard-validator 96.07% <ø> (ø)
swagger-ui 95.45% <ø> (ø)
typedriver-validator 100.00% <ø> (ø)
zod-openapi 95.11% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

Copy link
Copy Markdown
Member

@shivamlaur022 Thanks!

@yusukebe
yusukebe merged commit 32f50fa into honojs:main Nov 26, 2026
107 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 26, 2026
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.

Typescript or docs error in openapi() hook argument

2 participants