fix(zod-openapi): handle `Conflicting names for parameter` (#356)
* fix(zod-openapi): handle `Conflicting names for parameter` * add changesetpull/357/head
parent
6bf91e1908
commit
168a0a6d68
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: handle `Conflicting names for parameter`
|
|
@ -40,4 +40,4 @@
|
||||||
"tsup": "^8.0.1",
|
"tsup": "^8.0.1",
|
||||||
"vitest": "^1.0.4"
|
"vitest": "^1.0.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -321,8 +321,12 @@ export class OpenAPIHono<
|
||||||
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
|
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
|
||||||
return this.get(path, (c) => {
|
return this.get(path, (c) => {
|
||||||
const config = typeof configure === 'function' ? configure(c) : configure
|
const config = typeof configure === 'function' ? configure(c) : configure
|
||||||
const document = this.getOpenAPIDocument(config)
|
try {
|
||||||
return c.json(document)
|
const document = this.getOpenAPIDocument(config)
|
||||||
|
return c.json(document)
|
||||||
|
} catch (e) {
|
||||||
|
return c.json(e, 500)
|
||||||
|
}
|
||||||
}) as any
|
}) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,8 +336,12 @@ export class OpenAPIHono<
|
||||||
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
|
): OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath> => {
|
||||||
return this.get(path, (c) => {
|
return this.get(path, (c) => {
|
||||||
const config = typeof configure === 'function' ? configure(c) : configure
|
const config = typeof configure === 'function' ? configure(c) : configure
|
||||||
const document = this.getOpenAPI31Document(config)
|
try {
|
||||||
return c.json(document)
|
const document = this.getOpenAPI31Document(config)
|
||||||
|
return c.json(document)
|
||||||
|
} catch (e) {
|
||||||
|
return c.json(e, 500)
|
||||||
|
}
|
||||||
}) as any
|
}) as any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1274,3 +1274,71 @@ describe('Named params in nested routes', () => {
|
||||||
expect(Object.keys(data['paths'])[0]).toBe('/root/{rootId}/sub/{subId}')
|
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',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
"hono": "^3.11.7",
|
"hono": "^3.11.7",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
"rimraf": "^5.0.5",
|
"rimraf": "^5.0.5",
|
||||||
|
"typescript": "^5.3.3",
|
||||||
"zod": "3.19.1"
|
"zod": "3.19.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1694,6 +1694,7 @@ __metadata:
|
||||||
hono: "npm:^3.11.7"
|
hono: "npm:^3.11.7"
|
||||||
jest: "npm:^29.7.0"
|
jest: "npm:^29.7.0"
|
||||||
rimraf: "npm:^5.0.5"
|
rimraf: "npm:^5.0.5"
|
||||||
|
typescript: "npm:^5.3.3"
|
||||||
zod: "npm:3.19.1"
|
zod: "npm:3.19.1"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
hono: ">=3.9.0"
|
hono: ">=3.9.0"
|
||||||
|
|
Loading…
Reference in New Issue