forked from loafle/openapi-generator-original
* Revert "Minor enhancement to Python client generator's code format (#6510)" This reverts commit 3ddc78393cf936076778783a956db613b3a07812. * Revert "[Python][Client] Fix delimiter collision issue #5981 (#6451)" This reverts commit 6783b90fe230360c58b360817887f418475b745b.
This commit is contained in:
parent
3ddc78393c
commit
f91a5f7108
@ -683,12 +683,8 @@ 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
|
||||
// convert to enum var name later in postProcessModels
|
||||
return (String) p.getDefault();
|
||||
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
|
||||
}
|
||||
} else if (ModelUtils.isArraySchema(p)) {
|
||||
if (p.getDefault() != null) {
|
||||
@ -721,10 +717,8 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
}
|
||||
// correct "true"s into "True"s, since super.toExampleValue uses "toString()" on Java booleans
|
||||
if (ModelUtils.isBooleanSchema(schema) && null!=example) {
|
||||
if ("false".equalsIgnoreCase(example))
|
||||
example = "False";
|
||||
else
|
||||
example = "True";
|
||||
if ("false".equalsIgnoreCase(example)) example = "False";
|
||||
else example = "True";
|
||||
}
|
||||
|
||||
// correct "'"s into "'"s after toString()
|
||||
@ -734,8 +728,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
|
||||
if (StringUtils.isNotBlank(example) && !"null".equals(example)) {
|
||||
if (ModelUtils.isStringSchema(schema)) {
|
||||
// wrap using double quotes to avoid the need to escape any embedded single quotes
|
||||
example = "\"" + example + "\"";
|
||||
example = "'" + example + "'";
|
||||
}
|
||||
return example;
|
||||
}
|
||||
@ -785,20 +778,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
example = "YQ==";
|
||||
} else if (ModelUtils.isStringSchema(schema)) {
|
||||
// a BigDecimal:
|
||||
if ("Number".equalsIgnoreCase(schema.getFormat())) {
|
||||
return "1";
|
||||
}
|
||||
if (StringUtils.isNotBlank(schema.getPattern()))
|
||||
return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp
|
||||
|
||||
if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";}
|
||||
if (StringUtils.isNotBlank(schema.getPattern())) return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp
|
||||
int len = 0;
|
||||
|
||||
if (null != schema.getMinLength())
|
||||
len = schema.getMinLength().intValue();
|
||||
|
||||
if (len < 1)
|
||||
len = 1;
|
||||
|
||||
if (null != schema.getMinLength()) len = schema.getMinLength().intValue();
|
||||
if (len < 1) len = 1;
|
||||
example = "";
|
||||
for (int i=0;i<len;i++) example += i;
|
||||
} else if (ModelUtils.isIntegerSchema(schema)) {
|
||||
@ -863,8 +847,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
|
||||
Map<String, Schema> properties = schema.getProperties();
|
||||
Set<String> propkeys = null;
|
||||
if (properties != null)
|
||||
propkeys = properties.keySet();
|
||||
if (properties != null) propkeys = properties.keySet();
|
||||
if (toExclude != null && reqs.contains(toExclude)) {
|
||||
reqs.remove(toExclude);
|
||||
}
|
||||
@ -919,7 +902,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
type = p.dataType;
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
|
||||
if (example == null) {
|
||||
example = p.paramName + "_example";
|
||||
@ -958,7 +940,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
} else {
|
||||
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
|
||||
}
|
||||
}
|
||||
|
||||
if (example == null) {
|
||||
example = "None";
|
||||
|
@ -17,24 +17,30 @@
|
||||
package org.openapitools.codegen.languages;
|
||||
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.examples.Example;
|
||||
import io.swagger.v3.oas.models.media.*;
|
||||
import io.swagger.v3.oas.models.media.ArraySchema;
|
||||
import io.swagger.v3.oas.models.media.MediaType;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||
import io.swagger.v3.oas.models.parameters.RequestBody;
|
||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.examples.ExampleGenerator;
|
||||
import org.openapitools.codegen.meta.GeneratorMetadata;
|
||||
import org.openapitools.codegen.meta.Stability;
|
||||
import org.openapitools.codegen.meta.features.*;
|
||||
import org.openapitools.codegen.utils.ModelUtils;
|
||||
import org.openapitools.codegen.utils.ProcessUtils;
|
||||
import org.openapitools.codegen.meta.GeneratorMetadata;
|
||||
import org.openapitools.codegen.meta.Stability;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -899,11 +905,11 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
||||
* Primitive types in the OAS specification are implemented in Python using the corresponding
|
||||
* Python primitive types.
|
||||
* Composed types (e.g. allAll, oneOf, anyOf) are represented in Python using list of types.
|
||||
* <p>
|
||||
*
|
||||
* The caller should set the prefix and suffix arguments to empty string, except when
|
||||
* getTypeString invokes itself recursively. A non-empty prefix/suffix may be specified
|
||||
* to wrap the return value in a python dict, list or tuple.
|
||||
* <p>
|
||||
*
|
||||
* Examples:
|
||||
* - "bool, date, float" The data must be a bool, date or float.
|
||||
* - "[bool, date]" The data must be an array, and the array items must be a bool or date.
|
||||
|
@ -19,17 +19,16 @@ 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 org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class PythonClientCodegenTest {
|
||||
|
||||
@ -94,99 +93,12 @@ public class PythonClientCodegenTest {
|
||||
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
|
||||
}
|
||||
|
||||
@Test(description = "test default value with single quotes")
|
||||
public void testSingleQuotesDefaultValue() {
|
||||
@Test(description = "test single quotes escape")
|
||||
public void testSingleQuotes() {
|
||||
final PythonClientCodegen codegen = new PythonClientCodegen();
|
||||
StringSchema schema = new StringSchema();
|
||||
schema.setDefault("Text containing 'single' quote");
|
||||
String defaultValue = codegen.toDefaultValue(schema);
|
||||
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");
|
||||
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user