From f2fbea6cf4c1c400890437ffdaf5fe1f54fe7f69 Mon Sep 17 00:00:00 2001 From: Miran Setinc Date: Thu, 26 Sep 2024 13:51:27 +0200 Subject: [PATCH] support for method added to ignorePaths function --- src/framework/openapi.context.ts | 10 ++++++---- src/middlewares/openapi.metadata.ts | 9 ++++++--- test/ignore.paths.spec.ts | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/framework/openapi.context.ts b/src/framework/openapi.context.ts index 8d6fc23f..08d45da1 100644 --- a/src/framework/openapi.context.ts +++ b/src/framework/openapi.context.ts @@ -32,17 +32,19 @@ export class OpenApiContext { this.serial = spec.serial; } - public isManagedRoute(path: string): boolean { + public isManagedRoute(path: string, method: string): boolean { for (const bp of this.basePaths) { - if (path.startsWith(bp) && !this.shouldIgnoreRoute(path)) { + if (path.startsWith(bp) && !this.shouldIgnoreRoute(path, method)) { return true; } } return false; } - public shouldIgnoreRoute(path: string) { - return typeof this.ignorePaths === 'function' ? this.ignorePaths(path) : this.ignorePaths?.test(path); + public shouldIgnoreRoute(path: string, method: string): boolean { + return typeof this.ignorePaths === 'function' + ? this.ignorePaths(path, method) + : this.ignorePaths?.test(path); } public routePair(route: string): RoutePair { diff --git a/src/middlewares/openapi.metadata.ts b/src/middlewares/openapi.metadata.ts index b2675c09..fefd8391 100644 --- a/src/middlewares/openapi.metadata.ts +++ b/src/middlewares/openapi.metadata.ts @@ -22,7 +22,8 @@ export function applyOpenApiMetadata( const path = req.path.startsWith(req.baseUrl) ? req.path : `${req.baseUrl}/${req.path}`; - if (openApiContext.shouldIgnoreRoute(path)) { + const { method } = req; + if (openApiContext.shouldIgnoreRoute(path, method)) { return next(); } const matched = lookupRoute(req, openApiContext.useRequestUrl); @@ -56,7 +57,7 @@ export function applyOpenApiMetadata( (req.openapi)._responseSchema = (matched)._responseSchema; } } else if ( - openApiContext.isManagedRoute(path) && + openApiContext.isManagedRoute(path, method) && !openApiContext.ignoreUndocumented ) { throw new NotFound({ @@ -71,7 +72,9 @@ export function applyOpenApiMetadata( req: OpenApiRequest, useRequestUrl: boolean, ): OpenApiRequestMetadata { - const path = useRequestUrl ? req.url.split('?')[0] : req.originalUrl.split('?')[0]; + const path = useRequestUrl + ? req.url.split('?')[0] + : req.originalUrl.split('?')[0]; const method = req.method; const routeEntries = Object.entries(openApiContext.expressRouteMap); for (const [expressRoute, methods] of routeEntries) { diff --git a/test/ignore.paths.spec.ts b/test/ignore.paths.spec.ts index b795b7a3..5594a198 100644 --- a/test/ignore.paths.spec.ts +++ b/test/ignore.paths.spec.ts @@ -112,7 +112,11 @@ describe('ignorePaths as Function', () => { const apiSpec = path.join('test', 'resources', 'ignore.paths.yaml'); app = await createApp( - { apiSpec, ignorePaths: (path) => path.endsWith('/hippies') }, + { + apiSpec, + ignorePaths: (path, method) => + path.endsWith('/hippies') && ['GET', 'POST'].includes(method), + }, 3005, app => { app.all('/v1/hippies', (req, res) => { @@ -121,6 +125,7 @@ describe('ignorePaths as Function', () => { { id: 2, name: 'fred' }, ]); }); + app.get('/v1/hippies/1', (req, res) => { res.json({ id: 1, name: 'farah' }); }); @@ -156,6 +161,17 @@ describe('ignorePaths as Function', () => { .expect('Content-Type', /json/) .expect(200)); + it('should not ignore path and return 405', async () => + request(app) + .put(`${basePath}/hippies`) + .query({ + test: 'one', + limit: 2, + }) + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(405)); + it('should not ignore path and return 404', async () => request(app) .get(`${basePath}/hippies/1`)