Skip to content

Commit b83db8e

Browse files
committed
Merge remote-tracking branch 'upstream/master' into add-auth-annotations-jaxrs
2 parents 7bbb883 + eb1e0b5 commit b83db8e

File tree

68 files changed

+2800
-430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2800
-430
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface CodegenConfig {
6767

6868
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
6969

70+
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
71+
7072
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
7173

7274
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
public class CodegenParameter {
99
public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
10-
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer, secondaryParam, isBinary;
10+
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer,
11+
secondaryParam, isBinary, isCollectionFormatMulti;
1112
public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue;
1213
public String jsonSchema;
1314
public boolean isEnum;
@@ -33,6 +34,7 @@ public CodegenParameter copy() {
3334
output.paramName = this.paramName;
3435
output.dataType = this.dataType;
3536
output.collectionFormat = this.collectionFormat;
37+
output.isCollectionFormatMulti = this.isCollectionFormatMulti;
3638
output.description = this.description;
3739
output.baseType = this.baseType;
3840
output.isFormParam = this.isFormParam;

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

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,12 @@ private Response findMethodResponse(Map<String, Response> responses) {
867867
}
868868
return responses.get(code);
869869
}
870-
870+
871871
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
872+
return fromOperation(path, httpMethod, operation, definitions, null);
873+
}
874+
875+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
872876
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
873877
Set<String> imports = new HashSet<String>();
874878
op.vendorExtensions = operation.getVendorExtensions();
@@ -904,15 +908,32 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
904908
op.summary = escapeText(operation.getSummary());
905909
op.notes = escapeText(operation.getDescription());
906910
op.tags = operation.getTags();
907-
908-
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
911+
op.hasConsumes = false;
912+
op.hasProduces = false;
913+
914+
List<String> consumes = new ArrayList<String>();
915+
if (operation.getConsumes() != null) {
916+
if (operation.getConsumes().size() > 0) {
917+
// use consumes defined in the operation
918+
consumes = operation.getConsumes();
919+
} else {
920+
// empty list, do nothing to override global setting
921+
}
922+
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
923+
// use consumes defined globally
924+
consumes = swagger.getConsumes();
925+
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
926+
}
927+
928+
// if "consumes" is defined (per operation or using global definition)
929+
if (consumes != null && consumes.size() > 0) {
909930
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
910931
int count = 0;
911-
for (String key : operation.getConsumes()) {
932+
for (String key : consumes) {
912933
Map<String, String> mediaType = new HashMap<String, String>();
913934
mediaType.put("mediaType", key);
914935
count += 1;
915-
if (count < operation.getConsumes().size()) {
936+
if (count < consumes.size()) {
916937
mediaType.put("hasMore", "true");
917938
} else {
918939
mediaType.put("hasMore", null);
@@ -923,14 +944,29 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
923944
op.hasConsumes = true;
924945
}
925946

926-
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
947+
List<String> produces = new ArrayList<String>();
948+
if (operation.getProduces() != null) {
949+
if (operation.getProduces().size() > 0) {
950+
// use produces defined in the operation
951+
produces = operation.getProduces();
952+
} else {
953+
// empty list, do nothing to override global setting
954+
}
955+
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
956+
// use produces defined globally
957+
produces = swagger.getProduces();
958+
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
959+
}
960+
961+
// if "produces" is defined (per operation or using global definition)
962+
if (produces != null && produces.size() > 0) {
927963
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
928964
int count = 0;
929-
for (String key : operation.getProduces()) {
965+
for (String key : produces) {
930966
Map<String, String> mediaType = new HashMap<String, String>();
931967
mediaType.put("mediaType", key);
932968
count += 1;
933-
if (count < operation.getProduces().size()) {
969+
if (count < produces.size()) {
934970
mediaType.put("hasMore", "true");
935971
} else {
936972
mediaType.put("hasMore", null);
@@ -1044,7 +1080,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10441080
}
10451081
}
10461082
for (String i : imports) {
1047-
if (!defaultIncludes.contains(i) && !languageSpecificPrimitives.contains(i)) {
1083+
if (needToImport(i)) {
10481084
op.imports.add(i);
10491085
}
10501086
}
@@ -1205,6 +1241,9 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
12051241
p._enum = model._enum;
12061242
p.allowableValues = model.allowableValues;
12071243
p.collectionFormat = collectionFormat;
1244+
if(collectionFormat != null && collectionFormat.equals("multi")) {
1245+
p.isCollectionFormatMulti = true;
1246+
}
12081247
p.paramName = toParamName(qp.getName());
12091248

12101249
if (model.complexType != null) {
@@ -1333,6 +1372,12 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
13331372
return secs;
13341373
}
13351374

1375+
protected boolean needToImport(String type) {
1376+
return !defaultIncludes.contains(type)
1377+
&& !languageSpecificPrimitives.contains(type)
1378+
&& type.indexOf(".") < 0;
1379+
}
1380+
13361381
protected List<Map<String, Object>> toExamples(Map<String, Object> examples) {
13371382
if (examples == null) {
13381383
return null;
@@ -1436,7 +1481,7 @@ public static String underscore(String word) {
14361481
}
14371482

14381483
private void addImport(CodegenModel m, String type) {
1439-
if (type != null && !languageSpecificPrimitives.contains(type) && !defaultIncludes.contains(type)) {
1484+
if (type != null && needToImport(type)) {
14401485
m.imports.add(type);
14411486
}
14421487
}
@@ -1615,8 +1660,8 @@ protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries
16151660
* @return sanitized string
16161661
*/
16171662
public String sanitizeName(String name) {
1618-
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
1619-
// character with _ or empty character. Below aims to spell out different cases we've
1663+
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
1664+
// character with _ or empty character. Below aims to spell out different cases we've
16201665
// encountered so far and hopefully make it easier for others to add more special
16211666
// cases in the future.
16221667

@@ -1639,7 +1684,7 @@ public String sanitizeName(String name) {
16391684

16401685
// input name and age => input_name_and_age
16411686
name = name.replaceAll(" ", "_");
1642-
1687+
16431688
// remove everything else other than word, number and _
16441689
// $php_variable => php_variable
16451690
return name.replaceAll("[^a-zA-Z0-9_]", "");

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.swagger.models.Model;
1313
import io.swagger.models.Operation;
1414
import io.swagger.models.Path;
15+
import io.swagger.models.SecurityRequirement;
1516
import io.swagger.models.Swagger;
1617
import io.swagger.models.auth.OAuth2Definition;
1718
import io.swagger.models.auth.SecuritySchemeDefinition;
@@ -470,22 +471,31 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
470471
for (String tag : tags) {
471472
CodegenOperation co = null;
472473
try {
473-
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
474+
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
474475
co.tags = new ArrayList<String>();
475476
co.tags.add(sanitizeTag(tag));
476477
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
477478

478479
List<Map<String, List<String>>> securities = operation.getSecurity();
479-
if (securities == null) {
480+
if (securities == null && swagger.getSecurity() != null) {
481+
securities = new ArrayList<Map<String, List<String>>>();
482+
for (SecurityRequirement sr : swagger.getSecurity()) {
483+
securities.add(sr.getRequirements());
484+
}
485+
}
486+
if (securities == null || securities.isEmpty()) {
480487
continue;
481488
}
482489
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
483-
for (Map<String, List<String>> security : securities) {
484-
if (security.size() != 1) {
485-
//Not sure what to do
486-
continue;
487-
}
488-
String securityName = security.keySet().iterator().next();
490+
// NOTE: Use only the first security requirement for now.
491+
// See the "security" field of "Swagger Object":
492+
// https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object
493+
// "there is a logical OR between the security requirements"
494+
if (securities.size() > 1) {
495+
LOGGER.warn("More than 1 security requirements are found, using only the first one");
496+
}
497+
Map<String, List<String>> security = securities.get(0);
498+
for (String securityName : security.keySet()) {
489499
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
490500
if (securityDefinition != null) {
491501
if(securityDefinition instanceof OAuth2Definition) {
@@ -496,7 +506,7 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
496506
oauth2Operation.setFlow(oauth2Definition.getFlow());
497507
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
498508
oauth2Operation.setScopes(new HashMap<String, String>());
499-
for (String scope : security.values().iterator().next()) {
509+
for (String scope : security.get(securityName)) {
500510
if (oauth2Definition.getScopes().containsKey(scope)) {
501511
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
502512
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ public void processOpts() {
104104
}
105105

106106
additionalProperties.put("clientPackage", clientPackage);
107-
107+
108108
supportingFiles.add(new SupportingFile("Configuration.mustache",
109-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "Configuration.cs"));
109+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "Configuration.cs"));
110110
supportingFiles.add(new SupportingFile("ApiClient.mustache",
111-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiClient.cs"));
111+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "ApiClient.cs"));
112112
supportingFiles.add(new SupportingFile("ApiException.mustache",
113-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiException.cs"));
113+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "ApiException.cs"));
114114
supportingFiles.add(new SupportingFile("Newtonsoft.Json.dll", "bin", "Newtonsoft.Json.dll"));
115115
supportingFiles.add(new SupportingFile("RestSharp.dll", "bin", "RestSharp.dll"));
116116
supportingFiles.add(new SupportingFile("compile.mustache", "", "compile.bat"));
@@ -137,12 +137,11 @@ public String escapeReservedWord(String name) {
137137

138138
@Override
139139
public String apiFileFolder() {
140-
141-
return outputFolder + File.separator + (sourceFolder + File.separator + apiPackage()).replace('.', File.separatorChar);
140+
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar);
142141
}
143142

144143
public String modelFileFolder() {
145-
return outputFolder + File.separator + (sourceFolder + File.separator + modelPackage()).replace('.', File.separatorChar);
144+
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar);
146145
}
147146

148147
@Override

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CsharpDotNet2ClientCodegen.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ public void processOpts() {
108108
}
109109

110110
supportingFiles.add(new SupportingFile("Configuration.mustache",
111-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "Configuration.cs"));
111+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "Configuration.cs"));
112112
supportingFiles.add(new SupportingFile("ApiClient.mustache",
113-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiClient.cs"));
113+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "ApiClient.cs"));
114114
supportingFiles.add(new SupportingFile("ApiException.mustache",
115-
(sourceFolder + File.separator + clientPackage).replace(".", java.io.File.separator), "ApiException.cs"));
115+
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "ApiException.cs"));
116116
supportingFiles.add(new SupportingFile("packages.config.mustache", "vendor", "packages.config"));
117117
supportingFiles.add(new SupportingFile("compile-mono.sh.mustache", "", "compile-mono.sh"));
118118
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
@@ -142,11 +142,11 @@ public String escapeReservedWord(String name) {
142142

143143
@Override
144144
public String apiFileFolder() {
145-
return (outputFolder + File.separator + sourceFolder + File.separator + apiPackage()).replace('.', File.separatorChar);
145+
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar);
146146
}
147147

148148
public String modelFileFolder() {
149-
return (outputFolder + File.separator + sourceFolder + File.separator + modelPackage()).replace('.', File.separatorChar);
149+
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar);
150150
}
151151

152152
@Override

0 commit comments

Comments
 (0)