Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancement to Python client generator's code format #6510

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public String toExampleValue(Schema schema) {

private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
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);

if (ModelUtils.isNullType(schema) && null != example) {
Expand All @@ -720,9 +720,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
return "None";
}
// 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 (ModelUtils.isBooleanSchema(schema) && null != example) {
if ("false".equalsIgnoreCase(example))
example = "False";
else
example = "True";
}

// correct "&#39;"s into "'"s after toString()
Expand All @@ -739,7 +741,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
}

if (schema.getEnum() != null && !schema.getEnum().isEmpty()) {
// Enum case:
// Enum case:
example = schema.getEnum().get(0).toString();
if (ModelUtils.isStringSchema(schema)) {
example = "'" + escapeText(example) + "'";
Expand All @@ -749,7 +751,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche

return example;
} else if (null != schema.get$ref()) {
// $ref case:
// $ref case:
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
String ref = ModelUtils.getSimpleRef(schema.get$ref());
if (allDefinitions != null) {
Expand Down Expand Up @@ -783,13 +785,22 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
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;
for (int i = 0; i < len; i++) example += i;
} else if (ModelUtils.isIntegerSchema(schema)) {
if (schema.getMinimum() != null)
example = schema.getMinimum().toString();
Expand All @@ -807,7 +818,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
included_schemas.add(schema.getTitle());
}
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)) {
if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) {
included_schemas.add(schema.getTitle());
Expand All @@ -822,7 +833,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
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 {
example = "{ }";
}
Expand All @@ -834,11 +845,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche

// I remove any property that is a discriminator, since it is not well supported by the python generator
String toExclude = null;
if (schema.getDiscriminator()!=null) {
if (schema.getDiscriminator() != null) {
toExclude = schema.getDiscriminator().getPropertyName();
}

example = packageName + ".models." + underscore(schema.getTitle())+"."+schema.getTitle()+"(";
example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "(";

// if required only:
// List<String> reqs = schema.getRequired();
Expand All @@ -852,7 +863,8 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche

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);
}
Expand All @@ -879,7 +891,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
}
}
}
example +=")";
example += ")";
} else {
LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue");
}
Expand Down Expand Up @@ -908,44 +920,44 @@ public void setParameterExampleValue(CodegenParameter p) {
}

if (type != null) {
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
if (example == null) {
example = p.paramName + "_example";
}
example = "'" + escapeText(example) + "'";
} else if ("Integer".equals(type) || "int".equals(type)) {
if (example == null) {
example = "56";
}
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
if (example == null) {
example = "3.4";
}
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
if (example == null) {
example = "True";
}
} else if ("file".equalsIgnoreCase(type)) {
if (example == null) {
example = "/path/to/file";
}
example = "'" + escapeText(example) + "'";
} else if ("Date".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20";
}
example = "'" + escapeText(example) + "'";
} else if ("DateTime".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20T19:20:30+01:00";
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
if (example == null) {
example = p.paramName + "_example";
}
example = "'" + escapeText(example) + "'";
} else if ("Integer".equals(type) || "int".equals(type)) {
if (example == null) {
example = "56";
}
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
if (example == null) {
example = "3.4";
}
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
if (example == null) {
example = "True";
}
} else if ("file".equalsIgnoreCase(type)) {
if (example == null) {
example = "/path/to/file";
}
example = "'" + escapeText(example) + "'";
} else if ("Date".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20";
}
example = "'" + escapeText(example) + "'";
} else if ("DateTime".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20T19:20:30+01:00";
}
example = "'" + escapeText(example) + "'";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = this.packageName + "." + type + "()";
} else {
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
}
example = "'" + escapeText(example) + "'";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = this.packageName + "." + type + "()";
} else {
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
}
}

if (example == null) {
Expand Down
Loading