-
Notifications
You must be signed in to change notification settings - Fork 97
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
[zod-openapi] Problem with strict type checking of return values #374
Comments
Indeed, only Since const route = createRoute({
method: 'get',
path: '/hello',
responses: {
200: {
description: 'Say hello'
}
}
})
app.openapi(route, (c) => {
return c.text('hello')
}) |
Hi @yusukebe Thanks for the quick reply. I think it's a bit of a pity that it can't be declared in such a way as to explicitly and precisely specify the returned Content-Type (at least) and some example of the returned response in dedicated fields. Some simple example: to describe an endpoint that returns the exact API version, I have to describe it like this: responses: {
200: {
description: 'Returns string (`text/plain` body) with API version number, e.g. `API v1.0.0`.',
},
} Instead of, e.g.: responses: {
200: {
description: 'Returns string with API version number.',
content: {
'text/plain': {
schema: z.string().openapi({ example: 'API v1.0.0' }),
},
},
},
} Better example: if the same endpoint can return data in YAML and JSON format for different status codes, then response for YAML declared only using |
I understand what you want to do. Thanks for the explanation. Certainly it would be better to be able to specify content other than JSON. However, since shema supports only JSON, it would be better to make schema optional or undefined for const route = createRoute({
method: 'get',
path: '/info',
responses: {
200: {
description: 'OK',
content: {
'text/plain': {
example: 'API v1.0.0',
schema: undefined
}
}
}
}
}) This feature can be added. |
Yes exactly, I think it would be really nice to add such an option. |
Thanks! I'll work on it later. |
Strict type checking was added in version 0.9.1 in such a way that if response type is declared, the handler of a given route can only return a response of the
TypedResponse
type, and of the methods available in Hono Context, only the.json()
(and deprecated.jsonT()
) method returns such a type, which forces that only JSON can be returned from this endpoint.It's about this piece of code:
As a result, if I want to have a route that returns text or e.g. YAML, TS will report an error because the methods
Context.text()
,Context.body()
etc. return theResponse
type, butTypedResponse
is expected.The text was updated successfully, but these errors were encountered: