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 (p.getDefault() != null) {
|
||||||
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
|
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
|
||||||
return "'''" + p.getDefault() + "'''";
|
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
|
else
|
||||||
// convert to enum var name later in postProcessModels
|
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
|
||||||
return (String) p.getDefault();
|
|
||||||
}
|
}
|
||||||
} else if (ModelUtils.isArraySchema(p)) {
|
} else if (ModelUtils.isArraySchema(p)) {
|
||||||
if (p.getDefault() != null) {
|
if (p.getDefault() != null) {
|
||||||
@ -711,7 +707,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
|
|
||||||
private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
|
private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
|
||||||
String indentation_string = "";
|
String indentation_string = "";
|
||||||
for (int i = 0; i < indentation; i++) indentation_string += " ";
|
for (int i=0 ; i< indentation ; i++) indentation_string += " ";
|
||||||
String example = super.toExampleValue(schema);
|
String example = super.toExampleValue(schema);
|
||||||
|
|
||||||
if (ModelUtils.isNullType(schema) && null != example) {
|
if (ModelUtils.isNullType(schema) && null != example) {
|
||||||
@ -720,11 +716,9 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
return "None";
|
return "None";
|
||||||
}
|
}
|
||||||
// correct "true"s into "True"s, since super.toExampleValue uses "toString()" on Java booleans
|
// correct "true"s into "True"s, since super.toExampleValue uses "toString()" on Java booleans
|
||||||
if (ModelUtils.isBooleanSchema(schema) && null != example) {
|
if (ModelUtils.isBooleanSchema(schema) && null!=example) {
|
||||||
if ("false".equalsIgnoreCase(example))
|
if ("false".equalsIgnoreCase(example)) example = "False";
|
||||||
example = "False";
|
else example = "True";
|
||||||
else
|
|
||||||
example = "True";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// correct "'"s into "'"s after toString()
|
// 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 (StringUtils.isNotBlank(example) && !"null".equals(example)) {
|
||||||
if (ModelUtils.isStringSchema(schema)) {
|
if (ModelUtils.isStringSchema(schema)) {
|
||||||
// wrap using double quotes to avoid the need to escape any embedded single quotes
|
example = "'" + example + "'";
|
||||||
example = "\"" + example + "\"";
|
|
||||||
}
|
}
|
||||||
return example;
|
return example;
|
||||||
}
|
}
|
||||||
@ -785,22 +778,13 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
example = "YQ==";
|
example = "YQ==";
|
||||||
} else if (ModelUtils.isStringSchema(schema)) {
|
} else if (ModelUtils.isStringSchema(schema)) {
|
||||||
// a BigDecimal:
|
// a BigDecimal:
|
||||||
if ("Number".equalsIgnoreCase(schema.getFormat())) {
|
if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";}
|
||||||
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 (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;
|
int len = 0;
|
||||||
|
if (null != schema.getMinLength()) len = schema.getMinLength().intValue();
|
||||||
if (null != schema.getMinLength())
|
if (len < 1) len = 1;
|
||||||
len = schema.getMinLength().intValue();
|
|
||||||
|
|
||||||
if (len < 1)
|
|
||||||
len = 1;
|
|
||||||
|
|
||||||
example = "";
|
example = "";
|
||||||
for (int i = 0; i < len; i++) example += i;
|
for (int i=0;i<len;i++) example += i;
|
||||||
} else if (ModelUtils.isIntegerSchema(schema)) {
|
} else if (ModelUtils.isIntegerSchema(schema)) {
|
||||||
if (schema.getMinimum() != null)
|
if (schema.getMinimum() != null)
|
||||||
example = schema.getMinimum().toString();
|
example = schema.getMinimum().toString();
|
||||||
@ -818,7 +802,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
included_schemas.add(schema.getTitle());
|
included_schemas.add(schema.getTitle());
|
||||||
}
|
}
|
||||||
ArraySchema arrayschema = (ArraySchema) schema;
|
ArraySchema arrayschema = (ArraySchema) schema;
|
||||||
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation + 1) + "\n" + indentation_string + "]";
|
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation+1) + "\n" + indentation_string + "]";
|
||||||
} else if (ModelUtils.isMapSchema(schema)) {
|
} else if (ModelUtils.isMapSchema(schema)) {
|
||||||
if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) {
|
if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) {
|
||||||
included_schemas.add(schema.getTitle());
|
included_schemas.add(schema.getTitle());
|
||||||
@ -833,7 +817,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
the_key = "'" + escapeText(the_key) + "'";
|
the_key = "'" + escapeText(the_key) + "'";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation + 1) + "\n" + indentation_string + "}";
|
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation+1) + "\n" + indentation_string + "}";
|
||||||
} else {
|
} else {
|
||||||
example = "{ }";
|
example = "{ }";
|
||||||
}
|
}
|
||||||
@ -845,11 +829,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
|
|
||||||
// I remove any property that is a discriminator, since it is not well supported by the python generator
|
// I remove any property that is a discriminator, since it is not well supported by the python generator
|
||||||
String toExclude = null;
|
String toExclude = null;
|
||||||
if (schema.getDiscriminator() != null) {
|
if (schema.getDiscriminator()!=null) {
|
||||||
toExclude = schema.getDiscriminator().getPropertyName();
|
toExclude = schema.getDiscriminator().getPropertyName();
|
||||||
}
|
}
|
||||||
|
|
||||||
example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "(";
|
example = packageName + ".models." + underscore(schema.getTitle())+"."+schema.getTitle()+"(";
|
||||||
|
|
||||||
// if required only:
|
// if required only:
|
||||||
// List<String> reqs = schema.getRequired();
|
// List<String> reqs = schema.getRequired();
|
||||||
@ -863,8 +847,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
|
|
||||||
Map<String, Schema> properties = schema.getProperties();
|
Map<String, Schema> properties = schema.getProperties();
|
||||||
Set<String> propkeys = null;
|
Set<String> propkeys = null;
|
||||||
if (properties != null)
|
if (properties != null) propkeys = properties.keySet();
|
||||||
propkeys = properties.keySet();
|
|
||||||
if (toExclude != null && reqs.contains(toExclude)) {
|
if (toExclude != null && reqs.contains(toExclude)) {
|
||||||
reqs.remove(toExclude);
|
reqs.remove(toExclude);
|
||||||
}
|
}
|
||||||
@ -891,7 +874,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
example += ")";
|
example +=")";
|
||||||
} else {
|
} else {
|
||||||
LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue");
|
LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue");
|
||||||
}
|
}
|
||||||
@ -919,7 +902,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
type = p.dataType;
|
type = p.dataType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != null) {
|
|
||||||
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
|
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
|
||||||
if (example == null) {
|
if (example == null) {
|
||||||
example = p.paramName + "_example";
|
example = p.paramName + "_example";
|
||||||
@ -958,7 +940,6 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
} else {
|
} else {
|
||||||
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
|
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (example == null) {
|
if (example == null) {
|
||||||
example = "None";
|
example = "None";
|
||||||
|
@ -17,24 +17,30 @@
|
|||||||
package org.openapitools.codegen.languages;
|
package org.openapitools.codegen.languages;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.Operation;
|
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.*;
|
||||||
|
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.parameters.RequestBody;
|
||||||
import io.swagger.v3.oas.models.responses.ApiResponse;
|
import io.swagger.v3.oas.models.responses.ApiResponse;
|
||||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.openapitools.codegen.*;
|
import org.openapitools.codegen.*;
|
||||||
import org.openapitools.codegen.examples.ExampleGenerator;
|
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.meta.features.*;
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
import org.openapitools.codegen.utils.ModelUtils;
|
||||||
import org.openapitools.codegen.utils.ProcessUtils;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -299,7 +305,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
// name looks like cat.Cat
|
// name looks like cat.Cat
|
||||||
String moduleName = name.split("\\.")[0];
|
String moduleName = name.split("\\.")[0];
|
||||||
// https://exceptionshub.com/circular-or-cyclic-imports-in-python.html
|
// https://exceptionshub.com/circular-or-cyclic-imports-in-python.html
|
||||||
return "from " + modelPackage() + " import " + moduleName;
|
return "from " + modelPackage() + " import "+ moduleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String robustImport(String name) {
|
private String robustImport(String name) {
|
||||||
@ -307,7 +313,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
String moduleName = name.split("\\.")[0];
|
String moduleName = name.split("\\.")[0];
|
||||||
// https://exceptionshub.com/circular-or-cyclic-imports-in-python.html
|
// https://exceptionshub.com/circular-or-cyclic-imports-in-python.html
|
||||||
String modelImport = "try:\n from " + modelPackage() +
|
String modelImport = "try:\n from " + modelPackage() +
|
||||||
" import " + moduleName + "\nexcept ImportError:\n " +
|
" import " + moduleName+ "\nexcept ImportError:\n " +
|
||||||
moduleName + " = sys.modules[\n '" + modelPackage() + "." + moduleName + "']";
|
moduleName + " = sys.modules[\n '" + modelPackage() + "." + moduleName + "']";
|
||||||
return modelImport;
|
return modelImport;
|
||||||
}
|
}
|
||||||
@ -340,9 +346,9 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("static-method")
|
@SuppressWarnings("static-method")
|
||||||
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
||||||
HashMap<String, Object> val = (HashMap<String, Object>) objs.get("operations");
|
HashMap<String, Object> val = (HashMap<String, Object>)objs.get("operations");
|
||||||
ArrayList<CodegenOperation> operations = (ArrayList<CodegenOperation>) val.get("operation");
|
ArrayList<CodegenOperation> operations = (ArrayList<CodegenOperation>) val.get("operation");
|
||||||
ArrayList<HashMap<String, String>> imports = (ArrayList<HashMap<String, String>>) objs.get("imports");
|
ArrayList<HashMap<String, String>> imports = (ArrayList<HashMap<String, String>>)objs.get("imports");
|
||||||
imports.clear();
|
imports.clear();
|
||||||
for (CodegenOperation operation : operations) {
|
for (CodegenOperation operation : operations) {
|
||||||
fixOperationImports(operation.imports);
|
fixOperationImports(operation.imports);
|
||||||
@ -714,7 +720,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
public void postProcessParameter(CodegenParameter p) {
|
public void postProcessParameter(CodegenParameter p) {
|
||||||
postProcessPattern(p.pattern, p.vendorExtensions);
|
postProcessPattern(p.pattern, p.vendorExtensions);
|
||||||
// set baseType to null so the api docs will not point to a model for languageSpecificPrimitives
|
// set baseType to null so the api docs will not point to a model for languageSpecificPrimitives
|
||||||
if (p.baseType != null && languageSpecificPrimitives.contains(p.baseType)) {
|
if (p.baseType != null && languageSpecificPrimitives.contains(p.baseType)){
|
||||||
p.baseType = null;
|
p.baseType = null;
|
||||||
} else if (p.isListContainer && p.mostInnerItems.complexType != null && !languageSpecificPrimitives.contains(p.mostInnerItems.complexType)) {
|
} else if (p.isListContainer && p.mostInnerItems.complexType != null && !languageSpecificPrimitives.contains(p.mostInnerItems.complexType)) {
|
||||||
// fix ListContainers
|
// fix ListContainers
|
||||||
@ -722,7 +728,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel result) {
|
private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel result){
|
||||||
// for composed schema models, if the required properties are only from oneOf or anyOf models
|
// for composed schema models, if the required properties are only from oneOf or anyOf models
|
||||||
// give them a nulltype.Null so the user can omit including them in python
|
// give them a nulltype.Null so the user can omit including them in python
|
||||||
ComposedSchema cs = (ComposedSchema) schema;
|
ComposedSchema cs = (ComposedSchema) schema;
|
||||||
@ -744,7 +750,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
if (anyOf != null) {
|
if (anyOf != null) {
|
||||||
oneOfanyOfSchemas.addAll(anyOf);
|
oneOfanyOfSchemas.addAll(anyOf);
|
||||||
}
|
}
|
||||||
for (Schema sc : oneOfanyOfSchemas) {
|
for (Schema sc: oneOfanyOfSchemas) {
|
||||||
Schema refSchema = ModelUtils.getReferencedSchema(this.openAPI, sc);
|
Schema refSchema = ModelUtils.getReferencedSchema(this.openAPI, sc);
|
||||||
addProperties(otherProperties, otherRequired, refSchema);
|
addProperties(otherProperties, otherRequired, refSchema);
|
||||||
}
|
}
|
||||||
@ -762,7 +768,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
|
|
||||||
List<CodegenProperty> reqVars = result.getRequiredVars();
|
List<CodegenProperty> reqVars = result.getRequiredVars();
|
||||||
if (reqVars != null) {
|
if (reqVars != null) {
|
||||||
for (CodegenProperty cp : reqVars) {
|
for (CodegenProperty cp: reqVars) {
|
||||||
String propName = cp.baseName;
|
String propName = cp.baseName;
|
||||||
if (otherRequiredSet.contains(propName) && !selfRequiredSet.contains(propName)) {
|
if (otherRequiredSet.contains(propName) && !selfRequiredSet.contains(propName)) {
|
||||||
// if var is in otherRequiredSet and is not in selfRequiredSet and is in result.requiredVars
|
// if var is in otherRequiredSet and is not in selfRequiredSet and is in result.requiredVars
|
||||||
@ -899,11 +905,11 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
* Primitive types in the OAS specification are implemented in Python using the corresponding
|
* Primitive types in the OAS specification are implemented in Python using the corresponding
|
||||||
* Python primitive types.
|
* Python primitive types.
|
||||||
* Composed types (e.g. allAll, oneOf, anyOf) are represented in Python using list of 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
|
* 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
|
* getTypeString invokes itself recursively. A non-empty prefix/suffix may be specified
|
||||||
* to wrap the return value in a python dict, list or tuple.
|
* to wrap the return value in a python dict, list or tuple.
|
||||||
* <p>
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
* - "bool, date, float" The data must be a bool, date or float.
|
* - "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.
|
* - "[bool, date]" The data must be an array, and the array items must be a bool or date.
|
||||||
@ -1052,7 +1058,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
if (example == null) {
|
if (example == null) {
|
||||||
example = "/path/to/file";
|
example = "/path/to/file";
|
||||||
}
|
}
|
||||||
example = "open('" + example + "', 'rb')";
|
example = "open('"+example+"', 'rb')";
|
||||||
} else if ("Date".equalsIgnoreCase(type)) {
|
} else if ("Date".equalsIgnoreCase(type)) {
|
||||||
if (example == null) {
|
if (example == null) {
|
||||||
example = "2013-10-20";
|
example = "2013-10-20";
|
||||||
@ -1123,7 +1129,7 @@ public class PythonClientExperimentalCodegen extends PythonClientCodegen {
|
|||||||
@Override
|
@Override
|
||||||
public String toModelName(String name) {
|
public String toModelName(String name) {
|
||||||
// we have a custom version of this function so we can support circular references in python 2 and 3
|
// we have a custom version of this function so we can support circular references in python 2 and 3
|
||||||
return toModelFilename(name) + "." + simpleModelName(name);
|
return toModelFilename(name)+"."+simpleModelName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,17 +19,16 @@ package org.openapitools.codegen.python;
|
|||||||
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import io.swagger.v3.oas.models.Operation;
|
import io.swagger.v3.oas.models.Operation;
|
||||||
import io.swagger.v3.oas.models.media.Schema;
|
|
||||||
import io.swagger.v3.oas.models.media.StringSchema;
|
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.languages.PythonClientCodegen;
|
||||||
import org.openapitools.codegen.utils.ModelUtils;
|
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
public class PythonClientCodegenTest {
|
public class PythonClientCodegenTest {
|
||||||
|
|
||||||
@ -94,99 +93,12 @@ public class PythonClientCodegenTest {
|
|||||||
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
|
Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(description = "test default value with single quotes")
|
@Test(description = "test single quotes escape")
|
||||||
public void testSingleQuotesDefaultValue() {
|
public void testSingleQuotes() {
|
||||||
final PythonClientCodegen codegen = new PythonClientCodegen();
|
final PythonClientCodegen codegen = new PythonClientCodegen();
|
||||||
StringSchema schema = new StringSchema();
|
StringSchema schema = new StringSchema();
|
||||||
schema.setDefault("Text containing 'single' quote");
|
schema.setDefault("Text containing 'single' quote");
|
||||||
String defaultValue = codegen.toDefaultValue(schema);
|
String defaultValue = codegen.toDefaultValue(schema);
|
||||||
Assert.assertEquals(defaultValue, "\"Text containing 'single' quote\"");
|
Assert.assertEquals("'Text containing \'single\' quote'", defaultValue);
|
||||||
}
|
|
||||||
|
|
||||||
@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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ public class PythonClientExperimentalTest {
|
|||||||
|
|
||||||
@Test(description = "convert a python model with dots")
|
@Test(description = "convert a python model with dots")
|
||||||
public void modelTest() {
|
public void modelTest() {
|
||||||
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/v1beta3.json");
|
final OpenAPI openAPI= TestUtils.parseFlattenSpec("src/test/resources/2_0/v1beta3.json");
|
||||||
final DefaultCodegen codegen = new PythonClientExperimentalCodegen();
|
final DefaultCodegen codegen = new PythonClientExperimentalCodegen();
|
||||||
codegen.setOpenAPI(openAPI);
|
codegen.setOpenAPI(openAPI);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user