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" ;
@@ -62,6 +56,9 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
62
56
public static final String IMPLICIT_HEADERS = "implicitHeaders" ;
63
57
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig" ;
64
58
public static final String TARGET_OPENFEIGN = "generateForOpenFeign" ;
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" ;
65
62
66
63
protected String title = "swagger-petstore" ;
67
64
protected String configPackage = "io.swagger.configuration" ;
@@ -79,6 +76,8 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
79
76
protected boolean swaggerDocketConfig = false ;
80
77
protected boolean useOptional = false ;
81
78
protected boolean openFeign = false ;
79
+ protected boolean defaultInterfaces = true ;
80
+ protected String springBootVersion = "1.5.22.RELEASE" ;
82
81
83
82
public SpringCodegen () {
84
83
super ();
@@ -100,7 +99,6 @@ public SpringCodegen() {
100
99
cliOptions .add (CliOption .newBoolean (INTERFACE_ONLY , "Whether to generate only API interface stubs without the server files." ));
101
100
cliOptions .add (CliOption .newBoolean (DELEGATE_PATTERN , "Whether to generate the server files using the delegate pattern" ));
102
101
cliOptions .add (CliOption .newBoolean (SINGLE_CONTENT_TYPES , "Whether to select only one produces/consumes content-type by operation." ));
103
- cliOptions .add (CliOption .newBoolean (JAVA_8 , "use java8 default interface" ));
104
102
cliOptions .add (CliOption .newBoolean (ASYNC , "use async Callable controllers" ));
105
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)" ));
106
104
cliOptions .add (CliOption .newBoolean (USE_TAGS , "use tags for creating interface and controller classnames" ));
@@ -110,6 +108,7 @@ public SpringCodegen() {
110
108
cliOptions .add (CliOption .newBoolean (USE_OPTIONAL ,
111
109
"Use Optional container for optional parameters" ));
112
110
cliOptions .add (CliOption .newBoolean (TARGET_OPENFEIGN ,"Generate for usage with OpenFeign (instead of feign)" ));
111
+ cliOptions .add (CliOption .newBoolean (DEFAULT_INTERFACES , "Generate default implementations for interfaces" ).defaultValue ("true" ));
113
112
114
113
supportedLibraries .put (DEFAULT_LIBRARY , "Spring-boot Server application using the SpringFox integration." );
115
114
supportedLibraries .put (SPRING_MVC_LIBRARY , "Spring-MVC Server application using the SpringFox integration." );
@@ -121,6 +120,14 @@ public SpringCodegen() {
121
120
library .setEnum (supportedLibraries );
122
121
library .setDefault (DEFAULT_LIBRARY );
123
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
+
124
131
}
125
132
126
133
@ Override
@@ -144,15 +151,23 @@ public void processOpts() {
144
151
additionalProperties .put (CodegenConstants .USE_OAS2 , true );
145
152
146
153
// Process java8 option before common java ones to change the default dateLibrary to java8.
147
- if (additionalProperties .containsKey (JAVA_8 )) {
148
- 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
+ }
149
162
}
150
163
if (this .java8 ) {
151
164
additionalProperties .put ("javaVersion" , "1.8" );
152
- additionalProperties .put ("jdk8" , " true" );
165
+ additionalProperties .put ("jdk8" , true );
153
166
if (!additionalProperties .containsKey (DATE_LIBRARY )) {
154
167
setDateLibrary ("java8" );
155
168
}
169
+ } else {
170
+ this .defaultInterfaces = false ;
156
171
}
157
172
158
173
// set invokerPackage as basePackage
@@ -198,10 +213,6 @@ public void processOpts() {
198
213
this .setSingleContentTypes (Boolean .valueOf (additionalProperties .get (SINGLE_CONTENT_TYPES ).toString ()));
199
214
}
200
215
201
- if (additionalProperties .containsKey (JAVA_8 )) {
202
- this .setJava8 (Boolean .valueOf (additionalProperties .get (JAVA_8 ).toString ()));
203
- }
204
-
205
216
if (additionalProperties .containsKey (ASYNC )) {
206
217
this .setAsync (Boolean .valueOf (additionalProperties .get (ASYNC ).toString ()));
207
218
}
@@ -226,6 +237,20 @@ public void processOpts() {
226
237
this .setOpenFeign (convertPropertyToBoolean (TARGET_OPENFEIGN ));
227
238
}
228
239
240
+ if (additionalProperties .containsKey (DEFAULT_INTERFACES )) {
241
+ this .setDefaultInterfaces (Boolean .valueOf (additionalProperties .get (DEFAULT_INTERFACES ).toString ()));
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 );
252
+ }
253
+
229
254
if (useBeanValidation ) {
230
255
writePropertyBack (USE_BEANVALIDATION , useBeanValidation );
231
256
}
@@ -252,7 +277,7 @@ public void processOpts() {
252
277
} else {
253
278
throw new IllegalArgumentException (
254
279
String .format ("Can not generate code with `%s` and `%s` true while `%s` is false." ,
255
- DELEGATE_PATTERN , INTERFACE_ONLY , JAVA_8 ));
280
+ DELEGATE_PATTERN , INTERFACE_ONLY , JAVA8_MODE ));
256
281
}
257
282
}
258
283
@@ -262,6 +287,7 @@ public void processOpts() {
262
287
if (!this .interfaceOnly ) {
263
288
264
289
if (library .equals (DEFAULT_LIBRARY )) {
290
+ apiTestTemplateFiles .clear ();
265
291
supportingFiles .add (new SupportingFile ("homeController.mustache" ,
266
292
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "HomeController.java" ));
267
293
supportingFiles .add (new SupportingFile ("swagger2SpringBoot.mustache" ,
@@ -288,13 +314,24 @@ public void processOpts() {
288
314
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "ApiKeyRequestInterceptor.java" ));
289
315
supportingFiles .add (new SupportingFile ("clientConfiguration.mustache" ,
290
316
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "ClientConfiguration.java" ));
291
- supportingFiles .add (new SupportingFile ("Application.mustache" ,
292
- (testFolder + File .separator + basePackage ).replace ("." , java .io .File .separator ), "Application.java" ));
293
317
apiTemplateFiles .put ("apiClient.mustache" , "Client.java" );
294
318
if (!additionalProperties .containsKey (SINGLE_CONTENT_TYPES )) {
295
319
additionalProperties .put (SINGLE_CONTENT_TYPES , "true" );
296
320
this .setSingleContentTypes (true );
297
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
+ }
298
335
} else {
299
336
apiTemplateFiles .put ("apiController.mustache" , "Controller.java" );
300
337
supportingFiles .add (new SupportingFile ("apiException.mustache" ,
@@ -313,6 +350,9 @@ public void processOpts() {
313
350
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "SwaggerDocumentationConfig.java" ));
314
351
}
315
352
353
+ if (this .interfaceOnly ) {
354
+ apiTestTemplateFiles .clear ();
355
+ }
316
356
if ("threetenbp" .equals (dateLibrary )) {
317
357
supportingFiles .add (new SupportingFile ("customInstantDeserializer.mustache" ,
318
358
(sourceFolder + File .separator + configPackage ).replace ("." , java .io .File .separator ), "CustomInstantDeserializer.java" ));
@@ -333,8 +373,6 @@ public void processOpts() {
333
373
}
334
374
335
375
if (this .java8 ) {
336
- additionalProperties .put ("javaVersion" , "1.8" );
337
- additionalProperties .put ("jdk8" , "true" );
338
376
if (this .async ) {
339
377
additionalProperties .put (RESPONSE_WRAPPER , "CompletableFuture" );
340
378
}
@@ -343,7 +381,7 @@ public void processOpts() {
343
381
}
344
382
345
383
if (this .openFeign ){
346
- additionalProperties .put ("isOpenFeign" , " true" );
384
+ additionalProperties .put ("isOpenFeign" , true );
347
385
}
348
386
349
387
// Some well-known Spring or Spring-Cloud response wrappers
@@ -385,6 +423,10 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
385
423
writer .write (fragment .execute ().replaceAll ("\\ r|\\ n" , "" ));
386
424
}
387
425
});
426
+
427
+ if ((this .java8 && !this .defaultInterfaces ) || !this .java8 ) {
428
+ additionalProperties .put ("fullController" , true );
429
+ }
388
430
}
389
431
390
432
@ Override
@@ -779,4 +821,8 @@ public void setUseOptional(boolean useOptional) {
779
821
public void setOpenFeign (boolean openFeign ) {
780
822
this .openFeign = openFeign ;
781
823
}
824
+
825
+ public void setDefaultInterfaces (boolean defaultInterfaces ) {
826
+ this .defaultInterfaces = defaultInterfaces ;
827
+ }
782
828
}
0 commit comments