c.json() does not enforce OpenAPI return type
Hello there!
I'm playing around Hono and RPC, and it works great, but for some reason the return type of the handler function is not enforced by the OpenAPI spec that is provided to it. I have searched on github issues and in here, but did not find anyone with the same problem.
It might just be that it's not possible at the moment to do this automatically, but rather I have to type the
c.json<ReturnType, 200>()
myself
Here's a demo:
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYygUwIYzQJQgV2wBo4AvEgeTDQDsBBABQEkAJCGiOAXzgDMoIIOAHIAAgAt2EAPSkIAEwC0EajQxhgwgNwAoUJFiI445Nz4ChwyR2nIANsFoxhOncnYBneAMJoAImi8wDTAMMDscAC8KOhYuATYABQIOnBpcCBoMJLyAFwikF7CcBgeKJ4wRKnpYFji+cLS6ADmwF5oUMJV6XDoAI74aF75KT09AEYKAJ4j1WNj7jTYS7Pza+nC6mAOyFjhNNIAVh7swqvrF2keyOJoIBj5pAB0EOOHaMgwyXOXv-geHTUmUeTy8UGCzUSAEoniBgokAKxQ7q-VG1DweADu0DyZFBMHBNEhMLhNESAA4oT9UT0uFSaekuNS1kz1qzGSi0ugPJAaACPOd5gAmAAMIsFF0WyxgEtRmzA22AuzC7COJxoZ0QzIZ11u9xBr3en2+DMuMAgAGtaCCwRDodqaXSHWznRzXWl5ENUMAwCqaA0AMr4ZDIIYeXj4Ox2Ka9NCtdroeRPFxszljAAsIoAnCM4J7ruDffsGgBVAFQEp2WLyGNoAAebRgHmT3DTPQRYtz+e9RfYpZo9eonzQ8jgHQEFYgIfwiZb7LSTLpujcFRKCqicAHmLgVFojFYUntPReqnUwESPmwASCIT9JESyChUQAfFr1tJpHAVLQz7GYDOaDgGApmoOA2k3CB4HGNAIRQW5kCtUdbnQB0PzAuBsUjUcHCtIDxHA80-wAkpAPHaA4Gg3Z-jQMDnDKDh4GyGiPAwTISjKJjYx5TwaM9G9Qn2B10H-KBAOQJ5jnYZIIJlEROOA0CMEmAA3NBii4EhRRFKFdFpHTXB0BSaLoBUABUQJo6IjIgXg1zAXRFi8FAHCcDcTAAHhMsBzOoZ9EisGAYDAXIPzsKcMDsSRhnJAB2WLpGEKkVz5bwhl5AENwwTEMFCZzHCWJ4WkbDongAEkKL4kEk-0jGo0TWLQBpqK6OB0SxHEGja9TuH04BbIvNKeJeC0n1GdJHPgeQsAwTLsty7l0rQCT1WhXT0jQnoAD0AH4fjQqaYBm8CME+fAIujIxzStGrbSJbg9s-cZCDw3jpqI0SRzA+jIJ0LggA
Here's a quick snippet:
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
7 Replies
Interesting! I will also like to have this in hono. Let me check on this, have you created an issue for this?
Thank you for your response! No I haven't 😄
Okay, let me try this out. If I got it working then we can create an issue and merge it. I will add the issue here once I am there
Sure thing! Thanks a lot!
I tried looking into it myself. What I could deduct is that the type
RouteConfigToTypedResponse
works, but I did not get much further, to why it's not being inferred later on to the c.json() call:Okay, I will look into this also. Thank You
It looks like if I change
[statusCode: number]
to [statusCode: StatusCode]
here RouteHandler's type is correct, otherwise RouteTestReponse is any:GitHub
middleware/packages/zod-openapi/src/index.ts at 3d83b7a36fcfbe1c069...
monorepo for Hono third-party middleware/helpers/wrappers - honojs/middleware
but it still isn't being used by c.json