Skip to content

Commit 612afe1

Browse files
committed
2 parents 61b2d21 + 9830e82 commit 612afe1

File tree

90 files changed

+3722
-365
lines changed

Some content is hidden

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

90 files changed

+3722
-365
lines changed

bin/all-petstore.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fi
2020
cd $APP_DIR
2121
./bin/akka-scala-petstore.sh
2222
./bin/android-petstore.sh
23+
./bin/aspnet5-petstore-server.sh
2324
./bin/clojure-petstore.sh
2425
./bin/csharp-petstore.sh
2526
./bin/dynamic-html.sh
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "spring-cloud",
3+
"artifactId": "swagger-petstore-spring-cloud"
4+
}

bin/spring-cloud-feign-petstore.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l spring -c bin/spring-cloud-feign-petstore.json -o samples/client/petstore/spring-cloud -DhideGenerationTimestamp=true"
30+
31+
echo "Removing files and folders under samples/client/petstore/spring-cloud/src/main"
32+
rm -rf samples/client/petstore/spring-cloud/src/main
33+
find samples/client/petstore/spring-cloud -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
34+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "spring-mvc",
3+
"artifactId": "swagger-spring-mvc-server"
4+
}

bin/spring-mvc-petstore-server.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l spring --library spring-mvc -o samples/server/petstore/spring-mvc -DhideGenerationTimestamp=true"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l spring -c bin/spring-mvc-petstore-server.json -o samples/server/petstore/spring-mvc -DhideGenerationTimestamp=true"
3030

