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() + }) })