fix(zod-openapi): replace path param strings correctly in basePath (#992)

* fix(zod-openapi): replace path param strings correctly in basePath

* add changeset
pull/993/head
Yusuke Wada 2025-03-02 17:48:35 +09:00 committed by GitHub
parent 50936f9967
commit 3c738f5ea4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: replace path param strings correctly in basePath

View File

@ -701,7 +701,7 @@ function addBasePathToDocument(document: Record<string, any>, basePath: string)
const updatedPaths: Record<string, any> = {} const updatedPaths: Record<string, any> = {}
Object.keys(document.paths).forEach((path) => { Object.keys(document.paths).forEach((path) => {
updatedPaths[mergePath(basePath, path)] = document.paths[path] updatedPaths[mergePath(basePath.replaceAll(/:([^\/]+)/g, '{$1}'), path)] = document.paths[path]
}) })
return { return {

View File

@ -1239,6 +1239,41 @@ describe('basePath()', () => {
expect(paths).not.toStrictEqual(['/message1', '/message2', '/hello']) 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', () => { describe('With hc', () => {