diff --git a/docs/generators/kotlin-spring.md b/docs/generators/kotlin-spring.md index b088f30ba114..cbb8afe1d220 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 8658f878432d..7d142d8a244a 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 KotlinSpringServerCodegen() { 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 void setDelegatePattern(boolean delegatePattern) { 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 void processOpts() { } } + 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 @@ protected Builder addMustacheLambdas() { @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 e2b87d84d3f1..d6b3e5f6e5fe 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 java.io.File; import java.nio.file.Files; import java.util.Collections; +import java.util.List; public class KotlinSpringServerCodegenTest { @@ -183,4 +185,28 @@ public void testDelegatePattern() { 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 000000000000..38d97f4e22e7 --- /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 b5d898602c2c..9e9d3e448fb4 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 b500f9155686..612713d99ff6 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 71017e33a506..99dfad74f4fa 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 1dbc304d88e4..d10327cd2877 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 e80de43feeef..1dca81e1b9de 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 e61c947c0b75..35d8d3cbe03a 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 4f04e676dacc..7fdaa627b7c1 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 b500f9155686..612713d99ff6 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 71017e33a506..99dfad74f4fa 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 1dbc304d88e4..d10327cd2877 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) {