Skip to content

Commit

Permalink
Add beanQualifiers option to kotlin-server (#11745)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsergey authored Mar 1, 2022
1 parent f03b1e7 commit 1b6ab63
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|artifactId|Generated artifact id (name of jar).| |openapi-spring|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|basePackage|base package (invokerPackage) for generated code| |org.openapitools|
|beanQualifiers|Whether to add fully-qualifier class names as bean qualifiers in @Component and @RestController annotations. May be used to prevent bean names clash if multiple generated libraries (contexts) added to single project.| |false|
|delegatePattern|Whether to generate the server files using the delegate pattern| |false|
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |camelCase|
|exceptionHandler|generate default global exception handlers (not compatible with reactive. enabling reactive will disable exceptionHandler )| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String DELEGATE_PATTERN = "delegatePattern";
public static final String USE_TAGS = "useTags";
public static final String BEAN_QUALIFIERS = "beanQualifiers";

private String basePackage;
private String invokerPackage;
Expand All @@ -82,6 +83,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private boolean interfaceOnly = false;
private boolean delegatePattern = false;
protected boolean useTags = false;
private boolean beanQualifiers = false;

public KotlinSpringServerCodegen() {
super();
Expand Down Expand Up @@ -146,6 +148,9 @@ public KotlinSpringServerCodegen() {
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);
addSwitch(USE_TAGS, "Whether to use tags for creating interface and controller class names", useTags);
addSwitch(BEAN_QUALIFIERS, "Whether to add fully-qualifier class names as bean qualifiers in @Component and " +
"@RestController annotations. May be used to prevent bean names clash if multiple generated libraries" +
" (contexts) added to single project.", beanQualifiers);
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
setLibrary(SPRING_BOOT);

Expand Down Expand Up @@ -256,6 +261,14 @@ public void setReactive(boolean reactive) {
this.reactive = reactive;
}

public boolean isBeanQualifiers() {
return beanQualifiers;
}

public void setBeanQualifiers(boolean beanQualifiers) {
this.beanQualifiers = beanQualifiers;
}

@Override
public CodegenType getTag() {
return CodegenType.SERVER;
Expand Down Expand Up @@ -363,6 +376,11 @@ public void processOpts() {
writePropertyBack(REACTIVE, reactive);
writePropertyBack(EXCEPTION_HANDLER, exceptionHandler);

if (additionalProperties.containsKey(BEAN_QUALIFIERS) && library.equals(SPRING_BOOT)) {
this.setBeanQualifiers(convertPropertyToBoolean(BEAN_QUALIFIERS));
}
writePropertyBack(BEAN_QUALIFIERS, beanQualifiers);

if (additionalProperties.containsKey(INTERFACE_ONLY)) {
this.setInterfaceOnly(Boolean.parseBoolean(additionalProperties.get(INTERFACE_ONLY).toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import kotlinx.coroutines.flow.Flow;
import kotlin.collections.List
import kotlin.collections.Map

@RestController
@RestController{{#beanQualifiers}}("{{package}}.{{classname}}Controller"){{/beanQualifiers}}
{{#useBeanValidation}}
@Validated
{{/useBeanValidation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Optional;
{{>generatedAnnotation}}

@Controller
@Controller{{#beanQualifiers}}("{{package}}.{{classname}}Controller"){{/beanQualifiers}}
{{=<% %>=}}
@RequestMapping("\${openapi.<%title%>.base-path:<%>defaultBasePath%>}")
<%={{ }}=%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,25 @@ public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)"
);
}

@Test(description = "test bean qualifiers")
public void beanQualifiers() 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.BEAN_QUALIFIERS, true);

new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/bean-qualifiers.yaml"))
.config(codegen))
.generate();

assertFileContains(
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PingApiController.kt"),
"@RestController(\"org.openapitools.api.PingApiController\")"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
openapi: "3.0.1"
info:
title: test
version: "1.0"

paths:

/ping:
get:
operationId: ping
responses:
200:
description: OK

0 comments on commit 1b6ab63

Please sign in to comment.