Skip to content

Commit ffd69fe

Browse files
[kotlin-spring] add form params specific unit test cribbed from SpringServerCodegenTest.java
1 parent 2c296fe commit ffd69fe

1 file changed

Lines changed: 90 additions & 7 deletions

File tree

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import io.swagger.v3.oas.models.info.Info;
66
import io.swagger.v3.oas.models.servers.Server;
77
import io.swagger.v3.parser.core.models.ParseOptions;
8+
import java.util.Arrays;
89
import java.util.HashMap;
910
import java.util.function.Consumer;
11+
import java.util.stream.Stream;
1012
import org.apache.commons.io.FileUtils;
1113
import org.assertj.core.api.Assertions;
1214
import org.jetbrains.annotations.NotNull;
@@ -789,7 +791,7 @@ public void contractWithResolvedInnerEnumContainsEnumConverter() throws IOExcept
789791
}
790792

791793
@Test
792-
public void givenMultipartFormArray_whenGenerateDelegateAndService_thenParameterIsCreatedAsListOfMultipartFile() throws IOException {
794+
public void givenNonRequiredMultipartFileArray_whenGenerateDelegateAndService_thenParameterIsCreatedAsNullableListOfMultipartFile() throws IOException {
793795
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
794796
output.deleteOnExit();
795797
String outputPath = output.getAbsolutePath().replace('\\', '/');
@@ -799,7 +801,8 @@ public void givenMultipartFormArray_whenGenerateDelegateAndService_thenParameter
799801
codegen.setOpenAPI(openAPI);
800802
codegen.setOutputDir(output.getAbsolutePath());
801803
codegen.setDelegatePattern(true);
802-
codegen.setServiceInterface(true);
804+
// this will generate the service interface & implementation files
805+
codegen.setServiceImplementation(true);
803806

804807
ClientOptInput input = new ClientOptInput();
805808
input.openAPI(openAPI);
@@ -817,14 +820,94 @@ public void givenMultipartFormArray_whenGenerateDelegateAndService_thenParameter
817820

818821
Path delegateFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiDelegate.kt");
819822
assertFileContains(delegateFile, "additionalMetadata: kotlin.String?");
820-
assertFileContains(delegateFile, "images: Array<org.springframework.web.multipart.MultipartFile>");
823+
assertFileContains(delegateFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
821824

822825
Path controllerFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApi.kt");
823-
assertFileContains(controllerFile, "images: Array<org.springframework.web.multipart.MultipartFile>");
824-
826+
assertFileContains(controllerFile, "additionalMetadata: kotlin.String?");
827+
assertFileContains(controllerFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
825828

826829
Path serviceFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiService.kt");
827-
assertFileContains(serviceFile, "images: Array<org.springframework.web.multipart.MultipartFile>");
830+
assertFileContains(serviceFile, "additionalMetadata: kotlin.String?");
831+
assertFileContains(serviceFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
832+
833+
Path serviceImplFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiServiceImpl.kt");
834+
assertFileContains(serviceImplFile, "additionalMetadata: kotlin.String?");
835+
assertFileContains(serviceImplFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
836+
}
837+
838+
@Test
839+
public void givenMultipartBinaryArray_whenGenerateDelegateAndService_correctMultipartFileIsCreated() throws IOException {
840+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
841+
output.deleteOnExit();
842+
String outputPath = output.getAbsolutePath().replace('\\', '/');
843+
844+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/form-multipart-binary-array.yaml");
845+
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
846+
codegen.setOpenAPI(openAPI);
847+
codegen.setOutputDir(output.getAbsolutePath());
848+
codegen.setDelegatePattern(true);
849+
// this will generate the service interface & implementation files
850+
codegen.setServiceImplementation(true);
851+
852+
ClientOptInput input = new ClientOptInput();
853+
input.openAPI(openAPI);
854+
input.config(codegen);
855+
856+
DefaultGenerator generator = new DefaultGenerator();
857+
858+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
859+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
860+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
861+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
862+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
863+
864+
generator.opts(input).generate();
865+
866+
validateMultipartFiles(
867+
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartArray",
868+
"files: Array<org.springframework.web.multipart.MultipartFile>?)"
869+
);
870+
871+
validateMultipartFiles(
872+
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartMixed",
873+
"file: org.springframework.web.multipart.MultipartFile,",
874+
"marker: MultipartMixedRequestMarker?",
875+
"statusArray: kotlin.collections.List<MultipartMixedStatus>?"
876+
);
877+
878+
validateMultipartFiles(
879+
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartSingle",
880+
"file: org.springframework.web.multipart.MultipartFile?"
881+
);
882+
}
883+
884+
private void validateMultipartFiles(
885+
String filePrefix,
886+
String... lines
887+
) {
888+
Stream.of(
889+
filePrefix + "ApiDelegate.kt",
890+
filePrefix + "ApiService.kt",
891+
filePrefix + "ApiServiceImpl.kt",
892+
filePrefix + "Api.kt"
893+
)
894+
.map(Paths::get)
895+
.forEach(path -> {
896+
try {
897+
validateMultipartFile(path, lines);
898+
} catch (AssertionError e) {
899+
throw new AssertionError(path.toString() + " does not contain a line", e);
900+
}
901+
});
902+
}
903+
904+
private void validateMultipartFile(
905+
Path filePath,
906+
String... lines
907+
) {
908+
for(String line : lines) {
909+
assertFileContains(filePath, line);
910+
}
828911
}
829912

830913
@Test
@@ -887,7 +970,7 @@ public void givenMultipartForm_whenGenerateReactiveServer_thenParameterAreCreate
887970
assertFileContains(outputFilepath,
888971
"@Parameter(description = \"Additional data to pass to server\") @Valid @RequestParam(value = \"additionalMetadata\", required = false) additionalMetadata: kotlin.String?");
889972
assertFileContains(outputFilepath,
890-
"@Parameter(description = \"image to upload\") @Valid @RequestPart(\"image\", required = false) image: org.springframework.web.multipart.MultipartFile");
973+
"@Parameter(description = \"image to upload\") @Valid @RequestPart(\"image\", required = false) image: org.springframework.web.multipart.MultipartFile?)");
891974

892975
}
893976

0 commit comments

Comments
 (0)