Skip to content

Commit c1d4e8c

Browse files
authored
Merge pull request #257 from cdimascio/url_encoded_path_params
decode urlencoded path parameters #256
2 parents 25905a4 + de5fbfe commit c1d4e8c

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/middlewares/openapi.metadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function applyOpenApiMetadata(
5252

5353
if (matchedRoute) {
5454
const paramKeys = keys.map(k => k.name);
55-
const paramsVals = matchedRoute.slice(1);
55+
const paramsVals = matchedRoute.slice(1).map(decodeURIComponent);
5656
const pathParams = _zipObject(paramKeys, paramsVals);
5757

5858
return {

test/path.params.spec.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as path from 'path';
2+
import { expect } from 'chai';
3+
import * as request from 'supertest';
4+
import { createApp } from './common/app';
5+
6+
const apiSpecPath = path.join('test', 'resources', 'path.params.yaml');
7+
8+
describe('path params', () => {
9+
let app = null;
10+
11+
before(async () => {
12+
// set up express app
13+
app = await createApp(
14+
{
15+
apiSpec: apiSpecPath,
16+
validateResponses: true,
17+
},
18+
3005,
19+
app => {
20+
app.get(`${app.basePath}/users/:id?`, (req, res) => {
21+
res.json({
22+
id: req.params.id,
23+
});
24+
});
25+
26+
app.use((err, req, res, next) => {
27+
res.status(err.status ?? 500).json({
28+
message: err.message,
29+
code: err.status ?? 500,
30+
});
31+
});
32+
},
33+
false,
34+
);
35+
});
36+
37+
after(() => {
38+
app.server.close();
39+
});
40+
41+
it('should url decode path parameters', async () =>
42+
request(app)
43+
.get(`${app.basePath}/users/c%20dimascio`)
44+
.expect(200)
45+
.then(r => {
46+
expect(r.body.id).to.equal('c dimascio');
47+
}));
48+
});

test/resources/path.params.yaml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: "Test for allOf"
4+
version: "1"
5+
servers:
6+
- url: /v1/
7+
paths:
8+
/users/{id}:
9+
get:
10+
parameters:
11+
- name: id
12+
in: path
13+
required: true
14+
schema:
15+
type: string
16+
responses:
17+
200:
18+
description: ""
19+
content:
20+
application/json:
21+
schema:
22+
$ref: "#/components/schemas/User"
23+
components:
24+
schemas:
25+
User:
26+
type: object
27+
properties:
28+
id:
29+
type: "string"

0 commit comments

Comments
 (0)