Skip to content

fix(zod-openapi): infer route middleware env in defineOpenAPIRoute handler#1986

Open
VihaanAgarwal wants to merge 1 commit into
honojs:mainfrom
VihaanAgarwal:fix/zod-openapi-define-route-middleware-env
Open

fix(zod-openapi): infer route middleware env in defineOpenAPIRoute handler#1986
VihaanAgarwal wants to merge 1 commit into
honojs:mainfrom
VihaanAgarwal:fix/zod-openapi-define-route-middleware-env

Conversation

@VihaanAgarwal

Copy link
Copy Markdown

What & why

defineOpenAPIRoute (added in #1752) types its handler with only the app Env, so variables set by route-level middleware are dropped. Inside the handler c.var.* falls back to any:

const requireIdentity = createMiddleware<{
  Variables: { identity: { id: string } }
}>(async (c, next) => {
  c.set('identity', { id: '123' })
  await next()
})

const route = defineOpenAPIRoute({
  route: createRoute({
    method: 'get',
    path: '/me',
    middleware: [requireIdentity] as const,
    responses: { 200: { description: 'ok' } },
  }),
  handler: (c) => {
    c.var.identity // typed as `any`, should be `{ id: string }`
    return c.text('ok')
  },
})

OpenAPIHono.openapi() already covers this case: when a route has middleware it widens the handler env to RouteMiddlewareParams<R>['env'] & E. HandlerFromRoute, which defineOpenAPIRoute relies on, only passed E. This change applies the same widening in HandlerFromRoute, so a route built through defineOpenAPIRoute infers the middleware variables.

Thanks to @Aros for the report and for pointing at the existing openapi() handling.

Test

Added a type test in index.test-d.ts, mirroring the existing openapi() middleware-env test, that asserts the handler's c.var.foo / c.var.bar resolve to the route middleware variable types. It fails on main (Property 'foo' does not exist on type ...) and passes with the fix.

Fixes #1879

The author should do the following, if applicable

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

…ndler

defineOpenAPIRoute typed its handler with only the app Env, so variables
set by route-level middleware were dropped and c.var.* fell back to any.
openapi() already widens the handler env with the middleware env when a
route has middleware; HandlerFromRoute now does the same, so a route built
through defineOpenAPIRoute infers those variables too.
@changeset-bot

changeset-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a14bd27

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 Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.05%. Comparing base (a401fee) to head (a14bd27).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1986   +/-   ##
=======================================
  Coverage   92.05%   92.05%           
=======================================
  Files         115      115           
  Lines        4065     4065           
  Branches     1059     1059           
=======================================
  Hits         3742     3742           
  Misses        287      287           
  Partials       36       36           
Flag Coverage Δ
zod-openapi 94.90% <ø> (ø)

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.

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.

[zod-openapi] defineOpenAPIRoute does not infer handler Context variables from route middleware

1 participant