Skip to content

Commit

Permalink
Merge pull request #257 from cdimascio/url_encoded_path_params
Browse files Browse the repository at this point in the history
decode urlencoded path parameters #256
  • Loading branch information
cdimascio committed Mar 7, 2020
2 parents 25905a4 + de5fbfe commit c1d4e8c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function applyOpenApiMetadata(

if (matchedRoute) {
const paramKeys = keys.map(k => k.name);
const paramsVals = matchedRoute.slice(1);
const paramsVals = matchedRoute.slice(1).map(decodeURIComponent);
const pathParams = _zipObject(paramKeys, paramsVals);

return {
Expand Down
48 changes: 48 additions & 0 deletions test/path.params.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const apiSpecPath = path.join('test', 'resources', 'path.params.yaml');

describe('path params', () => {
let app = null;

before(async () => {
// set up express app
app = await createApp(
{
apiSpec: apiSpecPath,
validateResponses: true,
},
3005,
app => {
app.get(`${app.basePath}/users/:id?`, (req, res) => {
res.json({
id: req.params.id,
});
});

app.use((err, req, res, next) => {
res.status(err.status ?? 500).json({
message: err.message,
code: err.status ?? 500,
});
});
},
false,
);
});

after(() => {
app.server.close();
});

it('should url decode path parameters', async () =>
request(app)
.get(`${app.basePath}/users/c%20dimascio`)
.expect(200)
.then(r => {
expect(r.body.id).to.equal('c dimascio');
}));
});
29 changes: 29 additions & 0 deletions test/resources/path.params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: "3.0.0"
info:
title: "Test for allOf"
version: "1"
servers:
- url: /v1/
paths:
/users/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
200:
description: ""
content:
application/json:
schema:
$ref: "#/components/schemas/User"
components:
schemas:
User:
type: object
properties:
id:
type: "string"

0 comments on commit c1d4e8c

Please sign in to comment.