fix(zod-openapi): replace path param strings correctly in basePath (#992)
* fix(zod-openapi): replace path param strings correctly in basePath * add changesetpull/993/head
parent
50936f9967
commit
3c738f5ea4
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/zod-openapi': patch
|
||||
---
|
||||
|
||||
fix: replace path param strings correctly in basePath
|
|
@ -701,7 +701,7 @@ function addBasePathToDocument(document: Record<string, any>, basePath: string)
|
|||
const updatedPaths: Record<string, any> = {}
|
||||
|
||||
Object.keys(document.paths).forEach((path) => {
|
||||
updatedPaths[mergePath(basePath, path)] = document.paths[path]
|
||||
updatedPaths[mergePath(basePath.replaceAll(/:([^\/]+)/g, '{$1}'), path)] = document.paths[path]
|
||||
})
|
||||
|
||||
return {
|
||||
|
|
|
@ -1239,6 +1239,41 @@ describe('basePath()', () => {
|
|||
|
||||
expect(paths).not.toStrictEqual(['/message1', '/message2', '/hello'])
|
||||
})
|
||||
|
||||
it('Should correctly handle path parameters in basePath', async () => {
|
||||
const app = new OpenAPIHono().basePath('/:param')
|
||||
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: 'get',
|
||||
path: '/',
|
||||
responses: {
|
||||
200: {
|
||||
description: 'Get message',
|
||||
},
|
||||
},
|
||||
}),
|
||||
(c) => {
|
||||
return c.json({ path: c.req.param('param') })
|
||||
}
|
||||
)
|
||||
|
||||
const json = app.getOpenAPIDocument({
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
version: '1.0.0',
|
||||
title: 'My API',
|
||||
},
|
||||
})
|
||||
|
||||
const paths = Object.keys(json.paths)
|
||||
|
||||
expect(paths).toStrictEqual(['/{param}'])
|
||||
|
||||
const res = await app.request('/abc')
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.json()).toEqual({ path: 'abc' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('With hc', () => {
|
||||
|
|
Loading…
Reference in New Issue