11
11
12
12
public class SpringCodegen extends AbstractJavaCodegen {
13
13
public static final String DEFAULT_LIBRARY = "spring-boot" ;
14
+ public static final String TITLE = "title" ;
14
15
public static final String CONFIG_PACKAGE = "configPackage" ;
15
16
public static final String BASE_PACKAGE = "basePackage" ;
16
17
public static final String INTERFACE_ONLY = "interfaceOnly" ;
17
18
public static final String SINGLE_CONTENT_TYPES = "singleContentTypes" ;
18
19
public static final String JAVA_8 = "java8" ;
19
20
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" ;
21
25
protected String configPackage = "io.swagger.configuration" ;
22
26
protected String basePackage = "io.swagger" ;
23
27
protected boolean interfaceOnly = false ;
@@ -33,12 +37,12 @@ public SpringCodegen() {
33
37
apiPackage = "io.swagger.api" ;
34
38
modelPackage = "io.swagger.model" ;
35
39
invokerPackage = "io.swagger.api" ;
36
- artifactId = "swagger-spring-server " ;
40
+ artifactId = "swagger-spring" ;
37
41
38
- additionalProperties .put ("title" , title );
39
42
additionalProperties .put (CONFIG_PACKAGE , configPackage );
40
43
additionalProperties .put (BASE_PACKAGE , basePackage );
41
44
45
+ cliOptions .add (new CliOption (TITLE , "server title name or client service name" ));
42
46
cliOptions .add (new CliOption (CONFIG_PACKAGE , "configuration package for generated code" ));
43
47
cliOptions .add (new CliOption (BASE_PACKAGE , "base package for generated code" ));
44
48
cliOptions .add (CliOption .newBoolean (INTERFACE_ONLY , "Whether to generate only API interface stubs without the server files." ));
@@ -47,7 +51,8 @@ public SpringCodegen() {
47
51
cliOptions .add (CliOption .newBoolean (ASYNC , "use async Callable controllers" ));
48
52
49
53
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." );
51
56
setLibrary (DEFAULT_LIBRARY );
52
57
53
58
CliOption library = new CliOption (CodegenConstants .LIBRARY , "library template (sub-template) to use" );
@@ -82,6 +87,10 @@ public void processOpts() {
82
87
modelDocTemplateFiles .remove ("model_doc.mustache" );
83
88
apiDocTemplateFiles .remove ("api_doc.mustache" );
84
89
90
+ if (additionalProperties .containsKey (TITLE )) {
91
+ this .setTitle ((String ) additionalProperties .get (TITLE ));
92
+ }
93
+
85
94
if (additionalProperties .containsKey (CONFIG_PACKAGE )) {
86
95
this .setConfigPackage ((String ) additionalProperties .get (CONFIG_PACKAGE ));
87
96
}
@@ -110,17 +119,6 @@ public void processOpts() {
110
119
supportingFiles .add (new SupportingFile ("README.mustache" , "" , "README.md" ));
111
120
112
121
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" ));
124
122
if (library .equals (DEFAULT_LIBRARY )) {
125
123
supportingFiles .add (new SupportingFile ("homeController.mustache" ,
126
124
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "HomeController.java" ));
@@ -129,7 +127,7 @@ public void processOpts() {
129
127
supportingFiles .add (new SupportingFile ("application.properties" ,
130
128
("src.main.resources" ).replace ("." , java .io .File .separator ), "application.properties" ));
131
129
}
132
- if (library .equals ("spring-mvc" )) {
130
+ if (library .equals (SPRING_MVC_LIBRARY )) {
133
131
supportingFiles .add (new SupportingFile ("webApplication.mustache" ,
134
132
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "WebApplication.java" ));
135
133
supportingFiles .add (new SupportingFile ("webMvcConfiguration.mustache" ,
@@ -139,6 +137,31 @@ public void processOpts() {
139
137
supportingFiles .add (new SupportingFile ("application.properties" ,
140
138
("src.main.resources" ).replace ("." , java .io .File .separator ), "swagger.properties" ));
141
139
}
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
+ }
142
165
}
143
166
144
167
if (this .java8 ) {
@@ -182,6 +205,22 @@ public void preprocessSwagger(Swagger swagger) {
182
205
swagger .setBasePath ("" );
183
206
}
184
207
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
+
185
224
String host = swagger .getHost ();
186
225
String port = "8080" ;
187
226
if (host != null ) {
@@ -265,6 +304,19 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
265
304
return objs ;
266
305
}
267
306
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
+
268
320
@ Override
269
321
public String toApiName (String name ) {
270
322
if (name .length () == 0 ) {
@@ -274,6 +326,10 @@ public String toApiName(String name) {
274
326
return camelize (name ) + "Api" ;
275
327
}
276
328
329
+ public void setTitle (String title ) {
330
+ this .title = title ;
331
+ }
332
+
277
333
public void setConfigPackage (String configPackage ) {
278
334
this .configPackage = configPackage ;
279
335
}
@@ -331,4 +387,5 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
331
387
332
388
return objs ;
333
389
}
390
+
334
391
}
0 commit comments