Skip to content

Commit 2d33d0a

Browse files
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 <[email protected]>
1 parent dffda28 commit 2d33d0a

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/framework/openapi.spec.loader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class OpenApiSpecLoader {
106106
// instead create our own syntax that is compatible with express' pathToRegex
107107
// /{path}* => /:path*)
108108
// /{path}(*) => /:path*)
109-
const pass1 = part.replace(/\/{([^\*]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2');
109+
const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2');
110110
// substitute params with express equivalent
111111
// /path/{id} => /path/:id
112112
return pass1.replace(/\{([^}]+)}/g, ':$1');

test/resources/wildcard.path.params.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ paths:
5252

5353
/d3:
5454
get:
55+
responses:
56+
200:
57+
description: dummy response
58+
content: {}
59+
60+
/d4/{multi}/spaced/{path}(*):
61+
get:
62+
parameters:
63+
- name: multi
64+
in: path
65+
required: true
66+
schema:
67+
type: string
68+
- name: path
69+
in: path
70+
required: true
71+
schema:
72+
type: string
73+
responses:
74+
200:
75+
description: dummy response
76+
content: {}
77+
78+
/d5/{multi}/{path}(*):
79+
get:
80+
parameters:
81+
- name: multi
82+
in: path
83+
required: true
84+
schema:
85+
type: string
86+
- name: path
87+
in: path
88+
required: true
89+
schema:
90+
type: string
5591
responses:
5692
200:
5793
description: dummy response

test/wildcard.path.params.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ describe('wildcard path params', () => {
3434
res.json({
3535
success: true,
3636
}),
37+
)
38+
.get(`${app.basePath}/d4/:multi/spaced/:path(*)`, (req, res) =>
39+
res.json({
40+
...req.params,
41+
}),
42+
)
43+
.get(`${app.basePath}/d5/:multi/:path(*)`, (req, res) =>
44+
res.json({
45+
...req.params,
46+
}),
3747
);
3848
},
3949
);
@@ -83,4 +93,22 @@ describe('wildcard path params', () => {
8393
.then((r) => {
8494
expect(r.body.success).to.be.true;
8595
}));
96+
97+
it('should return 200 when wildcard path includes all required params and multiple path params', async () =>
98+
request(app)
99+
.get(`${app.basePath}/d4/one/spaced/two/three/four`)
100+
.expect(200)
101+
.then((r) => {
102+
expect(r.body.multi).to.equal('one');
103+
expect(r.body.path).to.equal('two/three/four');
104+
}));
105+
106+
it('should return 200 when wildcard path includes all required params and multiple path params', async () =>
107+
request(app)
108+
.get(`${app.basePath}/d5/one/two/three/four`)
109+
.expect(200)
110+
.then((r) => {
111+
expect(r.body.multi).to.equal('one');
112+
expect(r.body.path).to.equal('two/three/four');
113+
}));
86114
});

0 commit comments

Comments
 (0)