Skip to content

Commit 9fea3ca

Browse files
committed
fix implementation and add tests
1 parent 3d75b21 commit 9fea3ca

2 files changed

Lines changed: 327 additions & 13 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
9494
public static final String USE_REQUEST_MAPPING_ON_CONTROLLER = "useRequestMappingOnController";
9595
public static final String USE_REQUEST_MAPPING_ON_INTERFACE = "useRequestMappingOnInterface";
9696

97+
@Getter
98+
public enum DeclarativeInterfaceReactiveMode {
99+
coroutines("Use kotlin-idiomatic 'suspend' functions", "reactiveModeCoroutines"),
100+
reactor("Use reactor return wrappers 'Mono' and 'Flux'", "reactiveModeReactor");
101+
102+
private final String description;
103+
private final String additionalPropertyName;
104+
105+
DeclarativeInterfaceReactiveMode(String description, String additionalPropertyName) {
106+
this.description = description;
107+
this.additionalPropertyName = additionalPropertyName;
108+
}
109+
}
110+
97111
public enum RequestMappingMode {
98112
api_interface("Generate the @RequestMapping annotation on the generated Api Interface."),
99113
controller("Generate the @RequestMapping annotation on the generated Api Controller Implementation."),
@@ -143,7 +157,7 @@ public String getDescription() {
143157
@Setter
144158
private boolean reactive = false;
145159
@Setter
146-
private String reactiveMode = "coroutines";
160+
private DeclarativeInterfaceReactiveMode declarativeInterfaceReactiveMode = DeclarativeInterfaceReactiveMode.coroutines;
147161
@Getter
148162
@Setter
149163
private boolean useFlowForArrayReturnType = true;
@@ -250,10 +264,6 @@ public KotlinSpringServerCodegen() {
250264
addSwitch(DECLARATIVE_INTERFACE_WRAP_RESPONSES,
251265
"Whether (when false) to return actual type (e.g. List<Fruit>) and handle non 2xx responses via exceptions or (when true) return entire ResponseEntity (e.g. ResponseEntity<List<Fruit>>)",
252266
declarativeInterfaceWrapResponses);
253-
addSwitch(DECLARATIVE_INTERFACE_WRAP_RESPONSES,
254-
"Whether (when false) to return actual type (e.g. List<Fruit>) and handle non 2xx responses via exceptions or (when true) return entire ResponseEntity (e.g. ResponseEntity<List<Fruit>>)",
255-
declarativeInterfaceWrapResponses);
256-
257267
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
258268
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
259269
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");
@@ -274,6 +284,14 @@ public KotlinSpringServerCodegen() {
274284
}
275285
cliOptions.add(requestMappingOpt);
276286

287+
CliOption declarativeInterfaceReactiveModeOpt = new CliOption(DECLARATIVE_INTERFACE_REACTIVE_MODE,
288+
"What type of reactive style to use in Spring Http declarative interface")
289+
.defaultValue(declarativeInterfaceReactiveMode.name());
290+
for (DeclarativeInterfaceReactiveMode mode : DeclarativeInterfaceReactiveMode.values()) {
291+
declarativeInterfaceReactiveModeOpt.addEnum(mode.name(), mode.getDescription());
292+
}
293+
cliOptions.add(declarativeInterfaceReactiveModeOpt);
294+
277295
if (null != defaultDocumentationProvider()) {
278296
CliOption documentationProviderCliOption = new CliOption(DOCUMENTATION_PROVIDER,
279297
"Select the OpenAPI documentation provider.")
@@ -565,14 +583,17 @@ public void processOpts() {
565583
}
566584
}
567585
if (additionalProperties.containsKey(DECLARATIVE_INTERFACE_REACTIVE_MODE)) {
568-
this.reactiveMode = String.valueOf(additionalProperties.get(DECLARATIVE_INTERFACE_REACTIVE_MODE));
569-
}
570-
if ("coroutines".equalsIgnoreCase(reactiveMode)) {
571-
writePropertyBack("reactiveModeCoroutines", true);
572-
} else if ("reactor".equalsIgnoreCase(reactiveMode)) {
573-
writePropertyBack("reactiveModeReactor", true);
574-
} else {
575-
throw new IllegalArgumentException("Invalid value for additional property '" + DECLARATIVE_INTERFACE_REACTIVE_MODE + "'. Supported values are 'coroutines' and 'reactor'.");
586+
try {
587+
DeclarativeInterfaceReactiveMode optValue = DeclarativeInterfaceReactiveMode.valueOf(
588+
String.valueOf(additionalProperties.get(DECLARATIVE_INTERFACE_REACTIVE_MODE)));
589+
setDeclarativeInterfaceReactiveMode(optValue);
590+
writePropertyBack(optValue.getAdditionalPropertyName(), true);
591+
additionalProperties.remove(DECLARATIVE_INTERFACE_REACTIVE_MODE);
592+
} catch (IllegalArgumentException e) {
593+
throw new IllegalArgumentException(
594+
"Invalid value for additional property '" + DECLARATIVE_INTERFACE_REACTIVE_MODE + "'. Supported values are " + Arrays.toString(DeclarativeInterfaceReactiveMode.values()) + "."
595+
);
596+
}
576597
}
577598
}
578599
}

0 commit comments

Comments
 (0)