From e0136f1240a0bea680ef3ca07e5b05e2bd4aa1ef Mon Sep 17 00:00:00 2001 From: "Joshua@Econify" Date: Fri, 27 Dec 2019 15:31:48 -0500 Subject: [PATCH 1/2] Request path parameters should be encoded --- Route.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Route.ts b/Route.ts index 8d39f0f..bbc3b37 100644 --- a/Route.ts +++ b/Route.ts @@ -268,14 +268,15 @@ 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); + parsedVariables[variableName] = encoder ? encode(typecastValue) : typecastValue; } ); @@ -287,7 +288,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 }; From 25b3935078d8fae4e756a65842dcc11f399be7a5 Mon Sep 17 00:00:00 2001 From: "Joshua@Econify" Date: Fri, 27 Dec 2019 17:06:40 -0500 Subject: [PATCH 2/2] Only encode path params that are typecast to string to avoid recasting integers as strings --- Route.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Route.ts b/Route.ts index bbc3b37..57bbf9e 100644 --- a/Route.ts +++ b/Route.ts @@ -276,7 +276,10 @@ export default class Route implements IMountableItem { const variableDefinition = this.operationVariables[variableName]; const typecastValue = typecastVariable(value, variableDefinition); - parsedVariables[variableName] = encoder ? encode(typecastValue) : typecastValue; + // 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; } );