15
15
import io .swagger .codegen .v3 .SupportingFile ;
16
16
import io .swagger .codegen .v3 .generators .features .BeanValidationFeatures ;
17
17
import io .swagger .codegen .v3 .generators .features .OptionalFeatures ;
18
- import io .swagger .codegen .v3 .templates .MustacheTemplateEngine ;
19
- import io .swagger .codegen .v3 .templates .TemplateEngine ;
20
18
import io .swagger .codegen .v3 .utils .URLPathUtil ;
21
19
import io .swagger .v3 .oas .models .OpenAPI ;
22
20
import io .swagger .v3 .oas .models .Operation ;
41
39
import static io .swagger .codegen .v3 .CodegenConstants .IS_ENUM_EXT_NAME ;
42
40
import static io .swagger .codegen .v3 .generators .handlebars .ExtensionHelper .getBooleanValue ;
43
41
44
- /*
45
- DEPRECATED, Spring Boot 1 is in maintenance mode only. Please use JavaSpring2.
46
- */
47
42
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures , OptionalFeatures {
48
43
static Logger LOGGER = LoggerFactory .getLogger (SpringCodegen .class );
49
44
public static final String DEFAULT_LIBRARY = "spring-boot" ;
@@ -53,7 +48,6 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
53
48
public static final String INTERFACE_ONLY = "interfaceOnly" ;
54
49
public static final String DELEGATE_PATTERN = "delegatePattern" ;
55
50
public static final String SINGLE_CONTENT_TYPES = "singleContentTypes" ;
56
- public static final String JAVA_8 = "java8" ;
57
51
public static final String ASYNC = "async" ;
58
52
public static final String RESPONSE_WRAPPER = "responseWrapper" ;
59
53
public static final String USE_TAGS = "useTags" ;
@@ -63,6 +57,8 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
63
57
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig" ;
64
58
public static final String TARGET_OPENFEIGN = "generateForOpenFeign" ;
65
59
public static final String DEFAULT_INTERFACES = "defaultInterfaces" ;
60
+ public static final String SPRING_BOOT_VERSION = "springBootVersion" ;
61
+ public static final String SPRING_BOOT_VERSION_2 = "springBootV2" ;
66
62
67
63
protected String title = "swagger-petstore" ;
68
64
protected String configPackage = "io.swagger.configuration" ;
@@ -81,6 +77,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
81
77
protected boolean useOptional = false ;
82
78
protected boolean openFeign = false ;
83
79
protected boolean defaultInterfaces = true ;
80
+ protected String springBootVersion = "1.5.22.RELEASE" ;
84
81
85
82
public SpringCodegen () {
86
83
super ();
@@ -102,7 +99,6 @@ public SpringCodegen() {
102
99
cliOptions .add (CliOption .newBoolean (INTERFACE_ONLY , "Whether to generate only API interface stubs without the server files." ));
103
100
cliOptions .add (CliOption .newBoolean (DELEGATE_PATTERN , "Whether to generate the server files using the delegate pattern" ));
104
101
cliOptions .add (CliOption .newBoolean (SINGLE_CONTENT_TYPES , "Whether to select only one produces/consumes content-type by operation." ));
105
- cliOptions .add (CliOption .newBoolean (JAVA_8 , "use java8 default interface" ));
106
102
cliOptions .add (CliOption .newBoolean (ASYNC , "use async Callable controllers" ));
107
103
cliOptions .add (new CliOption (RESPONSE_WRAPPER , "wrap the responses in given type (Future,Callable,CompletableFuture,ListenableFuture,DeferredResult,HystrixCommand,RxObservable,RxSingle or fully qualified type)" ));
108
104
cliOptions .add (CliOption .newBoolean (USE_TAGS , "use tags for creating interface and controller classnames" ));
@@ -124,6 +120,14 @@ public SpringCodegen() {
124
120
library .setEnum (supportedLibraries );
125
121
library .setDefault (DEFAULT_LIBRARY );
126
122
cliOptions .add (library );
123
+
124
+ CliOption springBootVersionOption = new CliOption (SPRING_BOOT_VERSION , "Spring boot version" );
125
+ Map <String , String > springBootEnum = new HashMap <>();
126
+ springBootEnum .put ("1.5.22.RELEASE" , "1.5.22.RELEASE" );
127
+ springBootEnum .put ("2.1.7.RELEASE" , "2.1.7.RELEASE" );
128
+ springBootVersionOption .setEnum (springBootEnum );
129
+ cliOptions .add (springBootVersionOption );
130
+
127
131
}
128
132
129
133
@ Override
@@ -147,15 +151,23 @@ public void processOpts() {
147
151
additionalProperties .put (CodegenConstants .USE_OAS2 , true );
148
152
149
153
// Process java8 option before common java ones to change the default dateLibrary to java8.
150
- if (additionalProperties .containsKey (JAVA_8 )) {
151
- this .setJava8 (Boolean .valueOf (additionalProperties .get (JAVA_8 ).toString ()));
154
+ if (additionalProperties .containsKey (JAVA8_MODE )) {
155
+ this .setJava8 (Boolean .valueOf (additionalProperties .get (JAVA8_MODE ).toString ()));
156
+ }
157
+
158
+ if (additionalProperties .containsKey (DATE_LIBRARY )) {
159
+ if (additionalProperties .get (DATE_LIBRARY ).toString ().startsWith ("java8" )) {
160
+ this .setJava8 (Boolean .valueOf (additionalProperties .get (JAVA8_MODE ).toString ()));
161
+ }
152
162
}
153
163
if (this .java8 ) {
154
164
additionalProperties .put ("javaVersion" , "1.8" );
155
- additionalProperties .put ("jdk8" , " true" );
165
+ additionalProperties .put ("jdk8" , true );
156
166
if (!additionalProperties .containsKey (DATE_LIBRARY )) {
157
167
setDateLibrary ("java8" );
158
168
}
169
+ } else {
170
+ this .defaultInterfaces = false ;
159
171
}
160
172
161
173
// set invokerPackage as basePackage
@@ -201,10 +213,6 @@ public void processOpts() {
201
213
this .setSingleContentTypes (Boolean .valueOf (additionalProperties .get (SINGLE_CONTENT_TYPES ).toString ()));
202
214
}
203
215
204
- if (additionalProperties .containsKey (JAVA_8 )) {
205
- this .setJava8 (Boolean .valueOf (additionalProperties .get (JAVA_8 ).toString ()));
206
- }
207
-
208
216
if (additionalProperties .containsKey (ASYNC )) {
209
217
this .setAsync (Boolean .valueOf (additionalProperties .get (ASYNC ).toString ()));
210
218
}
@@ -231,7 +239,16 @@ public void processOpts() {
231
239
232
240
if (additionalProperties .containsKey (DEFAULT_INTERFACES )) {
233
241
this .setDefaultInterfaces (Boolean .valueOf (additionalProperties .get (DEFAULT_INTERFACES ).toString ()));
234
- additionalProperties .put (DEFAULT_INTERFACES , this .defaultInterfaces );
242
+ }
243
+ additionalProperties .put (DEFAULT_INTERFACES , this .defaultInterfaces );
244
+
245
+ if (additionalProperties .containsKey (SPRING_BOOT_VERSION )) {
246
+ this .springBootVersion = additionalProperties .get (SPRING_BOOT_VERSION ).toString ();
247
+ }
248
+ additionalProperties .put (SPRING_BOOT_VERSION , this .springBootVersion );
249
+ if (springBootVersion .startsWith ("2" )) {
250
+ additionalProperties .put (SPRING_BOOT_VERSION_2 , true );
251
+ this .setOpenFeign (true );
235
252
}
236
253
237
254
if (useBeanValidation ) {
@@ -260,7 +277,7 @@ public void processOpts() {
260
277
} else {
261
278
throw new IllegalArgumentException (
262
279
String .format ("Can not generate code with `%s` and `%s` true while `%s` is false." ,
263
- DELEGATE_PATTERN , INTERFACE_ONLY , JAVA_8 ));
280
+ DELEGATE_PATTERN , INTERFACE_ONLY , JAVA8_MODE ));
264
281
}
265
282
}
266
283
@@ -270,6 +287,7 @@ public void processOpts() {
270
287
if (!this .interfaceOnly ) {
271
288
272
289
if (library .equals (DEFAULT_LIBRARY )) {
290
+ apiTestTemplateFiles .clear ();
273
291
supportingFiles .add (new SupportingFile ("homeController.mustache" ,
274
292
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "HomeController.java" ));
275
293
supportingFiles .add (new SupportingFile ("swagger2SpringBoot.mustache" ,
@@ -296,13 +314,24 @@ public void processOpts() {
296
314
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "ApiKeyRequestInterceptor.java" ));
297
315
supportingFiles .add (new SupportingFile ("clientConfiguration.mustache" ,
298
316
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "ClientConfiguration.java" ));
299
- supportingFiles .add (new SupportingFile ("Application.mustache" ,
300
- (testFolder + File .separator + basePackage ).replace ("." , java .io .File .separator ), "Application.java" ));
301
317
apiTemplateFiles .put ("apiClient.mustache" , "Client.java" );
302
318
if (!additionalProperties .containsKey (SINGLE_CONTENT_TYPES )) {
303
319
additionalProperties .put (SINGLE_CONTENT_TYPES , "true" );
304
320
this .setSingleContentTypes (true );
305
321
}
322
+ if (additionalProperties .containsKey (CodegenConstants .GENERATE_API_TESTS )) {
323
+ if (Boolean .valueOf (additionalProperties .get (CodegenConstants .GENERATE_API_TESTS ).toString ())) {
324
+ // TODO Api Tests are not currently supported in spring-cloud template
325
+ apiTestTemplateFiles .clear ();
326
+ supportingFiles .add (new SupportingFile ("application-test.mustache" ,
327
+ ("src.test.resources" ).replace ("." , java .io .File .separator ), "application.yml" ));
328
+ supportingFiles .add (new SupportingFile ("TestUtils.mustache" ,
329
+ (testFolder + File .separator + basePackage ).replace ("." , java .io .File .separator ), "TestUtils.java" ));
330
+ supportingFiles .add (new SupportingFile ("Application.mustache" ,
331
+ (testFolder + File .separator + basePackage ).replace ("." , java .io .File .separator ), "Application.java" ));
332
+
333
+ }
334
+ }
306
335
} else {
307
336
apiTemplateFiles .put ("apiController.mustache" , "Controller.java" );
308
337
supportingFiles .add (new SupportingFile ("apiException.mustache" ,
@@ -321,6 +350,9 @@ public void processOpts() {
321
350
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "SwaggerDocumentationConfig.java" ));
322
351
}
323
352
353
+ if (this .interfaceOnly ) {
354
+ apiTestTemplateFiles .clear ();
355
+ }
324
356
if ("threetenbp" .equals (dateLibrary )) {
325
357
supportingFiles .add (new SupportingFile ("customInstantDeserializer.mustache" ,
326
358
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "CustomInstantDeserializer.java" ));
@@ -341,8 +373,6 @@ public void processOpts() {
341
373
}
342
374
343
375
if (this .java8 ) {
344
- additionalProperties .put ("javaVersion" , "1.8" );
345
- additionalProperties .put ("jdk8" , "true" );
346
376
if (this .async ) {
347
377
additionalProperties .put (RESPONSE_WRAPPER , "CompletableFuture" );
348
378
}
@@ -351,7 +381,7 @@ public void processOpts() {
351
381
}
352
382
353
383
if (this .openFeign ){
354
- additionalProperties .put ("isOpenFeign" , " true" );
384
+ additionalProperties .put ("isOpenFeign" , true );
355
385
}
356
386
357
387
// Some well-known Spring or Spring-Cloud response wrappers
@@ -393,6 +423,10 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
393
423
writer .write (fragment .execute ().replaceAll ("\\ r|\\ n" , "" ));
394
424
}
395
425
});
426
+
427
+ if ((this .java8 && !this .defaultInterfaces ) || !this .java8 ) {
428
+ additionalProperties .put ("fullController" , true );
429
+ }
396
430
}
397
431
398
432
@ Override
0 commit comments