Skip to content

Commit 3ddc783

Browse files
authored
Minor enhancement to Python client generator's code format (#6510)
* minor enhancement to python java class code format * minor format python experimental
1 parent d8c4223 commit 3ddc783

4 files changed

Lines changed: 100 additions & 99 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ public String toExampleValue(Schema schema) {
711711

712712
private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
713713
String indentation_string = "";
714-
for (int i=0 ; i< indentation ; i++) indentation_string += " ";
714+
for (int i = 0; i < indentation; i++) indentation_string += " ";
715715
String example = super.toExampleValue(schema);
716716

717717
if (ModelUtils.isNullType(schema) && null != example) {
@@ -720,9 +720,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
720720
return "None";
721721
}
722722
// correct "true"s into "True"s, since super.toExampleValue uses "toString()" on Java booleans
723-
if (ModelUtils.isBooleanSchema(schema) && null!=example) {
724-
if ("false".equalsIgnoreCase(example)) example = "False";
725-
else example = "True";
723+
if (ModelUtils.isBooleanSchema(schema) && null != example) {
724+
if ("false".equalsIgnoreCase(example))
725+
example = "False";
726+
else
727+
example = "True";
726728
}
727729

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

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

750752
return example;
751753
} else if (null != schema.get$ref()) {
752-
// $ref case:
754+
// $ref case:
753755
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
754756
String ref = ModelUtils.getSimpleRef(schema.get$ref());
755757
if (allDefinitions != null) {
@@ -783,13 +785,22 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
783785
example = "YQ==";
784786
} else if (ModelUtils.isStringSchema(schema)) {
785787
// a BigDecimal:
786-
if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";}
787-
if (StringUtils.isNotBlank(schema.getPattern())) return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp
788+
if ("Number".equalsIgnoreCase(schema.getFormat())) {
789+
return "1";
790+
}
791+
if (StringUtils.isNotBlank(schema.getPattern()))
792+
return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp
793+
788794
int len = 0;
789-
if (null != schema.getMinLength()) len = schema.getMinLength().intValue();
790-
if (len < 1) len = 1;
795+
796+
if (null != schema.getMinLength())
797+
len = schema.getMinLength().intValue();
798+
799+
if (len < 1)
800+
len = 1;
801+
791802
example = "";
792-
for (int i=0;i<len;i++) example += i;
803+
for (int i = 0; i < len; i++) example += i;
793804
} else if (ModelUtils.isIntegerSchema(schema)) {
794805
if (schema.getMinimum() != null)
795806
example = schema.getMinimum().toString();
@@ -807,7 +818,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
807818
included_schemas.add(schema.getTitle());
808819
}
809820
ArraySchema arrayschema = (ArraySchema) schema;
810-
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation+1) + "\n" + indentation_string + "]";
821+
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation + 1) + "\n" + indentation_string + "]";
811822
} else if (ModelUtils.isMapSchema(schema)) {
812823
if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) {
813824
included_schemas.add(schema.getTitle());
@@ -822,7 +833,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
822833
the_key = "'" + escapeText(the_key) + "'";
823834
}
824835
}
825-
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation+1) + "\n" + indentation_string + "}";
836+
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation + 1) + "\n" + indentation_string + "}";
826837
} else {
827838
example = "{ }";
828839
}
@@ -834,11 +845,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
834845

835846
// I remove any property that is a discriminator, since it is not well supported by the python generator
836847
String toExclude = null;
837-
if (schema.getDiscriminator()!=null) {
848+
if (schema.getDiscriminator() != null) {
838849
toExclude = schema.getDiscriminator().getPropertyName();
839850
}
840851

841-
example = packageName + ".models." + underscore(schema.getTitle())+"."+schema.getTitle()+"(";
852+
example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "(";
842853

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

853864
Map<String, Schema> properties = schema.getProperties();
854865
Set<String> propkeys = null;
855-
if (properties != null) propkeys = properties.keySet();
866+
if (properties != null)
867+
propkeys = properties.keySet();
856868
if (toExclude != null && reqs.contains(toExclude)) {
857869
reqs.remove(toExclude);
858870
}
@@ -879,7 +891,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
879891
}
880892
}
881893
}
882-
example +=")";
894+
example += ")";
883895
} else {
884896
LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue");
885897
}
@@ -908,44 +920,44 @@ public void setParameterExampleValue(CodegenParameter p) {
908920
}
909921

910922
if (type != null) {
911-
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
912-
if (example == null) {
913-
example = p.paramName + "_example";
914-
}
915-
example = "'" + escapeText(example) + "'";
916-
} else if ("Integer".equals(type) || "int".equals(type)) {
917-
if (example == null) {
918-
example = "56";
919-
}
920-
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
921-
if (example == null) {
922-
example = "3.4";
923-
}
924-
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
925-
if (example == null) {
926-
example = "True";
927-
}
928-
} else if ("file".equalsIgnoreCase(type)) {
929-
if (example == null) {
930-
example = "/path/to/file";
931-
}
932-
example = "'" + escapeText(example) + "'";
933-
} else if ("Date".equalsIgnoreCase(type)) {
934-
if (example == null) {
935-
example = "2013-10-20";
936-
}
937-
example = "'" + escapeText(example) + "'";
938-
} else if ("DateTime".equalsIgnoreCase(type)) {
939-
if (example == null) {
940-
example = "2013-10-20T19:20:30+01:00";
923+
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
924+
if (example == null) {
925+
example = p.paramName + "_example";
926+
}
927+
example = "'" + escapeText(example) + "'";
928+
} else if ("Integer".equals(type) || "int".equals(type)) {
929+
if (example == null) {
930+
example = "56";
931+
}
932+
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
933+
if (example == null) {
934+
example = "3.4";
935+
}
936+
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
937+
if (example == null) {
938+
example = "True";
939+
}
940+
} else if ("file".equalsIgnoreCase(type)) {
941+
if (example == null) {
942+
example = "/path/to/file";
943+
}
944+
example = "'" + escapeText(example) + "'";
945+
} else if ("Date".equalsIgnoreCase(type)) {
946+
if (example == null) {
947+
example = "2013-10-20";
948+
}
949+
example = "'" + escapeText(example) + "'";
950+
} else if ("DateTime".equalsIgnoreCase(type)) {
951+
if (example == null) {
952+
example = "2013-10-20T19:20:30+01:00";
953+
}
954+
example = "'" + escapeText(example) + "'";
955+
} else if (!languageSpecificPrimitives.contains(type)) {
956+
// type is a model class, e.g. User
957+
example = this.packageName + "." + type + "()";
958+
} else {
959+
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
941960
}
942-
example = "'" + escapeText(example) + "'";
943-
} else if (!languageSpecificPrimitives.contains(type)) {
944-
// type is a model class, e.g. User
945-
example = this.packageName + "." + type + "()";
946-
} else {
947-
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
948-
}
949961
}
950962

951963
if (example == null) {

0 commit comments

Comments
 (0)