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 = "rxjava3" ;
35
+ private static final String RXJAVA3_LIBRARY = "rxjava3" ;
36
+ private static final String RXJAVA2_LIBRARY = "rxjava2" ;
37
+ private static final String REACTOR_LIBRARY = "reactor" ;
33
38
private static final String TITLE = "title" ;
34
39
private static final String CONFIG_PACKAGE = "configPackage" ;
35
40
private static final String BASE_PACKAGE = "basePackage" ;
36
41
private static final String USE_TAGS = "useTags" ;
42
+ private static final String USE_RXJAVA = "useRxJava" ;
43
+ private static final String USE_RXJAVA2 = "useRxJava2" ;
44
+ private static final String USE_RXJAVA3 = "useRxJava3" ;
45
+ private static final String USE_REACTOR = "useReactor" ;
37
46
private static final String IMPLICIT_HEADERS = "implicitHeaders" ;
38
47
private static final String SKIP_SUPPORT_FILES = "skipSupportFiles" ;
39
48
@@ -69,16 +78,21 @@ private void init() {
69
78
cliOptions .add (new CliOption (BASE_PACKAGE , "base package (invokerPackage) for generated code" ));
70
79
cliOptions .add (new CliOption (SKIP_SUPPORT_FILES , "skip support files such as pom.xml, mvnw, etc from code generation." ));
71
80
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" ));
81
+
82
+ CliOption useBeanValidation = CliOption .newBoolean (USE_BEANVALIDATION , "Use BeanValidation API annotations" );
83
+ useBeanValidation .setDefault ("true" );
84
+ cliOptions .add (useBeanValidation );
85
+
73
86
cliOptions .add (CliOption .newBoolean (IMPLICIT_HEADERS , "Use of @ApiImplicitParams for headers." ));
74
87
cliOptions .add (CliOption .newBoolean (USE_OPTIONAL ,
75
88
"Use Optional container for optional parameters" ));
76
89
77
- supportedLibraries .put (DEFAULT_LIBRARY , "Java Micronaut Server application." );
90
+ supportedLibraries .put (DEFAULT_LIBRARY , "Java Micronaut Server application with RxJava3 reactive streams implementation" );
91
+ supportedLibraries .put (USE_RXJAVA2 , "Java Micronaut Server application with RxJava2 reactive streams implementation" );
92
+ supportedLibraries .put (REACTOR_LIBRARY , "Java Micronaut Server application with Project Reactor reactive streams implementation" );
78
93
setLibrary (DEFAULT_LIBRARY );
79
94
80
95
CliOption library = new CliOption (CodegenConstants .LIBRARY , "library template (sub-template) to use" );
81
- library .setDefault (DEFAULT_LIBRARY );
82
96
library .setEnum (supportedLibraries );
83
97
library .setDefault (DEFAULT_LIBRARY );
84
98
cliOptions .add (library );
@@ -138,8 +152,8 @@ public void processOpts() {
138
152
this .setBasePackage ((String ) additionalProperties .get (BASE_PACKAGE ));
139
153
}
140
154
141
- if (additionalProperties .containsKey (USE_TAGS )) {
142
- this .setUseTags (Boolean .valueOf (additionalProperties .get (USE_TAGS ).toString ()));
155
+ if (additionalProperties .get (USE_TAGS ) != null ) {
156
+ this .setUseTags (Boolean .parseBoolean (additionalProperties .get (USE_TAGS ).toString ()));
143
157
}
144
158
145
159
if (additionalProperties .containsKey (USE_BEANVALIDATION )) {
@@ -151,21 +165,23 @@ public void processOpts() {
151
165
}
152
166
153
167
boolean skipSupportFiles = false ;
154
- if (additionalProperties .containsKey (SKIP_SUPPORT_FILES )) {
155
- skipSupportFiles = Boolean .valueOf (additionalProperties .get (SKIP_SUPPORT_FILES ).toString ());
168
+ if (additionalProperties .get (SKIP_SUPPORT_FILES ) != null ) {
169
+ skipSupportFiles = Boolean .parseBoolean (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
- if (additionalProperties .containsKey (IMPLICIT_HEADERS )) {
163
- this .setImplicitHeaders (Boolean .valueOf (additionalProperties .get (IMPLICIT_HEADERS ).toString ()));
174
+ if (additionalProperties .get (IMPLICIT_HEADERS ) != null ) {
175
+ this .setImplicitHeaders (Boolean .parseBoolean (additionalProperties .get (IMPLICIT_HEADERS ).toString ()));
164
176
}
165
177
166
- if (useOptional ) {
167
- writePropertyBack (USE_OPTIONAL , useOptional );
178
+ writePropertyBack (USE_OPTIONAL , useOptional );
179
+ if (isRxJava2Library ()) {
180
+ additionalProperties .put (USE_RXJAVA2 , true );
181
+ } else {
182
+ additionalProperties .put (USE_RXJAVA3 , isRxJava3Library ());
168
183
}
184
+ additionalProperties .put (USE_REACTOR , isReactorLibrary ());
169
185
170
186
if (!skipSupportFiles ) {
171
187
supportingFiles .add (new SupportingFile ("pom.mustache" , "" , "pom.xml" ));
@@ -174,7 +190,8 @@ public void processOpts() {
174
190
supportingFiles .add (new SupportingFile ("mvnw.cmd" , "" , "mvnw.cmd" ));
175
191
supportingFiles .add (new SupportingFile ("unsupportedOperationExceptionHandler.mustache" ,
176
192
(sourceFolder + File .separator + configPackage ).replace ("." , File .separator ), "UnsupportedOperationExceptionHandler.java" ));
177
- supportingFiles .add (new SupportingFile ("mainApplication.mustache" , (sourceFolder + File .separator ).replace ("." , File .separator ), "MainApplication.java" ));
193
+ supportingFiles .add (new SupportingFile ("mainApplication.mustache" , (sourceFolder + File .separator + basePackage ).replace ("." , File .separator ), "MainApplication.java" ));
194
+ apiTemplateFiles .put ("apiController.mustache" , "Controller.java" );
178
195
}
179
196
addHandlebarsLambdas (additionalProperties );
180
197
}
@@ -529,4 +546,22 @@ public void setUseBeanValidation(boolean useBeanValidation) {
529
546
public void setUseOptional (boolean useOptional ) {
530
547
this .useOptional = useOptional ;
531
548
}
549
+
550
+ @ Override
551
+ public void addHandlebarHelpers (Handlebars handlebars ) {
552
+ handlebars .setInfiniteLoops (true );
553
+ super .addHandlebarHelpers (handlebars );
554
+ }
555
+
556
+ private boolean isRxJava2Library () {
557
+ return library .equals (RXJAVA2_LIBRARY );
558
+ }
559
+
560
+ private boolean isRxJava3Library () {
561
+ return library .equals (RXJAVA3_LIBRARY );
562
+ }
563
+
564
+ private boolean isReactorLibrary () {
565
+ return library .equals (REACTOR_LIBRARY );
566
+ }
532
567
}
0 commit comments