From 4af8db43a7025018f2e5b0e6fdeb03bc34cf5eeb Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti Date: Wed, 8 Oct 2025 14:08:38 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20avoid=20error=20when=20us?= =?UTF-8?q?ing=20`exclude.tags`=20with=20an=20API=20without=20tags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openapi.ts | 4 ++-- test/index.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/openapi.ts b/src/openapi.ts index d99e9c7..573f1dd 100644 --- a/src/openapi.ts +++ b/src/openapi.ts @@ -320,7 +320,7 @@ export function toOpenAPISchema( continue const hooks: InputSchema & { - detail: Partial + detail?: Partial } = route.hooks ?? {} if (references?.length) @@ -380,7 +380,7 @@ export function toOpenAPISchema( if ( excludeTags && - hooks.detail.tags?.some((tag) => excludeTags?.includes(tag)) + hooks.detail?.tags?.some((tag) => excludeTags?.includes(tag)) ) continue diff --git a/test/index.test.ts b/test/index.test.ts index 9c61e47..c876b04 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -272,4 +272,38 @@ describe('Swagger', () => { const response = await res.json() expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8) }) + + // https://github.com/elysiajs/elysia-openapi/issues/273 + it('should exclude routes with specified tags', async () => { + const app = new Elysia() + .use( + openapi({ + exclude: { + tags: ['internal'] + } + }) + ) + .get('/', () => 'index') + .get('/healthz', () => ({ status: 'ok' }), { + detail: { + tags: ['internal'] + } + }) + + await app.modules + + const res = await app.handle(req('/openapi/json')) + expect(res.status).toBe(200) + const response = await res.json() + + // Check that only root path is included + expect(Object.keys(response.paths)).toEqual(['/']) + + // Verify /healthz is excluded + expect(response.paths['/healthz']).toBeUndefined() + + // Verify root path is included and has GET method + expect(response.paths['/']).toBeDefined() + expect(response.paths['/'].get).toBeDefined() + }) })