Skip to content

Request path parameters should be encoded #5

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,18 @@ export default class Route implements IMountableItem {
//
// This method will iterate through all variables, check their definition type from the spec
// and typecast them
private typecastVariables(variables: { [key: string]: string }): { [key: string]: any } {
private typecastVariables(variables: { [key: string]: string }, encoder?: Function): { [key: string]: any } {
const parsedVariables: { [key: string]: any } = {};

Object.entries(variables).forEach(
([variableName, value]) => {
const variableDefinition = this.operationVariables[variableName];
const typecastValue = typecastVariable(value, variableDefinition);

parsedVariables[variableName] = typecastVariable(value, variableDefinition);
// We only want to encode strings not integer values otherwise integers
// become strings
const shouldUseEncoder = encoder && typeof typecastValue === 'string';
parsedVariables[variableName] = shouldUseEncoder ? encode(typecastValue) : typecastValue;
}
);

Expand All @@ -287,7 +291,7 @@ export default class Route implements IMountableItem {
const { query, params, body } = req;

const parsedQueryVariables = this.typecastVariables(query);
const parsedPathVariables = this.typecastVariables(params);
const parsedPathVariables = this.typecastVariables(params, encodeURI);

const providedVariables = { ...parsedQueryVariables, ...parsedPathVariables, ...body };

Expand Down