mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-08 16:40:56 +00:00
* [python][client] Fix delimiter collision (#5981) * [python][client] Fix delimiter collision (#5981) update samples * [python][client] Fix delimiter collision (#5981) update samples * [python][client] Fix convert to enum var name (#5981) * [python][client] Fix convert to enum var name (#5981) update samples
This commit is contained in:
parent
15be875275
commit
6783b90fe2
@ -683,8 +683,12 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
if (p.getDefault() != null) {
|
||||
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
|
||||
return "'''" + p.getDefault() + "'''";
|
||||
else if (p.getEnum() == null)
|
||||
// wrap using double quotes to avoid the need to escape any embedded single quotes
|
||||
return "\"" + p.getDefault() + "\"";
|
||||
else
|
||||
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
|
||||
// convert to enum var name later in postProcessModels
|
||||
return (String) p.getDefault();
|
||||
}
|
||||
} else if (ModelUtils.isArraySchema(p)) {
|
||||
if (p.getDefault() != null) {
|
||||
@ -728,7 +732,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
|
||||
if (StringUtils.isNotBlank(example) && !"null".equals(example)) {
|
||||
if (ModelUtils.isStringSchema(schema)) {
|
||||
example = "'" + example + "'";
|
||||
// wrap using double quotes to avoid the need to escape any embedded single quotes
|
||||
example = "\"" + example + "\"";
|
||||
}
|
||||
return example;
|
||||
}
|
||||
@ -902,43 +907,45 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
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) {
|
||||
|
@ -19,11 +19,17 @@ package org.openapitools.codegen.python;
|
||||
|
||||
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;
|
||||
|
||||
@ -93,12 +99,99 @@ public class PythonClientCodegenTest {
|
||||
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
|
||||
}
|
||||
|
||||
@Test(description = "test single quotes escape")
|
||||
public void testSingleQuotes() {
|
||||
@Test(description = "test default value with single quotes")
|
||||
public void testSingleQuotesDefaultValue() {
|
||||
final PythonClientCodegen codegen = new PythonClientCodegen();
|
||||
StringSchema schema = new StringSchema();
|
||||
schema.setDefault("Text containing 'single' quote");
|
||||
String defaultValue = codegen.toDefaultValue(schema);
|
||||
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
|
||||
Assert.assertEquals(defaultValue, "\"Text containing 'single' quote\"");
|
||||
}
|
||||
|
||||
@Test(description = "test example value with single quotes")
|
||||
public void testSingleQuotesExampleValue() {
|
||||
final PythonClientCodegen codegen = new PythonClientCodegen();
|
||||
StringSchema schema = new StringSchema();
|
||||
schema.setExample("Text containing 'single' quote");
|
||||
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<String>());
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user