Skip to content

Commit 1ca0670

Browse files
author
Jochen Diekenbrock
committed
Merge branch 'next' into fix-consumes-test/plain
2 parents d9874b8 + 4365b63 commit 1ca0670

File tree

6 files changed

+887
-18
lines changed

6 files changed

+887
-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: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,40 +104,72 @@ class SchemaRoutes {
104104
this.logger.warn("wrong path param name", paramName);
105105
}
106106

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

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

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

140-
getRouteParams = (routeInfo, pathParams) => {
172+
getRouteParams = (routeInfo, pathParamsFromRouteName, queryParamsFromRouteName) => {
141173
const { parameters } = routeInfo;
142174

143175
const routeParams = {
@@ -187,13 +219,21 @@ class SchemaRoutes {
187219
});
188220

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

193225
if (!alreadyExist) {
194226
routeParams.path.push(pathParam);
195227
}
196228
});
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+
});
197237

198238
return routeParams;
199239
};
@@ -649,7 +689,11 @@ class SchemaRoutes {
649689
consumes,
650690
...otherInfo
651691
} = routeInfo;
652-
const { route, pathParams } = this.parseRouteName(rawRouteName);
692+
const {
693+
route,
694+
pathParams: pathParamsFromRouteName,
695+
queryParams: queryParamsFromRouteName,
696+
} = this.parseRouteName(rawRouteName);
653697

654698
const routeId = generateId();
655699
const firstTag = tags && tags.length > 0 ? tags[0] : null;
@@ -662,7 +706,7 @@ class SchemaRoutes {
662706
hasSecurity = security.length > 0;
663707
}
664708

665-
const routeParams = this.getRouteParams(routeInfo, pathParams);
709+
const routeParams = this.getRouteParams(routeInfo, pathParamsFromRouteName, queryParamsFromRouteName);
666710

667711
const pathArgs = routeParams.path.map((pathArgSchema) => ({
668712
name: pathArgSchema.name,

0 commit comments

Comments
 (0)