Skip to content

Commit

Permalink
fix: #887 allow multiple params with wildcard (#898)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Mitchell3514 and guillerecalde authored Feb 2, 2024
1 parent dffda28 commit 2d33d0a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/framework/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
36 changes: 36 additions & 0 deletions test/resources/wildcard.path.params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/wildcard.path.params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
},
);
Expand Down Expand Up @@ -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');
}));
});

0 comments on commit 2d33d0a

Please sign in to comment.