Skip to content

Commit f91a5f7

Browse files
authored
Undo PR #6451 (#6514)
* Revert "Minor enhancement to Python client generator's code format (#6510)" This reverts commit 3ddc783. * Revert "[Python][Client] Fix delimiter collision issue #5981 (#6451)" This reverts commit 6783b90.
1 parent 3ddc783 commit f91a5f7

File tree

4 files changed

+99
-200
lines changed

4 files changed

+99
-200
lines changed

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

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,8 @@ public String toDefaultValue(Schema p) {
683683
if (p.getDefault() != null) {
684684
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find())
685685
return "'''" + p.getDefault() + "'''";
686-
else if (p.getEnum() == null)
687-
// wrap using double quotes to avoid the need to escape any embedded single quotes
688-
return "\"" + p.getDefault() + "\"";
689686
else
690-
// convert to enum var name later in postProcessModels
691-
return (String) p.getDefault();
687+
return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'";
692688
}
693689
} else if (ModelUtils.isArraySchema(p)) {
694690
if (p.getDefault() != null) {
@@ -711,7 +707,7 @@ public String toExampleValue(Schema schema) {
711707

712708
private String toExampleValueRecursive(Schema schema, List<String> included_schemas, int indentation) {
713709
String indentation_string = "";
714-
for (int i = 0; i < indentation; i++) indentation_string += " ";
710+
for (int i=0 ; i< indentation ; i++) indentation_string += " ";
715711
String example = super.toExampleValue(schema);
716712

717713
if (ModelUtils.isNullType(schema) && null != example) {
@@ -720,11 +716,9 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
720716
return "None";
721717
}
722718
// 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))
725-
example = "False";
726-
else
727-
example = "True";
719+
if (ModelUtils.isBooleanSchema(schema) && null!=example) {
720+
if ("false".equalsIgnoreCase(example)) example = "False";
721+
else example = "True";
728722
}
729723

730724
// correct "&#39;"s into "'"s after toString()
@@ -734,14 +728,13 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
734728

735729
if (StringUtils.isNotBlank(example) && !"null".equals(example)) {
736730
if (ModelUtils.isStringSchema(schema)) {
737-
// wrap using double quotes to avoid the need to escape any embedded single quotes
738-
example = "\"" + example + "\"";
731+
example = "'" + example + "'";
739732
}
740733
return example;
741734
}
742735

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

752745
return example;
753746
} else if (null != schema.get$ref()) {
754-
// $ref case:
747+
// $ref case:
755748
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
756749
String ref = ModelUtils.getSimpleRef(schema.get$ref());
757750
if (allDefinitions != null) {
@@ -785,22 +778,13 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
785778
example = "YQ==";
786779
} else if (ModelUtils.isStringSchema(schema)) {
787780
// a BigDecimal:
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-
781+
if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";}
782+
if (StringUtils.isNotBlank(schema.getPattern())) return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp
794783
int len = 0;
795-
796-
if (null != schema.getMinLength())
797-
len = schema.getMinLength().intValue();
798-
799-
if (len < 1)
800-
len = 1;
801-
784+
if (null != schema.getMinLength()) len = schema.getMinLength().intValue();
785+
if (len < 1) len = 1;
802786
example = "";
803-
for (int i = 0; i < len; i++) example += i;
787+
for (int i=0;i<len;i++) example += i;
804788
} else if (ModelUtils.isIntegerSchema(schema)) {
805789
if (schema.getMinimum() != null)
806790
example = schema.getMinimum().toString();
@@ -818,7 +802,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
818802
included_schemas.add(schema.getTitle());
819803
}
820804
ArraySchema arrayschema = (ArraySchema) schema;
821-
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation + 1) + "\n" + indentation_string + "]";
805+
example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation+1) + "\n" + indentation_string + "]";
822806
} else if (ModelUtils.isMapSchema(schema)) {
823807
if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) {
824808
included_schemas.add(schema.getTitle());
@@ -833,7 +817,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
833817
the_key = "'" + escapeText(the_key) + "'";
834818
}
835819
}
836-
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation + 1) + "\n" + indentation_string + "}";
820+
example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation+1) + "\n" + indentation_string + "}";
837821
} else {
838822
example = "{ }";
839823
}
@@ -845,11 +829,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
845829

846830
// I remove any property that is a discriminator, since it is not well supported by the python generator
847831
String toExclude = null;
848-
if (schema.getDiscriminator() != null) {
832+
if (schema.getDiscriminator()!=null) {
849833
toExclude = schema.getDiscriminator().getPropertyName();
850834
}
851835

852-
example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "(";
836+
example = packageName + ".models." + underscore(schema.getTitle())+"."+schema.getTitle()+"(";
853837

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

864848
Map<String, Schema> properties = schema.getProperties();
865849
Set<String> propkeys = null;
866-
if (properties != null)
867-
propkeys = properties.keySet();
850+
if (properties != null) propkeys = properties.keySet();
868851
if (toExclude != null && reqs.contains(toExclude)) {
869852
reqs.remove(toExclude);
870853
}
@@ -891,7 +874,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
891874
}
892875
}
893876
}
894-
example += ")";
877+
example +=")";
895878
} else {
896879
LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue");
897880
}
@@ -919,45 +902,43 @@ public void setParameterExampleValue(CodegenParameter p) {
919902
type = p.dataType;
920903
}
921904

922-
if (type != null) {
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");
905+
if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) {
906+
if (example == null) {
907+
example = p.paramName + "_example";
908+
}
909+
example = "'" + escapeText(example) + "'";
910+
} else if ("Integer".equals(type) || "int".equals(type)) {
911+
if (example == null) {
912+
example = "56";
913+
}
914+
} else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) {
915+
if (example == null) {
916+
example = "3.4";
917+
}
918+
} else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) {
919+
if (example == null) {
920+
example = "True";
921+
}
922+
} else if ("file".equalsIgnoreCase(type)) {
923+
if (example == null) {
924+
example = "/path/to/file";
925+
}
926+
example = "'" + escapeText(example) + "'";
927+
} else if ("Date".equalsIgnoreCase(type)) {
928+
if (example == null) {
929+
example = "2013-10-20";
960930
}
931+
example = "'" + escapeText(example) + "'";
932+
} else if ("DateTime".equalsIgnoreCase(type)) {
933+
if (example == null) {
934+
example = "2013-10-20T19:20:30+01:00";
935+
}
936+
example = "'" + escapeText(example) + "'";
937+
} else if (!languageSpecificPrimitives.contains(type)) {
938+
// type is a model class, e.g. User
939+
example = this.packageName + "." + type + "()";
940+
} else {
941+
LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue");
961942
}
962943

963944
if (example == null) {

0 commit comments

Comments
 (0)