Skip to content

Commit d4a9120

Browse files
committed
fix: problem with query params in route name (#439)
1 parent e6e2649 commit d4a9120

File tree

6 files changed

+888
-18
lines changed

6 files changed

+888
-18
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"test:nullableRefTest3.0": "node tests/spec/nullable-3.0/test.js",
5757
"test:nullableRefTest2.0": "node tests/spec/nullable-2.0/test.js",
5858
"test:additionalProperties2.0": "node tests/spec/additional-properties-2.0/test.js",
59-
"test:enums2.0": "node tests/spec/enums-2.0/test.js"
59+
"test:enums2.0": "node tests/spec/enums-2.0/test.js",
60+
"test:another-query-params": "node tests/spec/another-query-params/test.js"
6061
},
6162
"author": "acacode",
6263
"license": "MIT",

src/schema-parser/schema-routes.js

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,73 @@ class SchemaRoutes {
103103
this.logger.warn("wrong path param name", paramName);
104104
}
105105

106-
return [
107-
...pathParams,
108-
{
109-
$match: match,
110-
name: _.camelCase(paramName),
111-
required: true,
106+
pathParams.push({
107+
$match: match,
108+
name: _.camelCase(paramName),
109+
required: true,
110+
type: "string",
111+
description: "",
112+
schema: {
112113
type: "string",
113-
description: "",
114-
schema: {
115-
type: "string",
116-
},
117-
in: "path",
118114
},
119-
];
115+
in: "path",
116+
});
117+
118+
return pathParams;
120119
},
121120
[],
122121
);
123122

124-
const fixedRoute = _.reduce(
123+
let fixedRoute = _.reduce(
125124
pathParams,
126125
(fixedRoute, pathParam) => {
127126
return _.replace(fixedRoute, pathParam.$match, `\${${pathParam.name}}`);
128127
},
129128
routeName || "",
130129
);
131130

131+
const queryParamMatches = fixedRoute.match(/(\{\?.*\})/g);
132+
133+
if (queryParamMatches && queryParamMatches.length) {
134+
queryParamMatches.forEach((match) => {
135+
fixedRoute = fixedRoute.replace(match, "");
136+
});
137+
}
138+
139+
const queryParams = _.uniq(
140+
queryParamMatches
141+
.join(",")
142+
.replace(/(\{\?)|(\})|\s/g, "")
143+
.split(","),
144+
).reduce((acc, paramName) => {
145+
if (_.includes(paramName, "-")) {
146+
this.logger.warn("wrong query param name", paramName);
147+
}
148+
149+
acc.push({
150+
$match: paramName,
151+
name: _.camelCase(paramName),
152+
required: true,
153+
type: "string",
154+
description: "",
155+
schema: {
156+
type: "string",
157+
},
158+
in: "query",
159+
});
160+
161+
return acc;
162+
}, []);
163+
132164
return {
133165
originalRoute: routeName || "",
134166
route: fixedRoute,
135167
pathParams,
168+
queryParams,
136169
};
137170
};
138171

139-
getRouteParams = (routeInfo, pathParams) => {
172+
getRouteParams = (routeInfo, pathParamsFromRouteName, queryParamsFromRouteName) => {
140173
const { parameters } = routeInfo;
141174

142175
const routeParams = {
@@ -186,13 +219,21 @@ class SchemaRoutes {
186219
});
187220

188221
// used in case when path parameters is not declared in requestInfo.parameters ("in": "path")
189-
_.each(pathParams, (pathParam) => {
222+
_.each(pathParamsFromRouteName, (pathParam) => {
190223
const alreadyExist = _.some(routeParams.path, (parameter) => parameter.name === pathParam.name);
191224

192225
if (!alreadyExist) {
193226
routeParams.path.push(pathParam);
194227
}
195228
});
229+
// used in case when path parameters is not declared in requestInfo.parameters ("in": "path")
230+
_.each(queryParamsFromRouteName, (queryParam) => {
231+
const alreadyExist = _.some(routeParams.query, (parameter) => parameter.name === queryParam.name);
232+
233+
if (!alreadyExist) {
234+
routeParams.query.push(queryParam);
235+
}
236+
});
196237

197238
return routeParams;
198239
};
@@ -642,7 +683,11 @@ class SchemaRoutes {
642683
consumes,
643684
...otherInfo
644685
} = routeInfo;
645-
const { route, pathParams } = this.parseRouteName(rawRouteName);
686+
const {
687+
route,
688+
pathParams: pathParamsFromRouteName,
689+
queryParams: queryParamsFromRouteName,
690+
} = this.parseRouteName(rawRouteName);
646691

647692
const routeId = generateId();
648693
const firstTag = tags && tags.length > 0 ? tags[0] : null;
@@ -655,7 +700,7 @@ class SchemaRoutes {
655700
hasSecurity = security.length > 0;
656701
}
657702

658-
const routeParams = this.getRouteParams(routeInfo, pathParams);
703+
const routeParams = this.getRouteParams(routeInfo, pathParamsFromRouteName, queryParamsFromRouteName);
659704

660705
const pathArgs = routeParams.path.map((pathArgSchema) => ({
661706
name: pathArgSchema.name,

0 commit comments

Comments
 (0)