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
OpenAPI and SwaggerUI support #717
Comments
Hi @jylin We do not have a specific plan at this time, but I think we can do it. We could refer to "itty-router-openapi". |
I'd like to bump this suggestion. Elysiajs has support for swagger now and its great <3 |
Yes, I'd like to get started after a while of releasing v3. Or someone could implement it... |
Would this be contract-first or code-first? |
Code-first and auto-generating openapi schema from that code |
Mentioned in Discord as well, but this library will likely be helpful. https://github.com/asteasolutions/zod-to-openapi |
What's new in this issue? |
@yusukebe is this on the making? |
Yeah, I'm making one of the implementations for OpenAPI. That is using the zod-to-openapi, and it will exntend Hono class. After creating the POC, I'll make the project in the middleware repo. Note, it's one of the ways. So, anyone can create other OpenAPI implementations. |
Thanks keep up the good work, discovered this and now Im using it more than expressjs |
Hi everyone! I've released Zod OpenAPI Hono: https://github.com/honojs/middleware/tree/main/packages/zod-openapi It's not fully documented, but you can try it! |
Is it possible not writing a path, like it is based on the defined routes, we just need to write zod schema? |
Hmm. Have you ever tried it? I think the Zod OpenAPI Hono has same feature with other framework/libraries. |
I havent tried it yet but read the docs, I was thinking if it would be possible to do it like this? Just one helper to generate everything. const app = new Hono();
app.get("*", prettyJSON());
app.route("/teachers", teacherController);
const openApiDocument = generateOpenApi(app, {
info: {
title: 'Posts API',
version: '1.0.0',
},
}); |
I got it so this is a real hono server? import { OpenAPIHono } from '@hono/zod-openapi'
import { createRoute } from '@hono/zod-openapi'
const app = new OpenAPIHono() //this is a real server not just a document builder?
const route = createRoute({
method: 'get',
path: '/users/:id',
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Get the user',
},
},
})
app.openapi(route, (c) => {
const { id } = c.req.valid('param')
return c.jsonT({
id,
age: 20,
name: 'Ultra-man',
})
})
// OpenAPI document will be served on /doc
app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
}) |
Yes. As same as Hono. You can do: export default app |
I think this limitation is really big, how I suppose to structure a real project? putting all the routes in the main file?
|
Hi @hectorAguero, You're absolutely right; it's a significant feature. However, for now, you can make use of the const app = new Hono()
app.use('*', cors())
app.mount('/api', api.fetch)
export default app Could you give this a try and let me know your thoughts? |
@yusukebe I tried now, I coudn't get the I tried with the updated readme implementation, but in that case between // localhost:3000/doc return a 404 Not Found
app.mount("/users", usersApi.fetch);
app.mount("/posts", postsApi.fetch); Btw I also tried mounting like this, but mount only work the first time, I guess because it was the same path // Need to set the full path inside each api
app.mount("/api", usersApi.fetch);
app.mount("/api", postsApi.fetch); // this didn't got mounted posts/index.ts basePath('/posts') not working // const postsApi = new OpenAPIHono().basePath('/posts');
const postsApi = new OpenAPIHono();
...
export default postsApi; I would like to see an example using the https://jsonplaceholder.typicode.com/ or similar |
The documentation is not accessible via Also, you shouldn't structure your code like this: app.mount("/api", usersApi.fetch);
app.mount("/api", postsApi.fetch); Instead, use the following: app.mount("/api/users", usersApi.fetch);
app.mount("/api/posts", postsApi.fetch); |
So I found my problem, it was the paramsSchema that I set for the requests params in the routes And because "doc" wasn't parsable number, zod return an error, I move the |
Hono released Third party support for Swagger UI just yesterday. |
As @sor4chi mentioned, Hono now supports both OpenAPI and SwaggerUI. So, I'll close this issue. |
Is it a possibility to add support for OpenAPI and be able to use the Swagger UI?
The text was updated successfully, but these errors were encountered: