Skip to content

Commit a2859a3

Browse files
committed
refactored operation parameter handling
1 parent 06863bf commit a2859a3

File tree

3 files changed

+178
-65
lines changed

3 files changed

+178
-65
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,17 +2117,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
21172117
}
21182118

21192119
List<Parameter> parameters = operation.getParameters();
2120-
CodegenParameter bodyParam = null;
2121-
List<CodegenParameter> allParams = new ArrayList<>();
2122-
List<CodegenParameter> bodyParams = new ArrayList<>();
2123-
List<CodegenParameter> pathParams = new ArrayList<>();
2124-
List<CodegenParameter> queryParams = new ArrayList<>();
2125-
List<CodegenParameter> headerParams = new ArrayList<>();
2126-
List<CodegenParameter> cookieParams = new ArrayList<>();
2127-
List<CodegenParameter> formParams = new ArrayList<>();
2128-
List<CodegenParameter> requiredParams = new ArrayList<>();
2129-
2130-
List<CodegenContent> codegenContents = new ArrayList<>();
2120+
final OpenAPIParameters openAPIParameters = new OpenAPIParameters();
21312121

21322122
RequestBody body = operation.getRequestBody();
21332123
if (body != null) {
@@ -2188,20 +2178,20 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
21882178
}
21892179
// todo: this segment is only to support the "older" template design. it should be removed once all templates are updated with the new {{#contents}} tag.
21902180
formParameter.getVendorExtensions().put(CodegenConstants.IS_FORM_PARAM_EXT_NAME, Boolean.TRUE);
2191-
formParams.add(formParameter.copy());
2181+
openAPIParameters.addFormParam(formParameter.copy());
21922182
if (body.getRequired() != null && body.getRequired()) {
2193-
requiredParams.add(formParameter.copy());
2183+
openAPIParameters.addRequiredParam(formParameter.copy());
21942184
}
2195-
allParams.add(formParameter);
2185+
openAPIParameters.addAllParams(formParameter);
21962186
}
2197-
codegenContents.add(codegenContent);
2187+
openAPIParameters.addCodegenContents(codegenContent);
21982188
}
21992189
} else {
2200-
bodyParam = fromRequestBody(body, schemaName, schema, schemas, imports);
2190+
CodegenParameter bodyParam = fromRequestBody(body, schemaName, schema, schemas, imports);
2191+
openAPIParameters.setBodyParam(bodyParam);
22012192
if (foundSchemas.isEmpty()) {
2202-
// todo: this segment is only to support the "older" template design. it should be removed once all templates are updated with the new {{#contents}} tag.
2203-
bodyParams.add(bodyParam.copy());
2204-
allParams.add(bodyParam);
2193+
openAPIParameters.addBodyParams(bodyParam.copy());
2194+
openAPIParameters.addAllParams(bodyParam);
22052195
} else {
22062196
boolean alreadyAdded = false;
22072197
for (Schema usedSchema : foundSchemas) {
@@ -2214,7 +2204,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
22142204
}
22152205
}
22162206
foundSchemas.add(schema);
2217-
codegenContents.add(codegenContent);
2207+
openAPIParameters.addCodegenContents(codegenContent);
22182208
}
22192209
}
22202210
}
@@ -2225,52 +2215,24 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
22252215
param = getParameterFromRef(param.get$ref(), openAPI);
22262216
}
22272217
CodegenParameter codegenParameter = fromParameter(param, imports);
2228-
allParams.add(codegenParameter);
2229-
// Issue #2561 (neilotoole) : Moved setting of is<Type>Param flags
2230-
// from here to fromParameter().
2231-
if (param instanceof QueryParameter || "query".equalsIgnoreCase(param.getIn())) {
2232-
queryParams.add(codegenParameter.copy());
2233-
} else if (param instanceof PathParameter || "path".equalsIgnoreCase(param.getIn())) {
2234-
pathParams.add(codegenParameter.copy());
2235-
} else if (param instanceof HeaderParameter || "header".equalsIgnoreCase(param.getIn())) {
2236-
headerParams.add(codegenParameter.copy());
2237-
} else if (param instanceof CookieParameter || "cookie".equalsIgnoreCase(param.getIn())) {
2238-
cookieParams.add(codegenParameter.copy());
2239-
}
2240-
if (codegenParameter.required) {
2241-
requiredParams.add(codegenParameter.copy());
2242-
}
2218+
openAPIParameters.addParameters(param, codegenParameter);
22432219
}
22442220
}
22452221

22462222
addOperationImports(codegenOperation, imports);
22472223

2248-
codegenOperation.bodyParam = bodyParam;
2224+
codegenOperation.bodyParam = openAPIParameters.getBodyParam();
22492225
codegenOperation.httpMethod = httpMethod.toUpperCase();
22502226

22512227
// move "required" parameters in front of "optional" parameters
22522228
if (sortParamsByRequiredFlag) {
2253-
Collections.sort(allParams, new Comparator<CodegenParameter>() {
2254-
@Override
2255-
public int compare(CodegenParameter one, CodegenParameter another) {
2256-
if (one.required == another.required) return 0;
2257-
else if (one.required) return -1;
2258-
else return 1;
2259-
}
2260-
});
2229+
openAPIParameters.sortRequiredAllParams();
22612230
}
22622231

2263-
codegenOperation.allParams = addHasMore(allParams);
2264-
codegenOperation.bodyParams = addHasMore(bodyParams);
2265-
codegenOperation.pathParams = addHasMore(pathParams);
2266-
codegenOperation.queryParams = addHasMore(queryParams);
2267-
codegenOperation.headerParams = addHasMore(headerParams);
2268-
codegenOperation.cookieParams = addHasMore(cookieParams);
2269-
codegenOperation.formParams = addHasMore(formParams);
2270-
codegenOperation.requiredParams = addHasMore(requiredParams);
2232+
openAPIParameters.addHasMore(codegenOperation);
22712233
codegenOperation.externalDocs = operation.getExternalDocs();
22722234

2273-
configuresParameterForMediaType(codegenOperation, codegenContents);
2235+
configuresParameterForMediaType(codegenOperation, openAPIParameters.getCodegenContents());
22742236
// legacy support
22752237
codegenOperation.nickname = codegenOperation.operationId;
22762238

@@ -3002,16 +2964,6 @@ private void addHeaders(ApiResponse response, List<CodegenProperty> target, Map<
30022964
}
30032965
}
30042966

