diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index e8df9d1b8ca5..21617e5c47c2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -683,9 +683,12 @@ public String toDefaultValue(Schema p) { if (p.getDefault() != null) { if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find()) return "'''" + p.getDefault() + "'''"; - else + else if (p.getEnum() == null) // wrap using double quotes to avoid the need to escape any embedded single quotes return "\"" + p.getDefault() + "\""; + else + // convert to enum var name later in postProcessModels + return (String) p.getDefault(); } } else if (ModelUtils.isArraySchema(p)) { if (p.getDefault() != null) { @@ -904,43 +907,45 @@ public void setParameterExampleValue(CodegenParameter p) { type = p.dataType; } - if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { + if (type != null) { + if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { if (example == null) { - example = p.paramName + "_example"; + example = p.paramName + "_example"; } example = "'" + escapeText(example) + "'"; - } else if ("Integer".equals(type) || "int".equals(type)) { + } else if ("Integer".equals(type) || "int".equals(type)) { if (example == null) { - example = "56"; + example = "56"; } - } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { + } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { if (example == null) { - example = "3.4"; + example = "3.4"; } - } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { + } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { if (example == null) { - example = "True"; + example = "True"; } - } else if ("file".equalsIgnoreCase(type)) { + } else if ("file".equalsIgnoreCase(type)) { if (example == null) { - example = "/path/to/file"; + example = "/path/to/file"; } example = "'" + escapeText(example) + "'"; - } else if ("Date".equalsIgnoreCase(type)) { + } else if ("Date".equalsIgnoreCase(type)) { if (example == null) { - example = "2013-10-20"; + example = "2013-10-20"; } example = "'" + escapeText(example) + "'"; - } else if ("DateTime".equalsIgnoreCase(type)) { + } else if ("DateTime".equalsIgnoreCase(type)) { if (example == null) { - example = "2013-10-20T19:20:30+01:00"; + example = "2013-10-20T19:20:30+01:00"; } example = "'" + escapeText(example) + "'"; - } else if (!languageSpecificPrimitives.contains(type)) { + } else if (!languageSpecificPrimitives.contains(type)) { // type is a model class, e.g. User example = this.packageName + "." + type + "()"; - } else { + } else { LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); + } } if (example == null) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java index 3c2544edf91e..687a04ec3acf 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientCodegenTest.java @@ -19,11 +19,17 @@ import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; +import java.util.HashSet; import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenModelFactory; +import org.openapitools.codegen.CodegenModelType; import org.openapitools.codegen.CodegenOperation; +import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.PythonClientCodegen; +import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; @@ -110,4 +116,82 @@ public void testSingleQuotesExampleValue() { String exampleValue = codegen.toExampleValue(schema); Assert.assertEquals(exampleValue, "\"Text containing 'single' quote\""); } + + @Test + public void testFormParameterHasDefaultValue() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml"); + final PythonClientCodegen codegen = new PythonClientCodegen(); + codegen.setOpenAPI(openAPI); + + Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody()); + CodegenParameter codegenParameter = codegen.fromFormProperty("enum_form_string", (Schema) requestBodySchema.getProperties().get("enum_form_string"), new HashSet()); + + Assert.assertEquals(codegenParameter.defaultValue, "-efg"); + } + + @Test + public void testExample1() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); + final PythonClientCodegen codegen = new PythonClientCodegen(); + + Operation operation = openAPI.getPaths().get("/example1/singular").getGet(); + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); + + Assert.assertEquals(codegenParameter.example, "example1 value"); + + Operation operation2 = openAPI.getPaths().get("/example1/plural").getGet(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); + + Assert.assertEquals(codegenParameter2.example, "An example1 value"); + } + + @Test + public void testExample2() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); + final PythonClientCodegen codegen = new PythonClientCodegen(); + + Operation operation = openAPI.getPaths().get("/example2/singular").getGet(); + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); + + Assert.assertEquals(codegenParameter.example, "example2 value"); + } + + @Test + public void testExample3() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); + final PythonClientCodegen codegen = new PythonClientCodegen(); + + Operation operation = openAPI.getPaths().get("/example3/singular").getGet(); + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0)); + + Assert.assertEquals(codegenParameter.example, "example3: parameter value"); + + Operation operation2 = openAPI.getPaths().get("/example3/plural").getGet(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0)); + + Assert.assertEquals(codegenParameter2.example, "example3: parameter value"); + } + + @Test + public void testExample4() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml"); + final PythonClientCodegen codegen = new PythonClientCodegen(); + + Operation operation = openAPI.getPaths().get("/example4/singular").getPost(); + CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody()); + + Assert.assertEquals(codegenParameter.example, "example4 value"); + + Operation operation2 = openAPI.getPaths().get("/example4/plural").getPost(); + CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER); + codegen.setParameterExampleValue(codegenParameter2, operation2.getRequestBody()); + + Assert.assertEquals(codegenParameter2.example, "An example4 value"); + } }