-
Notifications
You must be signed in to change notification settings - Fork 41
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
Swagger UI example request not being properly executed if mounted at /some/path #150
Comments
This happens due to an implementation detail on Honojs (docs here), basically they "manipulate" the request url before calling the inner mounted router, for the inner router to think they are at the root of the request. TLDR: I will think in a way to fix this issue, but currently the workaround is to disable the docs_url option, and to build your own docs page pointing to the correct path, then telling openapi that all routes should be prefixed with the base url. Here's a working example: import { OpenAPIRouter, getSwaggerUI } from '@cloudflare/itty-router-openapi'
import { Hono } from 'hono'
const router = OpenAPIRouter({
docs_url: null,
schema: {
servers: [{
url: '/api',
}],
},
})
router.get('/test', (request) => new Response(request.url))
router.original.get('/docs', () => {
return new Response(
getSwaggerUI('/api/openapi.json'),
{
headers: {
'content-type': 'text/html; charset=UTF-8',
},
status: 200,
},
)
})
const app = new Hono()
app.mount('/api', router.handle)
export default app |
Just as additional info. It's possible to set basepath to |
Hello @carafelix in the latest version of Hono this issue is solved, all you need to do is set the
Here is a full working example running in Hono v4.4.3 import { OpenAPIRoute, OpenAPIRouter } from '@cloudflare/itty-router-openapi'
import { Hono } from 'hono'
export class ToDoCreate extends OpenAPIRoute {
static schema = {
tags: ['ToDo'],
summary: 'Create a ToDo',
}
async handle(
request: Request,
env: any,
context: any,
data: any,
) {
return Response.json({ success: true })
}
}
const router = OpenAPIRouter({
base: '/api',
})
router.get('/example', ToDoCreate)
const app = new Hono()
app.mount('/api', router.handle, {
replaceRequest: (req) => req,
})
export default app Then openning the browser at |
Wow that was fast! Thank you very much. |
I'm already working on a very big revamp for this library, that will make it "router" agnostic. |
I'm planning on releasing this new revamp version in the next weeks, so after that i'm happy to review and discuss additional changes 😄 |
If, for example, you mount itty-router-openapi as a child endpoint inside a hono application, for e.g:
Request to the
/api/example
would succeed, but running the examples from the swagger ui mounted at/api/doc
would result in misrouted errors.Adding
api
as basepath to the OpenAPIRouter would only trail the error.A solution could be to add a
execution_basePath
property likeOfc this can be worked around by mounting on
/
and adding the/api
as a basepath in the itty router, but not as ideomaticThe text was updated successfully, but these errors were encountered: