Skip to content

Commit 04ede6d

Browse files
committed
Merge pull request #2242 from xhh/java-reserved-operation-id
Handle reserved operationId/modelName in Java/Android clients
2 parents 2573146 + 35079fe commit 04ede6d

39 files changed

+258
-227
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
121121
return objs;
122122
}
123123

124-
124+
125125
// override with any special post-processing
126126
@SuppressWarnings("static-method")
127127
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
@@ -157,7 +157,7 @@ public void processSwagger(Swagger swagger) {
157157
// override with any special text escaping logic
158158
@SuppressWarnings("static-method")
159159
public String escapeText(String input) {
160-
if (input != null) {
160+
if (input != null) {
161161
input = input.trim(); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
162162
String output = input.replaceAll("\n", "\\\\n");
163163
output = output.replace("\r", "\\r");
@@ -258,7 +258,7 @@ public Map<String, Object> additionalProperties() {
258258
public Map<String, Object> vendorExtensions() {
259259
return vendorExtensions;
260260
}
261-
261+
262262
public List<SupportingFile> supportingFiles() {
263263
return supportingFiles;
264264
}
@@ -297,7 +297,7 @@ public void setEnsureUniqueParams(Boolean ensureUniqueParams) {
297297

298298
/**
299299
* Return the file name of the Api Test
300-
*
300+
*
301301
* @param name the file name of the Api
302302
* @return the file name of the Api
303303
*/
@@ -307,7 +307,7 @@ public String toApiFilename(String name) {
307307

308308
/**
309309
* Return the file name of the Api Test
310-
*
310+
*
311311
* @param name the file name of the Api
312312
* @return the file name of the Api
313313
*/
@@ -317,7 +317,7 @@ public String toApiTestFilename(String name) {
317317

318318
/**
319319
* Return the variable name in the Api
320-
*
320+
*
321321
* @param name the varible name of the Api
322322
* @return the snake-cased variable name
323323
*/
@@ -327,7 +327,7 @@ public String toApiVarName(String name) {
327327

328328
/**
329329
* Return the capitalized file name of the model test
330-
*
330+
*
331331
* @param name the model name
332332
* @return the file name of the model
333333
*/
@@ -337,17 +337,17 @@ public String toModelFilename(String name) {
337337

338338
/**
339339
* Return the capitalized file name of the model test
340-
*
340+
*
341341
* @param name the model name
342342
* @return the file name of the model
343343
*/
344344
public String toModelTestFilename(String name) {
345345
return initialCaps(name) + "Test";
346346
}
347-
347+
348348
/**
349349
* Return the operation ID (method name)
350-
*
350+
*
351351
* @param operationId operation ID
352352
* @return the sanitized method name
353353
*/
@@ -364,7 +364,7 @@ public String toOperationId(String operationId) {
364364
/**
365365
* Return the variable name by removing invalid characters and proper escaping if
366366
* it's a reserved word.
367-
*
367+
*
368368
* @param name the variable name
369369
* @return the sanitized variable name
370370
*/
@@ -379,7 +379,7 @@ public String toVarName(String name) {
379379
/**
380380
* Return the parameter name by removing invalid characters and proper escaping if
381381
* it's a reserved word.
382-
*
382+
*
383383
* @param name Codegen property object
384384
* @return the sanitized parameter name
385385
*/
@@ -393,18 +393,18 @@ public String toParamName(String name) {
393393

394394
/**
395395
* Return the Enum name (e.g. StatusEnum given 'status')
396-
*
396+
*
397397
* @param property Codegen property object
398398
* @return the Enum name
399399
*/
400400
@SuppressWarnings("static-method")
401401
public String toEnumName(CodegenProperty property) {
402402
return StringUtils.capitalize(property.name) + "Enum";
403403
}
404-
404+
405405
/**
406406
* Return the escaped name of the reserved word
407-
*
407+
*
408408
* @param name the name to be escaped
409409
* @return the escaped reserved word
410410
*
@@ -417,7 +417,7 @@ public String escapeReservedWord(String name) {
417417

418418
/**
419419
* Return the fully-qualified "Model" name for import
420-
*
420+
*
421421
* @param name the name of the "Model"
422422
* @return the fully-qualified "Model" name for import
423423
*/
@@ -431,7 +431,7 @@ public String toModelImport(String name) {
431431

432432
/**
433433
* Return the fully-qualified "Api" name for import
434-
*
434+
*
435435
* @param name the name of the "Api"
436436
* @return the fully-qualified "Api" name for import
437437
*/
@@ -442,7 +442,7 @@ public String toApiImport(String name) {
442442
/**
443443
* Default constructor.
444444
* This method will map between Swagger type and language-specified type, as well as mapping
445-
* between Swagger type and the corresponding import statement for the language. This will
445+
* between Swagger type and the corresponding import statement for the language. This will
446446
* also add some language specified CLI options, if any.
447447
*
448448
*
@@ -603,7 +603,7 @@ public String toInstantiationType(Property p) {
603603
* @param p Swagger property object
604604
* @return string presentation of the example value of the property
605605
*/
606-
@SuppressWarnings("static-method")
606+
@SuppressWarnings("static-method")
607607
public String toExampleValue(Property p) {
608608
if(p.getExample() != null) {
609609
return p.getExample().toString();
@@ -689,7 +689,7 @@ public String toDefaultValue(Property p) {
689689
return "null";
690690
}
691691
}
692-
692+
693693
/**
694694
* Return the property initialized from a data object
695695
* Useful for initialization with a plain object in Javascript
@@ -965,7 +965,7 @@ public String getterAndSetterCapitalize(String name) {
965965
return camelize(toVarName(name));
966966

967967
}
968-
968+
969969
/**
970970
* Convert Swagger Property object to Codegen Property object
971971
*
@@ -1032,7 +1032,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10321032
property.allowableValues = allowableValues;
10331033
}
10341034
}
1035-
1035+
10361036
if (p instanceof IntegerProperty) {
10371037
IntegerProperty sp = (IntegerProperty) p;
10381038
property.isInteger = true;
@@ -1050,7 +1050,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10501050
property.allowableValues = allowableValues;
10511051
}
10521052
}
1053-
1053+
10541054
if (p instanceof LongProperty) {
10551055
LongProperty sp = (LongProperty) p;
10561056
property.isLong = true;
@@ -1068,7 +1068,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10681068
property.allowableValues = allowableValues;
10691069
}
10701070
}
1071-
1071+
10721072
if (p instanceof BooleanProperty) {
10731073
property.isBoolean = true;
10741074
}
@@ -1098,7 +1098,7 @@ public CodegenProperty fromProperty(String name, Property p) {
10981098
property.allowableValues = allowableValues;
10991099
}
11001100
}
1101-
1101+
11021102
if (p instanceof FloatProperty) {
11031103
FloatProperty sp = (FloatProperty) p;
11041104
property.isFloat = true;
@@ -1116,7 +1116,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11161116
property.allowableValues = allowableValues;
11171117
}
11181118
}
1119-
1119+
11201120
if (p instanceof DateProperty) {
11211121
DateProperty sp = (DateProperty) p;
11221122
property.isDate = true;
@@ -1134,7 +1134,7 @@ public CodegenProperty fromProperty(String name, Property p) {
11341134
property.allowableValues = allowableValues;
11351135
}
11361136
}
1137-
1137+
11381138
if (p instanceof DateTimeProperty) {
11391139
DateTimeProperty sp = (DateTimeProperty) p;
11401140
property.isDateTime = true;
@@ -1229,7 +1229,7 @@ private static Response findMethodResponse(Map<String, Response> responses) {
12291229
}
12301230
return responses.get(code);
12311231
}
1232-
1232+
12331233
/**
12341234
* Convert Swagger Operation object to Codegen Operation object (without providing a Swagger object)
12351235
*
@@ -1242,7 +1242,7 @@ private static Response findMethodResponse(Map<String, Response> responses) {
12421242
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
12431243
return fromOperation(path, httpMethod, operation, definitions, null);
12441244
}
1245-
1245+
12461246
/**
12471247
* Convert Swagger Operation object to Codegen Operation object
12481248
*
@@ -1277,11 +1277,11 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
12771277
// empty list, do nothing to override global setting
12781278
}
12791279
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
1280-
// use consumes defined globally
1280+
// use consumes defined globally
12811281
consumes = swagger.getConsumes();
12821282
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
12831283
}
1284-
1284+
12851285
// if "consumes" is defined (per operation or using global definition)
12861286
if (consumes != null && consumes.size() > 0) {
12871287
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
@@ -1310,11 +1310,11 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
13101310
// empty list, do nothing to override global setting
13111311
}
13121312
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
1313-
// use produces defined globally
1313+
// use produces defined globally
13141314
produces = swagger.getProduces();
13151315
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
13161316
}
1317-
1317+
13181318
// if "produces" is defined (per operation or using global definition)
13191319
if (produces != null && produces.size() > 0) {
13201320
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
@@ -1556,7 +1556,7 @@ public CodegenResponse fromResponse(String responseCode, Response response) {
15561556
}
15571557
return r;
15581558
}
1559-
1559+
15601560
/**
15611561
* Convert Swagger Parameter object to Codegen Parameter object
15621562
*
@@ -1658,7 +1658,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
16581658
p.maxLength = qp.getMaxLength();
16591659
p.minLength = qp.getMinLength();
16601660
p.pattern = qp.getPattern();
1661-
1661+
16621662
p.maximum = qp.getMaximum();
16631663
p.exclusiveMaximum = qp.isExclusiveMaximum();
16641664
p.minimum = qp.getMinimum();
@@ -1783,14 +1783,14 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
17831783
Map<String, Object> scope = new HashMap<String, Object>();
17841784
scope.put("scope", scopeEntry.getKey());
17851785
scope.put("description", scopeEntry.getValue());
1786-
1786+
17871787
count += 1;
17881788
if (count < numScopes) {
17891789
scope.put("hasMore", "true");
17901790
} else {
17911791
scope.put("hasMore", null);
17921792
}
1793-
1793+
17941794
scopes.add(scope);
17951795
}
17961796
sec.scopes = scopes;
@@ -1803,6 +1803,17 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
18031803
return secs;
18041804
}
18051805

1806+
protected void setReservedWordsLowerCase(List<String> words) {
1807+
reservedWords = new HashSet<String>();
1808+
for (String word : words) {
1809+
reservedWords.add(word.toLowerCase());
1810+
}
1811+
}
1812+
1813+
protected boolean isReservedWord(String word) {
1814+
return word != null && reservedWords.contains(word.toLowerCase());
1815+
}
1816+
18061817
/**
18071818
* Get operationId from the operation object, and if it's blank, generate a new one from the given parameters.
18081819
*
@@ -2046,9 +2057,9 @@ private void addVars(CodegenModel m, Map<String, Property> properties, Collectio
20462057
m.vars.add(cp);
20472058
}
20482059
}
2049-
2060+
20502061
m.mandatory = mandatory;
2051-
2062+
20522063
} else {
20532064
m.emptyVars = true;
20542065
m.hasVars = false;
@@ -2077,7 +2088,7 @@ public String apply(String input) {
20772088
}
20782089
return name;
20792090
}
2080-
2091+
20812092
/**
20822093
* Camelize name (parameter, property, method, etc) with upper case for first letter
20832094
* copied from Twitter elephant bird
@@ -2221,7 +2232,7 @@ public String sanitizeName(String name) {
22212232
// character with _ or empty character. Below aims to spell out different cases we've
22222233
// encountered so far and hopefully make it easier for others to add more special
22232234
// cases in the future.
2224-
2235+
22252236
// better error handling when map/array type is invalid
22262237
if (name == null) {
22272238
LOGGER.error("String to be sanitized is null. Default to ERROR_UNKNOWN");

0 commit comments

Comments
 (0)