Issue 5497: Support the use of tags in the delegated Spring Kotlin generator. (#5499)

Co-authored-by: Jim Schubert <james.schubert@gmail.com>
This commit is contained in:
dumitru-petrusca 2020-06-08 01:24:20 +02:00 committed by GitHub
parent 7baa72eefa
commit 057c4294de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 71 additions and 11 deletions

View File

@ -33,6 +33,7 @@ sidebar_label: kotlin-spring
|swaggerAnnotations|generate swagger annotations to go alongside controllers and models| |false| |swaggerAnnotations|generate swagger annotations to go alongside controllers and models| |false|
|title|server title name or client service name| |OpenAPI Kotlin Spring| |title|server title name or client service name| |OpenAPI Kotlin Spring|
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true| |useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
|useTags|Whether to use tags for creating interface and controller class names| |false|
## IMPORT MAPPING ## IMPORT MAPPING

View File

@ -66,6 +66,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String REACTIVE = "reactive"; public static final String REACTIVE = "reactive";
public static final String INTERFACE_ONLY = "interfaceOnly"; public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String DELEGATE_PATTERN = "delegatePattern"; public static final String DELEGATE_PATTERN = "delegatePattern";
public static final String USE_TAGS = "useTags";
private String basePackage; private String basePackage;
private String invokerPackage; private String invokerPackage;
@ -81,6 +82,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private boolean reactive = false; private boolean reactive = false;
private boolean interfaceOnly = false; private boolean interfaceOnly = false;
private boolean delegatePattern = false; private boolean delegatePattern = false;
protected boolean useTags = false;
public KotlinSpringServerCodegen() { public KotlinSpringServerCodegen() {
super(); super();
@ -152,6 +154,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive); addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly); 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(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);
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application."); supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
setLibrary(SPRING_BOOT); setLibrary(SPRING_BOOT);
@ -245,6 +248,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
this.delegatePattern = delegatePattern; this.delegatePattern = delegatePattern;
} }
public void setUseTags(boolean useTags) {
this.useTags = useTags;
}
@Override @Override
public void setUseBeanValidation(boolean useBeanValidation) { public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation; this.useBeanValidation = useBeanValidation;
@ -369,6 +376,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
} }
} }
if (additionalProperties.containsKey(USE_TAGS)) {
this.setUseTags(Boolean.parseBoolean(additionalProperties.get(USE_TAGS).toString()));
}
modelTemplateFiles.put("model.mustache", ".kt"); modelTemplateFiles.put("model.mustache", ".kt");
if (!this.interfaceOnly && this.delegatePattern) { if (!this.interfaceOnly && this.delegatePattern) {
@ -440,7 +451,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
@Override @Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) { public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (library.equals(SPRING_BOOT) && this.delegatePattern) { if (library.equals(SPRING_BOOT) && !useTags) {
String basePath = resourcePath; String basePath = resourcePath;
if (basePath.startsWith("/")) { if (basePath.startsWith("/")) {
basePath = basePath.substring(1); basePath = basePath.substring(1);

View File

@ -1,6 +1,7 @@
package org.openapitools.codegen.kotlin.spring; package org.openapitools.codegen.kotlin.spring;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.testing.Helpers;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.Info;
@ -21,6 +22,7 @@ import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Collections; import java.util.Collections;
import java.util.List;
public class KotlinSpringServerCodegenTest { public class KotlinSpringServerCodegenTest {
@ -183,4 +185,28 @@ public class KotlinSpringServerCodegenTest {
Assert.assertTrue(codegen.supportingFiles().stream().anyMatch(supportingFile -> supportingFile.templateFile.equals("apiUtil.mustache"))); Assert.assertTrue(codegen.supportingFiles().stream().anyMatch(supportingFile -> supportingFile.templateFile.equals("apiUtil.mustache")));
} }
@Test(description = "test delegate with tags")
public void delegateWithTags() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); //may be move to /build
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
List<File> files = new DefaultGenerator()
.opts(
new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml"))
.config(codegen)
)
.generate();
Helpers.assertContainsAllOf(files,
new File(output, "src/main/kotlin/org/openapitools/api/TestV1ApiController.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV1ApiDelegate.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiController.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt")
);
}
} }

View File

@ -0,0 +1,22 @@
openapi: "3.0.1"
info:
title: test
version: "1.0"
paths:
/api/v1/test:
get:
tags: [Test v1]
operationId: test1
responses:
200:
description: OK
/api/v2/test:
get:
tags: [Test v2]
operationId: test2
responses:
200:
description: OK

View File

@ -39,7 +39,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Pet", description = "The Pet API") @Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) { class PetApiController(@Autowired(required = true) val service: PetApiService) {

View File

@ -38,7 +38,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Store", description = "The Store API") @Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) { class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

View File

@ -38,7 +38,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "User", description = "The User API") @Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) { class UserApiController(@Autowired(required = true) val service: UserApiService) {

View File

@ -40,7 +40,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Pet", description = "The Pet API") @Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) { class PetApiController(@Autowired(required = true) val service: PetApiService) {

View File

@ -39,7 +39,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Store", description = "The Store API") @Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) { class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

View File

@ -39,7 +39,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "User", description = "The User API") @Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) { class UserApiController(@Autowired(required = true) val service: UserApiService) {

View File

@ -39,7 +39,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Pet", description = "The Pet API") @Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) { class PetApiController(@Autowired(required = true) val service: PetApiService) {

View File

@ -38,7 +38,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "Store", description = "The Store API") @Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) { class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

View File

@ -38,7 +38,7 @@ import kotlin.collections.Map
@RestController @RestController
@Validated @Validated
@Api(value = "User", description = "The User API") @Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}") @RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) { class UserApiController(@Autowired(required = true) val service: UserApiService) {