From 1a31b058e3bf487b41551724ffd66557b3facba6 Mon Sep 17 00:00:00 2001 From: Diorcet Yann Date: Mon, 3 Jun 2024 14:05:35 +0200 Subject: [PATCH] Fix python-fastapi signature of parameters for method --- .../languages/AbstractPythonCodegen.java | 16 ++++- .../languages/PythonFastAPIServerCodegen.java | 2 + .../python-fastapi/param_type.mustache | 2 +- .../PythonFastAPIServerCodegenTest.java | 60 +++++++++++++++++++ .../src/test/resources/bugs/pr_18691.json | 24 ++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFastAPIServerCodegenTest.java create mode 100644 modules/openapi-generator/src/test/resources/bugs/pr_18691.json diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index 36b74be9578b..916fc00fbd01 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -45,12 +45,12 @@ import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.CodegenConstants; -import org.openapitools.codegen.CodegenDiscriminator; import org.openapitools.codegen.CodegenMediaType; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.CodegenResponse; import org.openapitools.codegen.DefaultCodegen; import org.openapitools.codegen.GeneratorLanguage; import org.openapitools.codegen.IJsonSchemaValidationProperties; @@ -1263,6 +1263,20 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List allModels) { + super.postProcessOperationsWithModels(objs, allModels); + OperationMap operations = objs.getOperations(); // Set will make sure that no duplicated items are used. Set securityImports = new HashSet<>(); diff --git a/modules/openapi-generator/src/main/resources/python-fastapi/param_type.mustache b/modules/openapi-generator/src/main/resources/python-fastapi/param_type.mustache index d97c018015fa..0fd1f7fd9cad 100644 --- a/modules/openapi-generator/src/main/resources/python-fastapi/param_type.mustache +++ b/modules/openapi-generator/src/main/resources/python-fastapi/param_type.mustache @@ -1 +1 @@ -{{#isString}}str{{/isString}}{{#isInteger}}int{{/isInteger}}{{#isLong}}int{{/isLong}}{{#isFloat}}float{{/isFloat}}{{#isDouble}}float{{/isDouble}}{{#isByteArray}}str{{/isByteArray}}{{#isBinary}}str{{/isBinary}}{{#isBoolean}}bool{{/isBoolean}}{{#isDate}}str{{/isDate}}{{#isDateTime}}str{{/isDateTime}}{{#isModel}}{{dataType}}{{/isModel}}{{#isContainer}}{{dataType}}{{/isContainer}} \ No newline at end of file +{{{vendorExtensions.x-py-typing}}} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFastAPIServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFastAPIServerCodegenTest.java new file mode 100644 index 000000000000..52a6789ca154 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFastAPIServerCodegenTest.java @@ -0,0 +1,60 @@ +package org.openapitools.codegen.python; + +import com.google.common.collect.Sets; +import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.parser.core.models.ParseOptions; +import org.openapitools.codegen.*; +import org.openapitools.codegen.languages.PythonClientCodegen; +import org.openapitools.codegen.languages.PythonFastAPIServerCodegen; +import org.openapitools.codegen.languages.features.CXFServerFeatures; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import static org.openapitools.codegen.TestUtils.assertFileContains; +import static org.openapitools.codegen.TestUtils.assertFileExists; + +public class PythonFastAPIServerCodegenTest { + + // Helper function, intended to reduce boilerplate + static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException { + final File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + final String outputPath = output.getAbsolutePath().replace('\\', '/'); + + codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); + + final ClientOptInput input = new ClientOptInput(); + final OpenAPI openAPI = new OpenAPIParser().readLocation(filePath, null, new ParseOptions()).getOpenAPI(); + input.openAPI(openAPI); + input.config(codegen); + + final DefaultGenerator generator = new DefaultGenerator(); + final List files = generator.opts(input).generate(); + + Assert.assertTrue(files.size() > 0); + return outputPath + "/"; + } + + + @Test(description = "test containerType in parameters") + public void testContainerType() throws IOException { + final DefaultCodegen codegen = new PythonFastAPIServerCodegen(); + final String outputPath = generateFiles(codegen, "src/test/resources/bugs/pr_18691.json"); + final Path p = Paths.get(outputPath + "src/openapi_server/apis/default_api.py"); + + assertFileExists(p); + assertFileContains(p, "body: Optional[Dict[str, Any]] = Body(None, description=\"\"),"); + } +} diff --git a/modules/openapi-generator/src/test/resources/bugs/pr_18691.json b/modules/openapi-generator/src/test/resources/bugs/pr_18691.json new file mode 100644 index 000000000000..ed2b0664d954 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/pr_18691.json @@ -0,0 +1,24 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "paths": { + "/licensing/token/renew": { + "post": { + "description": "Manually ask license issuer for a new token. Available in Grafana Enterprise v7.4+.\n\nYou need to have a permission with action `licensing:update`.", + "operationId": "postRenewLicenseToken", + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + } + } + } +} \ No newline at end of file