[Kotlin-Spring] add skip-default-interface option (#14662)

This commit is contained in:
KlausH09
2023-03-13 15:51:49 +01:00
committed by GitHub
parent 23cf8368e8
commit 245851116f
5 changed files with 50 additions and 2 deletions

View File

@@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|serverPort|configuration the port in which the sever is to run on| |8080|
|serviceImplementation|generate stub service implementations that extends service interfaces. If this is set to true service interfaces will also be generated| |false|
|serviceInterface|generate service interfaces to go alongside controllers. In most cases this option would be used to update an existing project, so not to override implementations. Useful to help facilitate the generation gap pattern| |false|
|skipDefaultInterface|Whether to skip generation of default implementations for interfaces| |false|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
|sourceFolder|source folder for generated code| |src/main/kotlin|

View File

@@ -66,6 +66,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String GRADLE_BUILD_FILE = "gradleBuildFile";
public static final String SERVICE_INTERFACE = "serviceInterface";
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
public static final String REACTIVE = "reactive";
public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String DELEGATE_PATTERN = "delegatePattern";
@@ -79,6 +80,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private String serverPort = "8080";
private String title = "OpenAPI Kotlin Spring";
private boolean useBeanValidation = true;
private boolean skipDefaultInterface = false;
private boolean exceptionHandler = true;
private boolean gradleBuildFile = true;
private boolean useSwaggerUI = true;
@@ -153,6 +155,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
addSwitch(SERVICE_IMPLEMENTATION, "generate stub service implementations that extends service " +
"interfaces. If this is set to true service interfaces will also be generated", serviceImplementation);
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
addSwitch(SKIP_DEFAULT_INTERFACE, "Whether to skip generation of default implementations for interfaces", skipDefaultInterface);
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly);
addSwitch(DELEGATE_PATTERN, "Whether to generate the server files using the delegate pattern", delegatePattern);
@@ -334,6 +337,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
this.useBeanValidation = useBeanValidation;
}
public void setSkipDefaultInterface(boolean skipDefaultInterface) {
this.skipDefaultInterface = skipDefaultInterface;
}
public boolean isReactive() {
return reactive;
}
@@ -492,6 +499,11 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
}
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
if (additionalProperties.containsKey(SKIP_DEFAULT_INTERFACE)) {
this.setSkipDefaultInterface(convertPropertyToBoolean(SKIP_DEFAULT_INTERFACE));
}
writePropertyBack(SKIP_DEFAULT_INTERFACE, skipDefaultInterface);
if (additionalProperties.containsKey(REACTIVE) && library.equals(SPRING_BOOT)) {
this.setReactive(convertPropertyToBoolean(REACTIVE));
// spring webflux doesn't support @ControllerAdvice

View File

@@ -96,14 +96,14 @@ interface {{classname}} {
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
)
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
{{^isDelegate}}
return {{>returnValue}}
{{/isDelegate}}
{{#isDelegate}}
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}})
{{/isDelegate}}
}
}{{/skipDefaultInterface}}
{{/operation}}
}
{{/operations}}

View File

@@ -386,6 +386,28 @@ public class KotlinSpringServerCodegenTest {
);
}
@Test(description = "test skip default interface")
public void skipDefaultInterface() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(KotlinSpringServerCodegen.INTERFACE_ONLY, true);
codegen.additionalProperties().put(KotlinSpringServerCodegen.SKIP_DEFAULT_INTERFACE, true);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/skip-default-interface.yaml"))
.config(codegen))
.generate();
assertFileNotContains(
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PingApi.kt"),
"return "
);
}
@Test(description = "use Spring boot 3 & jakarta extension")
public void useSpringBoot3() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

View File

@@ -0,0 +1,13 @@
openapi: "3.0.1"
info:
title: test
version: "1.0"
paths:
/ping:
get:
operationId: ping
responses:
200:
description: OK