New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type inference not working with zod-openapi and path parameters #2525
Comments
The issue was here: createRoute({
path: "/{id}",
method: "get",
request: {
params: z.object({
id: z.number().int().positive(), // <--
}),
}, Apparently params must be of type string. Maybe this could create type error somewhere? |
You could use
|
hi @stabildev I must admit I found some issue with my suggested approach as well yesterday evening. The earlier mentioned code:
seems correct: however the generated OpenAPI spec looked like:
note the the type being I solved it with:
so first I preprocess the received value, I manually convert it to number, and if that's not possible I return a
PS: I'm not experiencing any slow type interference.. which IDE are you using? |
I'm using VS Code. Are you using the Hono client? My inference chain looks like this (in this example for the
|
I'm also using VC Code, but my chain is different. I'm using Prisma. |
@stabildev cool we are using almost the same setup! I also have a bit slow type interference - but still usable - and I think its on the radar of the main developer. We split our api in sections, I might help?
you can see how we split our api here: https://github.com/cellajs/cella/blob/development/backend/src/server.ts that said, the type performance I think is an issue. I think we need an typescript performance expert to somehow look into it. Would be interested in sponsoring that partially. |
I was struggling with this as well, however I found a different workaround using with z.string()
.optional()
.pipe(z.coerce.number().min(0).default(0))
.openapi({ type: "integer" }), This way you can still basically use your current schemas and The caveat is that the RPC client typing is still I'm not sure if this helps with type inference times, but it's worth a try. |
What version of Hono are you using?
4.2.5
What runtime/platform is your app running on?
Wrangler
What steps can reproduce the bug?
Using path parameters (e. g.
GET /customers/:id
) with@hono/zod-openapi
breaks type inference for the complete route.This renders the Hono Client unusable with zod-openapi.
The following is a simplified example with only one route. If this is combined with another route, e. g.
GET /customers
(without parameter), type inference will break for this endpoint as well.What is the expected behavior?
Type inference should return the correct response type:
What do you see instead?
The response type is inferred as
any
:Additional information
Working equivalent example without
@hono/zod-openapi
:Result:
The pattern works for routes without path parameters.
The text was updated successfully, but these errors were encountered: