fix(zod-openapi): handle `Conflicting names for parameter` (#356)

* fix(zod-openapi): handle `Conflicting names for parameter`

* add changeset
pull/357/head
Yusuke Wada 2024-01-25 08:36:58 +09:00 committed by GitHub
parent 6bf91e1908
commit 168a0a6d68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 88 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix: handle `Conflicting names for parameter`

View File

@ -40,4 +40,4 @@
"tsup": "^8.0.1",
"vitest": "^1.0.4"
}
}
}

View File

@ -321,8 +321,12 @@ export class OpenAPIHono<
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
return this.get(path, (c) => {
const config = typeof configure === 'function' ? configure(c) : configure
const document = this.getOpenAPIDocument(config)
return c.json(document)
try {
const document = this.getOpenAPIDocument(config)
return c.json(document)
} catch (e) {
return c.json(e, 500)
}
}) as any
}
@ -332,8 +336,12 @@ export class OpenAPIHono<
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
return this.get(path, (c) => {
const config = typeof configure === 'function' ? configure(c) : configure
const document = this.getOpenAPI31Document(config)
return c.json(document)
try {
const document = this.getOpenAPI31Document(config)
return c.json(document)
} catch (e) {
return c.json(e, 500)
}
}) as any
}

View File

@ -1274,3 +1274,71 @@ describe('Named params in nested routes', () => {
expect(Object.keys(data['paths'])[0]).toBe('/root/{rootId}/sub/{subId}')
})
})
describe('Handle "Conflicting names for parameter"', () => {
const app = new OpenAPIHono()
app.openapi(
createRoute({
method: 'get',
path: '/posts/{foo}', // should be `id`
request: {
params: z.object({
id: z.string().openapi({
param: {
name: 'foo',
in: 'path',
},
}),
}),
},
responses: {
200: {
description: 'response',
},
},
}),
(c) => c.text('foo')
)
app.doc('/doc', () => ({
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'my API',
},
}))
app.doc31('/doc31', () => ({
openapi: '3.1.0',
info: {
version: '1.0.0',
title: 'my API',
},
}))
it('Should return a 500 response correctly - /doc', async () => {
const res = await app.request('/doc')
expect(res.status).toBe(500)
const data = await res.json()
expect(data).toEqual({
data: {
key: 'name',
values: ['id', 'foo'],
},
message: 'Conflicting names for parameter',
})
})
it('Should return a 500 response correctly - /doc31', async () => {
const res = await app.request('/doc31')
expect(res.status).toBe(500)
const data = await res.json()
expect(data).toEqual({
data: {
key: 'name',
values: ['id', 'foo'],
},
message: 'Conflicting names for parameter',
})
})
})

View File

@ -34,6 +34,7 @@
"hono": "^3.11.7",
"jest": "^29.7.0",
"rimraf": "^5.0.5",
"typescript": "^5.3.3",
"zod": "3.19.1"
}
}

View File

@ -1694,6 +1694,7 @@ __metadata:
hono: "npm:^3.11.7"
jest: "npm:^29.7.0"
rimraf: "npm:^5.0.5"
typescript: "npm:^5.3.3"
zod: "npm:3.19.1"
peerDependencies:
hono: ">=3.9.0"