1
1
package io .swagger .codegen .v3 .generators .java ;
2
2
3
+ import com .github .jknack .handlebars .Handlebars ;
3
4
import com .github .jknack .handlebars .Lambda ;
4
5
import com .google .common .collect .ImmutableMap ;
5
6
import io .swagger .codegen .v3 .*;
30
31
public class MicronautCodegen extends AbstractJavaCodegen implements BeanValidationFeatures , OptionalFeatures {
31
32
32
33
private static Logger LOGGER = LoggerFactory .getLogger (MicronautCodegen .class );
34
+ private static final String DEFAULT_LIBRARY = "rxjava2" ;
35
+ private static final String RXJAVA3_LIBRARY = "rxjava3" ;
36
+ private static final String REACTOR_LIBRARY = "reactor" ;
33
37
private static final String TITLE = "title" ;
34
38
private static final String CONFIG_PACKAGE = "configPackage" ;
35
39
private static final String BASE_PACKAGE = "basePackage" ;
36
40
private static final String USE_TAGS = "useTags" ;
41
+ private static final String USE_RXJAVA = "useRxJava" ;
42
+ private static final String USE_RXJAVA2 = "useRxJava2" ;
43
+ private static final String USE_RXJAVA3 = "useRxJava3" ;
44
+ private static final String USE_REACTOR = "useReactor" ;
37
45
private static final String IMPLICIT_HEADERS = "implicitHeaders" ;
38
46
private static final String SKIP_SUPPORT_FILES = "skipSupportFiles" ;
39
47
@@ -69,12 +77,18 @@ private void init() {
69
77
cliOptions .add (new CliOption (BASE_PACKAGE , "base package (invokerPackage) for generated code" ));
70
78
cliOptions .add (new CliOption (SKIP_SUPPORT_FILES , "skip support files such as pom.xml, mvnw, etc from code generation." ));
71
79
cliOptions .add (CliOption .newBoolean (USE_TAGS , "use tags for creating interface and controller classnames" ));
72
- cliOptions .add (CliOption .newBoolean (USE_BEANVALIDATION , "Use BeanValidation API annotations" ));
80
+
81
+ CliOption useBeanValidation = CliOption .newBoolean (USE_BEANVALIDATION , "Use BeanValidation API annotations" );
82
+ useBeanValidation .setDefault ("true" );
83
+ cliOptions .add (useBeanValidation );
84
+
73
85
cliOptions .add (CliOption .newBoolean (IMPLICIT_HEADERS , "Use of @ApiImplicitParams for headers." ));
74
86
cliOptions .add (CliOption .newBoolean (USE_OPTIONAL ,
75
87
"Use Optional container for optional parameters" ));
76
88
77
- supportedLibraries .put (DEFAULT_LIBRARY , "Java Micronaut Server application." );
89
+ supportedLibraries .put (DEFAULT_LIBRARY , "Java Micronaut Server application with RxJava2 reactive streams implementation" );
90
+ supportedLibraries .put (RXJAVA3_LIBRARY , "Java Micronaut Server application with RxJava3 reactive streams implementation" );
91
+ supportedLibraries .put (REACTOR_LIBRARY , "Java Micronaut Server application with Project Reactor reactive streams implementation" );
78
92
setLibrary (DEFAULT_LIBRARY );
79
93
80
94
CliOption library = new CliOption (CodegenConstants .LIBRARY , "library template (sub-template) to use" );
@@ -155,17 +169,18 @@ public void processOpts() {
155
169
skipSupportFiles = Boolean .valueOf (additionalProperties .get (SKIP_SUPPORT_FILES ).toString ());
156
170
}
157
171
158
- if (useBeanValidation ) {
159
- writePropertyBack (USE_BEANVALIDATION , useBeanValidation );
160
- }
172
+ writePropertyBack (USE_BEANVALIDATION , useBeanValidation );
161
173
162
174
if (additionalProperties .containsKey (IMPLICIT_HEADERS )) {
163
175
this .setImplicitHeaders (Boolean .valueOf (additionalProperties .get (IMPLICIT_HEADERS ).toString ()));
164
176
}
165
177
166
- if (useOptional ) {
167
- writePropertyBack (USE_OPTIONAL , useOptional );
168
- }
178
+ writePropertyBack (USE_OPTIONAL , useOptional );
179
+
180
+ additionalProperties .put (USE_RXJAVA , isRxJava2Library () || isRxJava3Library ());
181
+ additionalProperties .put (USE_RXJAVA2 , isRxJava2Library ());
182
+ additionalProperties .put (USE_RXJAVA3 , isRxJava3Library ());
183
+ additionalProperties .put (USE_REACTOR , isReactorLibrary ());
169
184
170
185
if (!skipSupportFiles ) {
171
186
supportingFiles .add (new SupportingFile ("pom.mustache" , "" , "pom.xml" ));
@@ -175,6 +190,7 @@ public void processOpts() {
175
190
supportingFiles .add (new SupportingFile ("unsupportedOperationExceptionHandler.mustache" ,
176
191
(sourceFolder + File .separator + configPackage ).replace ("." , File .separator ), "UnsupportedOperationExceptionHandler.java" ));
177
192
supportingFiles .add (new SupportingFile ("mainApplication.mustache" , (sourceFolder + File .separator ).replace ("." , File .separator ), "MainApplication.java" ));
193
+ apiTemplateFiles .put ("apiController.mustache" , "Controller.java" );
178
194
}
179
195
addHandlebarsLambdas (additionalProperties );
180
196
}
@@ -529,4 +545,22 @@ public void setUseBeanValidation(boolean useBeanValidation) {
529
545
public void setUseOptional (boolean useOptional ) {
530
546
this .useOptional = useOptional ;
531
547
}
548
+
549
+ @ Override
550
+ public void addHandlebarHelpers (Handlebars handlebars ) {
551
+ handlebars .setInfiniteLoops (true );
552
+ super .addHandlebarHelpers (handlebars );
553
+ }
554
+
555
+ private boolean isRxJava2Library () {
556
+ return library .equals (DEFAULT_LIBRARY );
557
+ }
558
+
559
+ private boolean isRxJava3Library () {
560
+ return library .equals (RXJAVA3_LIBRARY );
561
+ }
562
+
563
+ private boolean isReactorLibrary () {
564
+ return library .equals (REACTOR_LIBRARY );
565
+ }
532
566
}
0 commit comments