From b2280e23f7d537d6d0f1f09b23124249aed2f661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9?= Date: Thu, 29 Jun 2023 10:25:18 +0200 Subject: [PATCH] =?UTF-8?q?[Kotlin][Spring]ctbarnev=5Fbugfix-13488-remove-?= =?UTF-8?q?request-mapping-from-for-Kotlin-spring-c=E2=80=A6=20(#15898)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ctbarnev_bugfix-13488-remove-request-mapping-from-for-Kotlin-spring-cloud * ctbarnev_bugfix-13488-remove-request-mapping-from-for-Kotlin-spring-cloud * Undo formatting code * Undo formatting code * Undo formatting code * ctbarnev_bugfix-13488-remove-request-mapping-from-for-Kotlin-spring-cloud * Undo formatting code * Undo formatting code * Cleanup --------- Co-authored-by: c.t.vanbarneveld --- .../languages/KotlinSpringServerCodegen.java | 8 ++++++++ .../kotlin-spring/apiInterface.mustache | 2 ++ .../spring/KotlinSpringServerCodegenTest.java | 20 +++++++++++++++++++ ...488_use_kotlinSpring_with_springCloud.yaml | 19 ++++++++++++++++++ .../kotlin/org/openapitools/api/PetApi.kt | 1 - .../kotlin/org/openapitools/api/StoreApi.kt | 1 - .../kotlin/org/openapitools/api/UserApi.kt | 1 - 7 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/kotlin/feat13488_use_kotlinSpring_with_springCloud.yaml 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 b4f4618e20b..5922abafa37 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 @@ -95,6 +95,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen public static final String REACTIVE = "reactive"; public static final String INTERFACE_ONLY = "interfaceOnly"; public static final String USE_FEIGN_CLIENT_URL = "useFeignClientUrl"; + public static final String USE_FEIGN_CLIENT = "useFeignClient"; public static final String DELEGATE_PATTERN = "delegatePattern"; public static final String USE_TAGS = "useTags"; public static final String BEAN_QUALIFIERS = "beanQualifiers"; @@ -116,6 +117,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen private boolean reactive = false; private boolean interfaceOnly = false; protected boolean useFeignClientUrl = true; + protected boolean useFeignClient = false; private boolean delegatePattern = false; protected boolean useTags = false; private boolean beanQualifiers = false; @@ -388,6 +390,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen this.useBeanValidation = useBeanValidation; } + public void setUseFeignClient( boolean useFeignClient ) { + this.useFeignClient = useFeignClient; + } + public void setSkipDefaultInterface(boolean skipDefaultInterface) { this.skipDefaultInterface = skipDefaultInterface; } @@ -585,6 +591,8 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen if (library.equals(SPRING_CLOUD_LIBRARY)) { this.setInterfaceOnly(true); + this.setUseFeignClient(true); + additionalProperties.put(USE_FEIGN_CLIENT, true); } if (additionalProperties.containsKey(USE_FEIGN_CLIENT_URL)) { diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache index 3617cb42371..bc4a32f53d7 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache @@ -58,9 +58,11 @@ import kotlin.collections.Map {{#swagger1AnnotationLibrary}} @Api(value = "{{{baseName}}}", description = "The {{{baseName}}} API") {{/swagger1AnnotationLibrary}} +{{^useFeignClient}} {{=<% %>=}} @RequestMapping("\${api.base-path:<%contextPath%>}") <%={{ }}=%> +{{/useFeignClient}} {{#operations}} interface {{classname}} { {{#isDelegate}} 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 09065942a8b..9f8a027907a 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 @@ -77,6 +77,26 @@ public class KotlinSpringServerCodegenTest { Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVER_PORT), "8080"); } + @Test + public void testNoRequestMappingAnnotation() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen(); + codegen.setOutputDir(output.getAbsolutePath()); + codegen.setLibrary("spring-cloud"); + + new DefaultGenerator().opts(new ClientOptInput() + .openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/feat13488_use_kotlinSpring_with_springCloud.yaml")) + .config(codegen)) + .generate(); + + // Check that the @RequestMapping annotation is not generated in the Api file + assertFileNotContains( + Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1Api.kt"), + "@RequestMapping(\"\\${api.base-path" + ); + } @Test public void testSettersForConfigValues() throws Exception { diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/feat13488_use_kotlinSpring_with_springCloud.yaml b/modules/openapi-generator/src/test/resources/3_0/kotlin/feat13488_use_kotlinSpring_with_springCloud.yaml new file mode 100644 index 00000000000..a6b3713d64a --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/feat13488_use_kotlinSpring_with_springCloud.yaml @@ -0,0 +1,19 @@ +openapi: "3.0.1" +info: + title: test + version: "1.0" + +paths: + + /api/v1/test/{id}: + get: + tags: [Test v1] + operationId: test1 + parameters: + - name: id + in: path + schema: + type: string + responses: + 200: + description: OK \ No newline at end of file diff --git a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt index 1bbcd768dc5..e8f9985989d 100644 --- a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt +++ b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt @@ -30,7 +30,6 @@ import kotlin.collections.List import kotlin.collections.Map @Validated -@RequestMapping("\${api.base-path:/v2}") interface PetApi { diff --git a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/StoreApi.kt b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/StoreApi.kt index 29726d68c4d..c29f7f071c2 100644 --- a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/StoreApi.kt +++ b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/StoreApi.kt @@ -29,7 +29,6 @@ import kotlin.collections.List import kotlin.collections.Map @Validated -@RequestMapping("\${api.base-path:/v2}") interface StoreApi { diff --git a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/UserApi.kt index 8c5ef43e9a3..c17ab667fd9 100644 --- a/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/client/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -29,7 +29,6 @@ import kotlin.collections.List import kotlin.collections.Map @Validated -@RequestMapping("\${api.base-path:/v2}") interface UserApi {