diff --git a/.changeset/modern-sheep-unite.md b/.changeset/modern-sheep-unite.md new file mode 100644 index 00000000..bd395106 --- /dev/null +++ b/.changeset/modern-sheep-unite.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: handle `Conflicting names for parameter` diff --git a/packages/bun-transpiler/package.json b/packages/bun-transpiler/package.json index 89ce6cf3..c13a720a 100644 --- a/packages/bun-transpiler/package.json +++ b/packages/bun-transpiler/package.json @@ -40,4 +40,4 @@ "tsup": "^8.0.1", "vitest": "^1.0.4" } -} \ No newline at end of file +} diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 63a9fc19..8b6cd248 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -321,8 +321,12 @@ export class OpenAPIHono< ): OpenAPIHono, 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, 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 } diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index e3c08148..ac4b8388 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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', + }) + }) +}) diff --git a/packages/zod-validator/package.json b/packages/zod-validator/package.json index f926ac7e..094e0b22 100644 --- a/packages/zod-validator/package.json +++ b/packages/zod-validator/package.json @@ -34,6 +34,7 @@ "hono": "^3.11.7", "jest": "^29.7.0", "rimraf": "^5.0.5", + "typescript": "^5.3.3", "zod": "3.19.1" } } diff --git a/yarn.lock b/yarn.lock index f6c05bbf..050aee35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"