Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #887 allow multiple params with wildcard #898

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}));
});
Loading