From 057c4294de472874574094d890b81c67afca6bd5 Mon Sep 17 00:00:00 2001 From: dumitru-petrusca Date: Mon, 8 Jun 2020 01:24:20 +0200 Subject: [PATCH] Issue 5497: Support the use of tags in the delegated Spring Kotlin generator. (#5499) Co-authored-by: Jim Schubert --- docs/generators/kotlin-spring.md | 1 + .../languages/KotlinSpringServerCodegen.java | 13 +++++++++- .../spring/KotlinSpringServerCodegenTest.java | 26 +++++++++++++++++++ .../3_0/kotlin/issue5497-use-tags-kotlin.yaml | 22 ++++++++++++++++ .../.openapi-generator/VERSION | 2 +- .../kotlin/org/openapitools/api/PetApi.kt | 2 +- .../kotlin/org/openapitools/api/StoreApi.kt | 2 +- .../kotlin/org/openapitools/api/UserApi.kt | 2 +- .../kotlin/org/openapitools/api/PetApi.kt | 2 +- .../kotlin/org/openapitools/api/StoreApi.kt | 2 +- .../kotlin/org/openapitools/api/UserApi.kt | 2 +- .../kotlin/org/openapitools/api/PetApi.kt | 2 +- .../kotlin/org/openapitools/api/StoreApi.kt | 2 +- .../kotlin/org/openapitools/api/UserApi.kt | 2 +- 14 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml diff --git a/docs/generators/kotlin-spring.md b/docs/generators/kotlin-spring.md index b088f30ba11..cbb8afe1d22 100644 --- a/docs/generators/kotlin-spring.md +++ b/docs/generators/kotlin-spring.md @@ -33,6 +33,7 @@ sidebar_label: kotlin-spring |swaggerAnnotations|generate swagger annotations to go alongside controllers and models| |false| |title|server title name or client service name| |OpenAPI Kotlin Spring| |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 diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java index 8658f878432..7d142d8a244 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java @@ -66,6 +66,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen public static final String REACTIVE = "reactive"; public static final String INTERFACE_ONLY = "interfaceOnly"; public static final String DELEGATE_PATTERN = "delegatePattern"; + public static final String USE_TAGS = "useTags"; private String basePackage; private String invokerPackage; @@ -81,6 +82,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen private boolean reactive = false; private boolean interfaceOnly = false; private boolean delegatePattern = false; + protected boolean useTags = false; public KotlinSpringServerCodegen() { super(); @@ -152,6 +154,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen 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); + addSwitch(USE_TAGS, "Whether to use tags for creating interface and controller class names", useTags); supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application."); setLibrary(SPRING_BOOT); @@ -245,6 +248,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen this.delegatePattern = delegatePattern; } + public void setUseTags(boolean useTags) { + this.useTags = useTags; + } + @Override public void setUseBeanValidation(boolean 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"); if (!this.interfaceOnly && this.delegatePattern) { @@ -440,7 +451,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen @Override public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map> operations) { - if (library.equals(SPRING_BOOT) && this.delegatePattern) { + if (library.equals(SPRING_BOOT) && !useTags) { String basePath = resourcePath; if (basePath.startsWith("/")) { basePath = basePath.substring(1); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java index e2b87d84d3f..d6b3e5f6e5f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java @@ -1,6 +1,7 @@ package org.openapitools.codegen.kotlin.spring; import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.collect.testing.Helpers; import io.swagger.parser.OpenAPIParser; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; @@ -21,6 +22,7 @@ import org.testng.annotations.Test; import java.io.File; import java.nio.file.Files; import java.util.Collections; +import java.util.List; public class KotlinSpringServerCodegenTest { @@ -183,4 +185,28 @@ public class KotlinSpringServerCodegenTest { 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 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") + ); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml new file mode 100644 index 00000000000..38d97f4e22e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml @@ -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 diff --git a/samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator/VERSION b/samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator/VERSION index b5d898602c2..9e9d3e448fb 100644 --- a/samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator/VERSION +++ b/samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator/VERSION @@ -1 +1 @@ -4.3.1-SNAPSHOT \ No newline at end of file +4.3.1-SNAPSHOT diff --git a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApi.kt b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApi.kt index b500f915568..612713d99ff 100644 --- a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApi.kt +++ b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApi.kt @@ -39,7 +39,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Pet", description = "The Pet API") +@Api(value = "pet", description = "The pet API") @RequestMapping("\${api.base-path:/v2}") class PetApiController(@Autowired(required = true) val service: PetApiService) { diff --git a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApi.kt b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApi.kt index 71017e33a50..99dfad74f4f 100644 --- a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApi.kt +++ b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApi.kt @@ -38,7 +38,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Store", description = "The Store API") +@Api(value = "store", description = "The store API") @RequestMapping("\${api.base-path:/v2}") class StoreApiController(@Autowired(required = true) val service: StoreApiService) { diff --git a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApi.kt index 1dbc304d88e..d10327cd287 100644 --- a/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -38,7 +38,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "User", description = "The User API") +@Api(value = "user", description = "The user API") @RequestMapping("\${api.base-path:/v2}") class UserApiController(@Autowired(required = true) val service: UserApiService) { diff --git a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/PetApi.kt b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/PetApi.kt index e80de43feee..1dca81e1b9d 100644 --- a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/PetApi.kt +++ b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/PetApi.kt @@ -40,7 +40,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Pet", description = "The Pet API") +@Api(value = "pet", description = "The pet API") @RequestMapping("\${api.base-path:/v2}") class PetApiController(@Autowired(required = true) val service: PetApiService) { diff --git a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/StoreApi.kt b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/StoreApi.kt index e61c947c0b7..35d8d3cbe03 100644 --- a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/StoreApi.kt +++ b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/StoreApi.kt @@ -39,7 +39,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Store", description = "The Store API") +@Api(value = "store", description = "The store API") @RequestMapping("\${api.base-path:/v2}") class StoreApiController(@Autowired(required = true) val service: StoreApiService) { diff --git a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApi.kt index 4f04e676dac..7fdaa627b7c 100644 --- a/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -39,7 +39,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "User", description = "The User API") +@Api(value = "user", description = "The user API") @RequestMapping("\${api.base-path:/v2}") class UserApiController(@Autowired(required = true) val service: UserApiService) { diff --git a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/PetApi.kt b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/PetApi.kt index b500f915568..612713d99ff 100644 --- a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/PetApi.kt +++ b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/PetApi.kt @@ -39,7 +39,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Pet", description = "The Pet API") +@Api(value = "pet", description = "The pet API") @RequestMapping("\${api.base-path:/v2}") class PetApiController(@Autowired(required = true) val service: PetApiService) { diff --git a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/StoreApi.kt b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/StoreApi.kt index 71017e33a50..99dfad74f4f 100644 --- a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/StoreApi.kt +++ b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/StoreApi.kt @@ -38,7 +38,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "Store", description = "The Store API") +@Api(value = "store", description = "The store API") @RequestMapping("\${api.base-path:/v2}") class StoreApiController(@Autowired(required = true) val service: StoreApiService) { diff --git a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/UserApi.kt index 1dbc304d88e..d10327cd287 100644 --- a/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -38,7 +38,7 @@ import kotlin.collections.Map @RestController @Validated -@Api(value = "User", description = "The User API") +@Api(value = "user", description = "The user API") @RequestMapping("\${api.base-path:/v2}") class UserApiController(@Autowired(required = true) val service: UserApiService) {