3131
java $JAVA_OPTS -jar $executable $ags

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CodegenSecurity {
1313
// Oauth specific
1414
public String flow, authorizationUrl, tokenUrl;
1515
public List<Map<String, Object>> scopes;
16+
public Boolean isCode, isPassword, isApplication, isImplicit;
1617

1718
@Override
1819
public String toString() {
@@ -50,6 +51,14 @@ public boolean equals(Object o) {
5051
return false;
5152
if (tokenUrl != null ? !tokenUrl.equals(that.tokenUrl) : that.tokenUrl != null)
5253
return false;
54+
if (isCode != null ? !isCode.equals(that.isCode) : that.isCode != null)
55+
return false;
56+
if (isPassword != null ? !isPassword.equals(that.isPassword) : that.isPassword != null)
57+
return false;
58+
if (isApplication != null ? !isApplication.equals(that.isApplication) : that.isApplication != null)
59+
return false;
60+
if (isImplicit != null ? !isImplicit.equals(that.isImplicit) : that.isImplicit != null)
61+
return false;
5362
return scopes != null ? scopes.equals(that.scopes) : that.scopes == null;
5463

5564
}
@@ -68,6 +77,10 @@ public int hashCode() {
6877
result = 31 * result + (flow != null ? flow.hashCode() : 0);
6978
result = 31 * result + (authorizationUrl != null ? authorizationUrl.hashCode() : 0);
7079
result = 31 * result + (tokenUrl != null ? tokenUrl.hashCode() : 0);
80+
result = 31 * result + (isCode != null ? isCode.hashCode() : 0);
81+
result = 31 * result + (isPassword != null ? isPassword.hashCode() : 0);
82+
result = 31 * result + (isApplication != null ? isApplication.hashCode() : 0);
83+
result = 31 * result + (isImplicit != null ? isImplicit.hashCode() : 0);
7184
result = 31 * result + (scopes != null ? scopes.hashCode() : 0);
7285
return result;
7386
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,7 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
23512351
CodegenSecurity sec = CodegenModelFactory.newInstance(CodegenModelType.SECURITY);
23522352
sec.name = entry.getKey();
23532353
sec.type = schemeDefinition.getType();
2354+
sec.isCode = sec.isPassword = sec.isApplication = sec.isImplicit = false;
23542355

23552356
if (schemeDefinition instanceof ApiKeyAuthDefinition) {
23562357
final ApiKeyAuthDefinition apiKeyDefinition = (ApiKeyAuthDefinition) schemeDefinition;
@@ -2367,6 +2368,22 @@ public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition>
23672368
sec.isKeyInHeader = sec.isKeyInQuery = sec.isApiKey = sec.isBasic = false;
23682369
sec.isOAuth = true;
23692370
sec.flow = oauth2Definition.getFlow();
2371+
switch(sec.flow) {
2372+
case "accessCode":
2373+
sec.isCode = true;
2374+
break;
2375+
case "password":
2376+
sec.isPassword = true;
2377+
break;
2378+
case "application":
2379+
sec.isApplication = true;
2380+
break;
2381+
case "implicit":
2382+
sec.isImplicit = true;
2383+
break;
2384+
default:
2385+
throw new RuntimeException("unknown oauth flow: " + sec.flow);
2386+
}
23702387
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
23712388
sec.tokenUrl = oauth2Definition.getTokenUrl();
23722389
if (oauth2Definition.getScopes() != null) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,22 @@ public void processOpts() {
8484
apiPackage = packageName + ".Controllers";
8585
modelPackage = packageName + ".Models";
8686

87+
supportingFiles.add(new SupportingFile("NuGet.Config", "", "NuGet.Config"));
8788
supportingFiles.add(new SupportingFile("global.json", "", "global.json"));
88-
supportingFiles.add(new SupportingFile("build.mustache", "", "build.sh"));
89+
supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh"));
90+
supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat"));
91+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
92+
supportingFiles.add(new SupportingFile("Solution.mustache", "", this.packageName + ".sln"));
8993
supportingFiles.add(new SupportingFile("Dockerfile.mustache", this.sourceFolder, "Dockerfile"));
9094
supportingFiles.add(new SupportingFile("gitignore", this.sourceFolder, ".gitignore"));
9195
supportingFiles.add(new SupportingFile("appsettings.json", this.sourceFolder, "appsettings.json"));
9296

93-
supportingFiles.add(new SupportingFile("project.mustache", this.sourceFolder, "project.json"));
97+
supportingFiles.add(new SupportingFile("project.json.mustache", this.sourceFolder, "project.json"));
9498
supportingFiles.add(new SupportingFile("Startup.mustache", this.sourceFolder, "Startup.cs"));
99+
supportingFiles.add(new SupportingFile("Program.mustache", this.sourceFolder, "Program.cs"));
100+
supportingFiles.add(new SupportingFile("web.config", this.sourceFolder, "web.config"));
101+
102+
supportingFiles.add(new SupportingFile("Project.xproj.mustache", this.sourceFolder, this.packageName + ".xproj"));
95103

96104
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", this.sourceFolder + File.separator + "Properties", "launchSettings.json"));
97105

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

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
public class SpringCodegen extends AbstractJavaCodegen {
1313
public static final String DEFAULT_LIBRARY = "spring-boot";
14+
public static final String TITLE = "title";
1415
public static final String CONFIG_PACKAGE = "configPackage";
1516
public static final String BASE_PACKAGE = "basePackage";
1617
public static final String INTERFACE_ONLY = "interfaceOnly";
1718
public static final String SINGLE_CONTENT_TYPES = "singleContentTypes";
1819
public static final String JAVA_8 = "java8";
1920
public static final String ASYNC = "async";
20-
protected String title = "Petstore Server";
21+
public static final String SPRING_MVC_LIBRARY = "spring-mvc";
22+
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
23+
24+
protected String title = "swagger-petstore";
2125
protected String configPackage = "io.swagger.configuration";
2226
protected String basePackage = "io.swagger";
2327
protected boolean interfaceOnly = false;
@@ -33,12 +37,12 @@ public SpringCodegen() {
3337
apiPackage = "io.swagger.api";
3438
modelPackage = "io.swagger.model";
3539
invokerPackage = "io.swagger.api";
36-
artifactId = "swagger-spring-server";
40+
artifactId = "swagger-spring";
3741

38-
additionalProperties.put("title", title);
3942
additionalProperties.put(CONFIG_PACKAGE, configPackage);
4043
additionalProperties.put(BASE_PACKAGE, basePackage);
4144

45+
cliOptions.add(new CliOption(TITLE, "server title name or client service name"));
4246
cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code"));
4347
cliOptions.add(new CliOption(BASE_PACKAGE, "base package for generated code"));
4448
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files."));
@@ -47,7 +51,8 @@ public SpringCodegen() {
4751
cliOptions.add(CliOption.newBoolean(ASYNC, "use async Callable controllers"));
4852

4953
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
50-
supportedLibraries.put("spring-mvc", "Spring-MVC Server application using the SpringFox integration.");
54+
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
55+
supportedLibraries.put(SPRING_CLOUD_LIBRARY, "Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");
5156
setLibrary(DEFAULT_LIBRARY);
5257

5358
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
@@ -82,6 +87,10 @@ public void processOpts() {
8287
modelDocTemplateFiles.remove("model_doc.mustache");
8388
apiDocTemplateFiles.remove("api_doc.mustache");
8489

90+
if (additionalProperties.containsKey(TITLE)) {
91+
this.setTitle((String) additionalProperties.get(TITLE));
92+
}
93+
8594
if (additionalProperties.containsKey(CONFIG_PACKAGE)) {
8695
this.setConfigPackage((String) additionalProperties.get(CONFIG_PACKAGE));
8796
}
@@ -110,17 +119,6 @@ public void processOpts() {
110119
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
111120

112121
if (!this.interfaceOnly) {
113-
apiTemplateFiles.put("apiController.mustache", "Controller.java");
114-
supportingFiles.add(new SupportingFile("apiException.mustache",
115-
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiException.java"));
116-
supportingFiles.add(new SupportingFile("apiOriginFilter.mustache",
117-
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiOriginFilter.java"));
118-
supportingFiles.add(new SupportingFile("apiResponseMessage.mustache",
119-
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiResponseMessage.java"));
120-
supportingFiles.add(new SupportingFile("notFoundException.mustache",
121-
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "NotFoundException.java"));
122-
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
123-
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
124122
if (library.equals(DEFAULT_LIBRARY)) {
125123
supportingFiles.add(new SupportingFile("homeController.mustache",
126124
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java"));
@@ -129,7 +127,7 @@ public void processOpts() {
129127
supportingFiles.add(new SupportingFile("application.properties",
130128
("src.main.resources").replace(".", java.io.File.separator), "application.properties"));
131129
}
132-
if (library.equals("spring-mvc")) {
130+
if (library.equals(SPRING_MVC_LIBRARY)) {
133131
supportingFiles.add(new SupportingFile("webApplication.mustache",
134132
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebApplication.java"));
135133
supportingFiles.add(new SupportingFile("webMvcConfiguration.mustache",
@@ -139,6 +137,31 @@ public void processOpts() {
139137
supportingFiles.add(new SupportingFile("application.properties",
140138
("src.main.resources").replace(".", java.io.File.separator), "swagger.properties"));
141139
}
140+
if (library.equals(SPRING_CLOUD_LIBRARY)) {
141+
supportingFiles.add(new SupportingFile("apiKeyRequestInterceptor.mustache",
142+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "ApiKeyRequestInterceptor.java"));
143+
supportingFiles.add(new SupportingFile("clientConfiguration.mustache",
144+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "ClientConfiguration.java"));
145+
apiTemplateFiles.put("apiClient.mustache", "Client.java");
146+
if (!additionalProperties.containsKey(SINGLE_CONTENT_TYPES)) {
147+
additionalProperties.put(SINGLE_CONTENT_TYPES, "true");
148+
this.setSingleContentTypes(true);
149+
150+
}
151+
152+
} else {
153+
apiTemplateFiles.put("apiController.mustache", "Controller.java");
154+
supportingFiles.add(new SupportingFile("apiException.mustache",
155+
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiException.java"));
156+
supportingFiles.add(new SupportingFile("apiResponseMessage.mustache",
157+
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiResponseMessage.java"));
158+
supportingFiles.add(new SupportingFile("notFoundException.mustache",
159+
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "NotFoundException.java"));
160+
supportingFiles.add(new SupportingFile("apiOriginFilter.mustache",
161+
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiOriginFilter.java"));
162+
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
163+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
164+
}
142165
}
143166

144167
if (this.java8) {
@@ -182,6 +205,22 @@ public void preprocessSwagger(Swagger swagger) {
182205
swagger.setBasePath("");
183206
}
184207

208+
if(!additionalProperties.containsKey(TITLE)) {
209+
// From the title, compute a reasonable name for the package and the API
210+
String title = swagger.getInfo().getTitle();
211+
212+
// Drop any API suffix
213+
if (title != null) {
214+
title = title.trim().replace(" ", "-");
215+
if (title.toUpperCase().endsWith("API")) {
216+
title = title.substring(0, title.length() - 3);
217+
}
218+
219+
this.title = camelize(sanitizeName(title), true);
220+
}
221+
additionalProperties.put(TITLE, this.title);
222+
}
223+
185224
String host = swagger.getHost();
186225
String port = "8080";
187226
if (host != null) {
@@ -265,6 +304,19 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
265304
return objs;
266305
}
267306

307+
@Override
308+
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
309+
if(library.equals(SPRING_CLOUD_LIBRARY)) {
310+
List<CodegenSecurity> authMethods = (List<CodegenSecurity>) objs.get("authMethods");
311+
if (authMethods != null) {
312+
for (CodegenSecurity authMethod : authMethods) {
313+
authMethod.name = camelize(sanitizeName(authMethod.name), true);
314+
}
315+
}
316+
}
317+
return objs;
318+
}
319+
268320
@Override
269321
public String toApiName(String name) {
270322
if (name.length() == 0) {
@@ -274,6 +326,10 @@ public String toApiName(String name) {
274326
return camelize(name) + "Api";
275327
}
276328

329+
public void setTitle(String title) {
330+
this.title = title;
331+
}
332+
277333
public void setConfigPackage(String configPackage) {
278334
this.configPackage = configPackage;
279335
}
@@ -331,4 +387,5 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
331387

332388
return objs;
333389
}
390+
334391
}

0 commit comments

Comments
 (0)