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
This commit is contained in:
de1mos 2021-04-01 08:47:11 +07:00 committed by GitHub
parent 26ca6ab27e
commit b335ba834f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.context.request.NativeWebRequest import org.springframework.web.context.request.NativeWebRequest
import org.springframework.core.io.Resource import org.springframework.core.io.Resource
{{#reactive}} {{#reactive}}
import kotlinx.coroutines.flow.Flow
import org.springframework.web.server.ServerWebExchange import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@ -32,7 +33,7 @@ interface {{classname}}Delegate {
/** /**
* @see {{classname}}#{{operationId}} * @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}} { {{/-last}}{{/allParams}}): {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {
{{>methodBody}} {{>methodBody}}
} }

View File

@ -83,7 +83,7 @@ interface {{classname}} {
return {{>returnValue}} return {{>returnValue}}
{{/isDelegate}} {{/isDelegate}}
{{#isDelegate}} {{#isDelegate}}
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}); return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
{{/isDelegate}} {{/isDelegate}}
} }
{{/operation}} {{/operation}}

View File

@ -1,3 +1,4 @@
{{^reactive}}
{{#examples}} {{#examples}}
{{#-first}} {{#-first}}
{{#async}} {{#async}}
@ -21,3 +22,7 @@ return CompletableFuture.supplyAsync(()-> {
{{^examples}} {{^examples}}
return {{#async}}CompletableFuture.completedFuture({{/async}}ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}) return {{#async}}CompletableFuture.completedFuture({{/async}}ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}})
{{/examples}} {{/examples}}
{{/reactive}}
{{#reactive}}
return ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}})
{{/reactive}}

View File

@ -1,9 +1,14 @@
package org.openapitools.codegen.kotlin.spring; package org.openapitools.codegen.kotlin.spring;
import com.google.common.collect.testing.Helpers; 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.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.kotlin.KotlinTestUtils;
import org.openapitools.codegen.languages.KotlinSpringServerCodegen; import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
import org.testng.Assert; import org.testng.Assert;
@ -11,12 +16,12 @@ 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.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import io.swagger.v3.oas.models.OpenAPI; import static org.openapitools.codegen.TestUtils.assertFileContains;
import io.swagger.v3.oas.models.info.Info; import static org.openapitools.codegen.TestUtils.assertFileNotContains;
import io.swagger.v3.oas.models.servers.Server;
public class KotlinSpringServerCodegenTest { public class KotlinSpringServerCodegenTest {
@ -203,4 +208,49 @@ public class KotlinSpringServerCodegenTest {
new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt") 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<File> 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<Flow<kotlin.String>>");
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");
}
} }

View File

@ -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