From 2d33d0af590c2c1782652efb2614839bd37d1643 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Fri, 2 Feb 2024 06:05:43 +0100 Subject: [PATCH] fix: #887 allow multiple params with wildcard (#898) * Add multiple path parameters with wildcard tests * Change regex to support multiple params when including file path params (#1) * Change regex to support multiple params when including URI path param * Update regex, remove unnecessary bracket --------- Co-authored-by: Guillermo Recalde --- src/framework/openapi.spec.loader.ts | 2 +- test/resources/wildcard.path.params.yaml | 36 ++++++++++++++++++++++++ test/wildcard.path.params.spec.ts | 28 ++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/framework/openapi.spec.loader.ts b/src/framework/openapi.spec.loader.ts index bc0d075f..44b6a8a3 100644 --- a/src/framework/openapi.spec.loader.ts +++ b/src/framework/openapi.spec.loader.ts @@ -106,7 +106,7 @@ export class OpenApiSpecLoader { // instead create our own syntax that is compatible with express' pathToRegex // /{path}* => /:path*) // /{path}(*) => /:path*) - const pass1 = part.replace(/\/{([^\*]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2'); + const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2'); // substitute params with express equivalent // /path/{id} => /path/:id return pass1.replace(/\{([^}]+)}/g, ':$1'); diff --git a/test/resources/wildcard.path.params.yaml b/test/resources/wildcard.path.params.yaml index d9122af3..13bf3a4a 100644 --- a/test/resources/wildcard.path.params.yaml +++ b/test/resources/wildcard.path.params.yaml @@ -52,6 +52,42 @@ paths: /d3: get: + responses: + 200: + description: dummy response + content: {} + + /d4/{multi}/spaced/{path}(*): + get: + parameters: + - name: multi + in: path + required: true + schema: + type: string + - name: path + in: path + required: true + schema: + type: string + responses: + 200: + description: dummy response + content: {} + + /d5/{multi}/{path}(*): + get: + parameters: + - name: multi + in: path + required: true + schema: + type: string + - name: path + in: path + required: true + schema: + type: string responses: 200: description: dummy response diff --git a/test/wildcard.path.params.spec.ts b/test/wildcard.path.params.spec.ts index 52f72f40..cbf610b9 100644 --- a/test/wildcard.path.params.spec.ts +++ b/test/wildcard.path.params.spec.ts @@ -34,6 +34,16 @@ describe('wildcard path params', () => { res.json({ success: true, }), + ) + .get(`${app.basePath}/d4/:multi/spaced/:path(*)`, (req, res) => + res.json({ + ...req.params, + }), + ) + .get(`${app.basePath}/d5/:multi/:path(*)`, (req, res) => + res.json({ + ...req.params, + }), ); }, ); @@ -83,4 +93,22 @@ describe('wildcard path params', () => { .then((r) => { expect(r.body.success).to.be.true; })); + + it('should return 200 when wildcard path includes all required params and multiple path params', async () => + request(app) + .get(`${app.basePath}/d4/one/spaced/two/three/four`) + .expect(200) + .then((r) => { + expect(r.body.multi).to.equal('one'); + expect(r.body.path).to.equal('two/three/four'); + })); + + it('should return 200 when wildcard path includes all required params and multiple path params', async () => + request(app) + .get(`${app.basePath}/d5/one/two/three/four`) + .expect(200) + .then((r) => { + expect(r.body.multi).to.equal('one'); + expect(r.body.path).to.equal('two/three/four'); + })); });