3005-
protected static List<CodegenParameter> addHasMore(List<CodegenParameter> objs) {
3006-
if (objs != null) {
3007-
for (int i = 0; i < objs.size(); i++) {
3008-
objs.get(i).secondaryParam = i > 0;
3009-
objs.get(i).getVendorExtensions().put(CodegenConstants.HAS_MORE_EXT_NAME, i < objs.size() - 1);
3010-
}
3011-
}
3012-
return objs;
3013-
}
3014-
30152967
private static Map<String, Object> addHasMore(Map<String, Object> objs) {
30162968
if (objs != null) {
30172969
for (int i = 0; i < objs.size() - 1; i++) {
@@ -4379,7 +4331,7 @@ protected void configuresParameterForMediaType(CodegenOperation codegenOperation
43794331
}
43804332
}
43814333
);
4382-
addHasMore(content.getParameters());
4334+
OpenAPIParameters.addHasMore(content.getParameters());
43834335
}
43844336
codegenOperation.getContents().addAll(codegenContents);
43854337
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package io.swagger.codegen.v3.generators;
2+
3+
import io.swagger.codegen.v3.CodegenConstants;
4+
import io.swagger.codegen.v3.CodegenContent;
5+
import io.swagger.codegen.v3.CodegenOperation;
6+
import io.swagger.codegen.v3.CodegenParameter;
7+
import io.swagger.v3.oas.models.parameters.CookieParameter;
8+
import io.swagger.v3.oas.models.parameters.HeaderParameter;
9+
import io.swagger.v3.oas.models.parameters.Parameter;
10+
import io.swagger.v3.oas.models.parameters.PathParameter;
11+
import io.swagger.v3.oas.models.parameters.QueryParameter;
12+
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.List;
16+
17+
public class OpenAPIParameters {
18+
19+
private CodegenParameter bodyParam = null;
20+
private List<CodegenParameter> allParams = new ArrayList<>();
21+
private List<CodegenParameter> bodyParams = new ArrayList<>();
22+
private List<CodegenParameter> pathParams = new ArrayList<>();
23+
private List<CodegenParameter> queryParams = new ArrayList<>();
24+
private List<CodegenParameter> headerParams = new ArrayList<>();
25+
private List<CodegenParameter> cookieParams = new ArrayList<>();
26+
private List<CodegenParameter> formParams = new ArrayList<>();
27+
private List<CodegenParameter> requiredParams = new ArrayList<>();
28+
private List<CodegenContent> codegenContents = new ArrayList<>();
29+
30+
public void setBodyParam(CodegenParameter bodyParam) {
31+
this.bodyParam = bodyParam;
32+
}
33+
34+
public CodegenParameter getBodyParam() {
35+
return bodyParam;
36+
}
37+
38+
public List<CodegenParameter> getAllParams() {
39+
return allParams;
40+
}
41+
42+
public List<CodegenParameter> getBodyParams() {
43+
return bodyParams;
44+
}
45+
46+
public List<CodegenParameter> getPathParams() {
47+
return pathParams;
48+
}
49+
50+
public List<CodegenParameter> getQueryParams() {
51+
return queryParams;
52+
}
53+
54+
public List<CodegenParameter> getHeaderParams() {
55+
return headerParams;
56+
}
57+
58+
public List<CodegenParameter> getCookieParams() {
59+
return cookieParams;
60+
}
61+
62+
public List<CodegenParameter> getFormParams() {
63+
return formParams;
64+
}
65+
66+
public List<CodegenParameter> getRequiredParams() {
67+
return requiredParams;
68+
}
69+
70+
public List<CodegenContent> getCodegenContents() {
71+
return codegenContents;
72+
}
73+
74+
public void addAllParams(CodegenParameter codegenParameter) {
75+
allParams.add(codegenParameter);
76+
}
77+
78+
public void addBodyParams(CodegenParameter codegenParameter) {
79+
bodyParams.add(codegenParameter);
80+
}
81+
82+
public void addPathParams(CodegenParameter codegenParameter) {
83+
pathParams.add(codegenParameter);
84+
}
85+
86+
public void addQueryParams(CodegenParameter codegenParameter) {
87+
queryParams.add(codegenParameter);
88+
}
89+
90+
public void addHeaderParams(CodegenParameter codegenParameter) {
91+
headerParams.add(codegenParameter);
92+
}
93+
94+
public void addCookieParams(CodegenParameter codegenParameter) {
95+
cookieParams.add(codegenParameter);
96+
}
97+
98+
public void addFormParam(CodegenParameter codegenParameter) {
99+
formParams.add(codegenParameter);
100+
}
101+
102+
public void addRequiredParam(CodegenParameter codegenParameter) {
103+
requiredParams.add(codegenParameter);
104+
}
105+
106+
public void addCodegenContents(CodegenContent codegenContent) {
107+
codegenContents.add(codegenContent);
108+
}
109+
110+
public void addParameters(Parameter parameter, CodegenParameter codegenParameter) {
111+
allParams.add(codegenParameter);
112+
113+
if (parameter instanceof QueryParameter || "query".equalsIgnoreCase(parameter.getIn())) {
114+
queryParams.add(codegenParameter.copy());
115+
} else if (parameter instanceof PathParameter || "path".equalsIgnoreCase(parameter.getIn())) {
116+
pathParams.add(codegenParameter.copy());
117+
} else if (parameter instanceof HeaderParameter || "header".equalsIgnoreCase(parameter.getIn())) {
118+
headerParams.add(codegenParameter.copy());
119+
} else if (parameter instanceof CookieParameter || "cookie".equalsIgnoreCase(parameter.getIn())) {
120+
cookieParams.add(codegenParameter.copy());
121+
}
122+
if (codegenParameter.required) {
123+
requiredParams.add(codegenParameter.copy());
124+
}
125+
}
126+
127+
public void addHasMore(CodegenOperation codegenOperation) {
128+
codegenOperation.allParams = addHasMore(allParams);
129+
codegenOperation.bodyParams = addHasMore(bodyParams);
130+
codegenOperation.pathParams = addHasMore(pathParams);
131+
codegenOperation.queryParams = addHasMore(queryParams);
132+
codegenOperation.headerParams = addHasMore(headerParams);
133+
codegenOperation.cookieParams = addHasMore(cookieParams);
134+
codegenOperation.formParams = addHasMore(formParams);
135+
codegenOperation.requiredParams = addHasMore(requiredParams);
136+
}
137+
138+
public void sortRequiredAllParams() {
139+
Collections.sort(allParams, (one, another) -> {
140+
if (one.required == another.required) {
141+
return 0;
142+
} else if (one.required) {
143+
return -1;
144+
} else {
145+
return 1;
146+
}
147+
});
148+
}
149+
150+
public static List<CodegenParameter> addHasMore(List<CodegenParameter> codegenParameters) {
151+
if (codegenParameters == null || codegenParameters.isEmpty()) {
152+
return codegenParameters;
153+
}
154+
for (int i = 0; i < codegenParameters.size(); i++) {
155+
codegenParameters.get(i).secondaryParam = i > 0;
156+
codegenParameters.get(i).getVendorExtensions().put(CodegenConstants.HAS_MORE_EXT_NAME, i < codegenParameters.size() - 1);
157+
}
158+
return codegenParameters;
159+
}
160+
}

src/main/java/io/swagger/codegen/v3/generators/nodejs/NodeJSServerCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.common.collect.ArrayListMultimap;
1111
import com.google.common.collect.Lists;
1212
import com.google.common.collect.Multimap;
13+
import io.swagger.codegen.v3.generators.OpenAPIParameters;
1314
import io.swagger.codegen.v3.utils.URLPathUtil;
1415
import io.swagger.v3.oas.models.OpenAPI;
1516
import io.swagger.v3.oas.models.Operation;
@@ -452,7 +453,7 @@ protected void configuresParameterForMediaType(CodegenOperation codegenOperation
452453
addParameters(content, codegenOperation.cookieParams);
453454
}
454455
for (CodegenContent content : codegenContents) {
455-
addHasMore(content.getParameters());
456+
OpenAPIParameters.addHasMore(content.getParameters());
456457
}
457458
codegenOperation.getContents().addAll(codegenContents);
458459
}

0 commit comments

Comments
 (0)