From b335ba834f07122bdee48f2999d3c769186b31bd Mon Sep 17 00:00:00 2001 From: de1mos Date: Thu, 1 Apr 2021 08:47:11 +0700 Subject: [PATCH] fix #7325 kotlin-spring fix reactive delegate pattern combination (#8867) * #7325 kotlin-spring remove exchange field from reactive delegate pattern * #7325 remove examples from kotlin-spring reactive delegate and add missing import * #7325 add test assertions * fix swagger spec in test --- .../kotlin-spring/apiDelegate.mustache | 3 +- .../kotlin-spring/apiInterface.mustache | 2 +- .../kotlin-spring/methodBody.mustache | 5 ++ .../spring/KotlinSpringServerCodegenTest.java | 60 +++++++++++++++++-- ...325-use-delegate-reactive-tags-kotlin.yaml | 35 +++++++++++ 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/kotlin/issue7325-use-delegate-reactive-tags-kotlin.yaml diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache index d45f9f79f2e..bbf57f85b7b 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache @@ -8,6 +8,7 @@ import org.springframework.http.ResponseEntity import org.springframework.web.context.request.NativeWebRequest import org.springframework.core.io.Resource {{#reactive}} +import kotlinx.coroutines.flow.Flow import org.springframework.web.server.ServerWebExchange import reactor.core.publisher.Flux import reactor.core.publisher.Mono @@ -32,7 +33,7 @@ interface {{classname}}Delegate { /** * @see {{classname}}#{{operationId}} */ - fun {{operationId}}({{#allParams}}{{paramName}}: {{^isFile}}{{>optionalDataType}}{{/isFile}}{{#isFile}}Resource?{{/isFile}}{{^-last}}, + {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{paramName}}: {{^isFile}}{{>optionalDataType}}{{/isFile}}{{#isFile}}Resource?{{/isFile}}{{^-last}}, {{/-last}}{{/allParams}}): {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} { {{>methodBody}} } 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 bc224b50080..5e9b3888c8d 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache @@ -83,7 +83,7 @@ interface {{classname}} { return {{>returnValue}} {{/isDelegate}} {{#isDelegate}} - return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}); + return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}); {{/isDelegate}} } {{/operation}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache index 69a9fd42d93..3e3b63e9ab0 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache @@ -1,3 +1,4 @@ +{{^reactive}} {{#examples}} {{#-first}} {{#async}} @@ -21,3 +22,7 @@ return CompletableFuture.supplyAsync(()-> { {{^examples}} return {{#async}}CompletableFuture.completedFuture({{/async}}ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}) {{/examples}} +{{/reactive}} +{{#reactive}} +return ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}) +{{/reactive}} \ No newline at end of file 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 b77afc858f9..dc3bceaa435 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,9 +1,14 @@ package org.openapitools.codegen.kotlin.spring; import com.google.common.collect.testing.Helpers; - +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.servers.Server; import org.apache.commons.io.FileUtils; -import org.openapitools.codegen.*; +import org.openapitools.codegen.ClientOptInput; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.DefaultGenerator; +import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.kotlin.KotlinTestUtils; import org.openapitools.codegen.languages.KotlinSpringServerCodegen; import org.testng.Assert; @@ -11,12 +16,12 @@ import org.testng.annotations.Test; import java.io.File; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Collections; import java.util.List; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.info.Info; -import io.swagger.v3.oas.models.servers.Server; +import static org.openapitools.codegen.TestUtils.assertFileContains; +import static org.openapitools.codegen.TestUtils.assertFileNotContains; public class KotlinSpringServerCodegenTest { @@ -203,4 +208,49 @@ public class KotlinSpringServerCodegenTest { new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt") ); } + + @Test(description = "test delegate reactive with tags") + public void delegateReactiveWithTags() 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.REACTIVE, true); + codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true); + + List files = new DefaultGenerator() + .opts( + new ClientOptInput() + .openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue7325-use-delegate-reactive-tags-kotlin.yaml")) + .config(codegen) + ) + .generate(); + + Helpers.assertContainsAllOf(files, + new File(output, "src/main/kotlin/org/openapitools/api/TestV1Api.kt"), + 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/TestV2Api.kt"), + new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiController.kt"), + new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt") + ); + + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1Api.kt"), + "suspend fun"); + assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1Api.kt"), + "exchange"); + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1ApiDelegate.kt"), + "suspend fun"); + assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1ApiDelegate.kt"), + "ApiUtil"); + + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV2Api.kt"), + "import kotlinx.coroutines.flow.Flow", "ResponseEntity>"); + assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV2Api.kt"), + "exchange"); + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt"), + "import kotlinx.coroutines.flow.Flow"); + assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt"), + "suspend fun", "ApiUtil"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/issue7325-use-delegate-reactive-tags-kotlin.yaml b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue7325-use-delegate-reactive-tags-kotlin.yaml new file mode 100644 index 00000000000..3a14311e1ca --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue7325-use-delegate-reactive-tags-kotlin.yaml @@ -0,0 +1,35 @@ +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 + + /api/v2/test: + get: + tags: [Test v2] + operationId: test2 + responses: + 200: + description: OK + content: + application/json: + schema: + type: array + items: + type: string + +