|
| 1 | +package org.openapitools.codegen.python; |
| 2 | + |
| 3 | +import io.swagger.parser.OpenAPIParser; |
| 4 | +import io.swagger.v3.oas.models.OpenAPI; |
| 5 | +import io.swagger.v3.parser.core.models.ParseOptions; |
| 6 | +import org.openapitools.codegen.*; |
| 7 | +import org.openapitools.codegen.languages.PythonFlaskConnexionServerCodegen; |
| 8 | +import org.openapitools.codegen.languages.features.CXFServerFeatures; |
| 9 | +import org.testng.Assert; |
| 10 | +import org.testng.annotations.Test; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.openapitools.codegen.TestUtils.assertFileContains; |
| 20 | +import static org.openapitools.codegen.TestUtils.assertFileExists; |
| 21 | + |
| 22 | +public class PythonFlaskConnexionServerCodegenTest { |
| 23 | + |
| 24 | + // Helper function, intended to reduce boilerplate |
| 25 | + static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException { |
| 26 | + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 27 | + output.deleteOnExit(); |
| 28 | + final String outputPath = output.getAbsolutePath().replace('\\', '/'); |
| 29 | + |
| 30 | + codegen.setOutputDir(output.getAbsolutePath()); |
| 31 | + codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); |
| 32 | + |
| 33 | + final ClientOptInput input = new ClientOptInput(); |
| 34 | + final OpenAPI openAPI = new OpenAPIParser().readLocation(filePath, null, new ParseOptions()).getOpenAPI(); |
| 35 | + input.openAPI(openAPI); |
| 36 | + input.config(codegen); |
| 37 | + |
| 38 | + final DefaultGenerator generator = new DefaultGenerator(); |
| 39 | + final List<File> files = generator.opts(input).generate(); |
| 40 | + |
| 41 | + Assert.assertTrue(files.size() > 0); |
| 42 | + return outputPath + "/"; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Test(description = "test requestBody") |
| 47 | + public void testRequestBody() throws IOException { |
| 48 | + final DefaultCodegen codegen = new PythonFlaskConnexionServerCodegen(); |
| 49 | + final String outputPath = generateFiles(codegen, "src/test/resources/bugs/issue_1666.yaml"); |
| 50 | + |
| 51 | + final Path p1 = Paths.get(outputPath + "openapi_server/controllers/test1_controller.py"); |
| 52 | + assertFileExists(p1); |
| 53 | + assertFileContains(p1, "def not_required(body=None):"); |
| 54 | + assertFileContains(p1, "test_request = body"); |
| 55 | + |
| 56 | + final Path p2 = Paths.get(outputPath + "openapi_server/controllers/test2_controller.py"); |
| 57 | + assertFileContains(p2, "def required(body):"); |
| 58 | + assertFileContains(p2, "test_request = body"); |
| 59 | + |
| 60 | + final Path p3 = Paths.get(outputPath + "openapi_server/controllers/test3_controller.py"); |
| 61 | + assertFileContains(p3, "def with_path_param(param1, body=None):"); |
| 62 | + assertFileContains(p3, "test_request = body"); |
| 63 | + |
| 64 | + final Path p4 = Paths.get(outputPath + "openapi_server/controllers/test4_controller.py"); |
| 65 | + assertFileContains(p4, "def with_path_param_required(param1, body):"); |
| 66 | + assertFileContains(p4, "test_request = body"); |
| 67 | + } |
| 68 | +} |
0 commit comments