fix(zod-openapi): infer route middleware env in defineOpenAPIRoute handler#1986
Open
VihaanAgarwal wants to merge 1 commit into
Open
fix(zod-openapi): infer route middleware env in defineOpenAPIRoute handler#1986VihaanAgarwal wants to merge 1 commit into
VihaanAgarwal wants to merge 1 commit into
Conversation
…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 detectedLatest commit: a14bd27 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 #1986 +/- ##
=======================================
Coverage 92.05% 92.05%
=======================================
Files 115 115
Lines 4065 4065
Branches 1059 1059
=======================================
Hits 3742 3742
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 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.
What & why
defineOpenAPIRoute(added in #1752) types itshandlerwith only the appEnv, so variables set by route-levelmiddlewareare dropped. Inside the handlerc.var.*falls back toany:OpenAPIHono.openapi()already covers this case: when a route has middleware it widens the handler env toRouteMiddlewareParams<R>['env'] & E.HandlerFromRoute, whichdefineOpenAPIRouterelies on, only passedE. This change applies the same widening inHandlerFromRoute, so a route built throughdefineOpenAPIRouteinfers 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 existingopenapi()middleware-env test, that asserts the handler'sc.var.foo/c.var.barresolve to the route middleware variable types. It fails onmain(Property 'foo' does not exist on type ...) and passes with the fix.Fixes #1879
The author should do the following, if applicable
yarn changesetat the top of this repo and push the changeset