diff --git a/.changeset/quiet-shrimps-rescue.md b/.changeset/quiet-shrimps-rescue.md new file mode 100644 index 00000000..c76c5db2 --- /dev/null +++ b/.changeset/quiet-shrimps-rescue.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: Fix basePath method disregarding defaultHook diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 8b6cd248..71166b9b 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -409,7 +409,7 @@ export class OpenAPIHono< } basePath(path: SubPath): OpenAPIHono> { - return new OpenAPIHono(super.basePath(path) as any) + return new OpenAPIHono({...super.basePath(path) as any, defaultHook: this.defaultHook}) } } diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index ac4b8388..8ce78fa0 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -823,6 +823,15 @@ describe('basePath()', () => { const res = await app.request('/api/doc') expect(res.status).toBe(200) }) + + it('Should retain defaultHook of the parent app', async () => { + const defaultHook = () => {} + const app = new OpenAPIHono({ + defaultHook + }).basePath('/api') + expect(app.defaultHook).toBeDefined() + expect(app.defaultHook).toBe(defaultHook) + }) }) describe('With hc', () => {