@@ -683,12 +683,8 @@ public String toDefaultValue(Schema p) {
683
683
if (p .getDefault () != null ) {
684
684
if (Pattern .compile ("\r \n |\r |\n " ).matcher ((String ) p .getDefault ()).find ())
685
685
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 () + "\" " ;
689
686
else
690
- // convert to enum var name later in postProcessModels
691
- return (String ) p .getDefault ();
687
+ return "'" + ((String ) p .getDefault ()).replaceAll ("'" , "\' " ) + "'" ;
692
688
}
693
689
} else if (ModelUtils .isArraySchema (p )) {
694
690
if (p .getDefault () != null ) {
@@ -711,7 +707,7 @@ public String toExampleValue(Schema schema) {
711
707
712
708
private String toExampleValueRecursive (Schema schema , List <String > included_schemas , int indentation ) {
713
709
String indentation_string = "" ;
714
- for (int i = 0 ; i < indentation ; i ++) indentation_string += " " ;
710
+ for (int i = 0 ; i < indentation ; i ++) indentation_string += " " ;
715
711
String example = super .toExampleValue (schema );
716
712
717
713
if (ModelUtils .isNullType (schema ) && null != example ) {
@@ -720,11 +716,9 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
720
716
return "None" ;
721
717
}
722
718
// 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" ;
728
722
}
729
723
730
724
// correct "'"s into "'"s after toString()
@@ -734,14 +728,13 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
734
728
735
729
if (StringUtils .isNotBlank (example ) && !"null" .equals (example )) {
736
730
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 + "'" ;
739
732
}
740
733
return example ;
741
734
}
742
735
743
736
if (schema .getEnum () != null && !schema .getEnum ().isEmpty ()) {
744
- // Enum case:
737
+ // Enum case:
745
738
example = schema .getEnum ().get (0 ).toString ();
746
739
if (ModelUtils .isStringSchema (schema )) {
747
740
example = "'" + escapeText (example ) + "'" ;
@@ -751,7 +744,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
751
744
752
745
return example ;
753
746
} else if (null != schema .get$ref ()) {
754
- // $ref case:
747
+ // $ref case:
755
748
Map <String , Schema > allDefinitions = ModelUtils .getSchemas (this .openAPI );
756
749
String ref = ModelUtils .getSimpleRef (schema .get$ref ());
757
750
if (allDefinitions != null ) {
@@ -785,22 +778,13 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
785
778
example = "YQ==" ;
786
779
} else if (ModelUtils .isStringSchema (schema )) {
787
780
// 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
794
783
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 ;
802
786
example = "" ;
803
- for (int i = 0 ; i < len ; i ++) example += i ;
787
+ for (int i = 0 ; i < len ;i ++) example += i ;
804
788
} else if (ModelUtils .isIntegerSchema (schema )) {
805
789
if (schema .getMinimum () != null )
806
790
example = schema .getMinimum ().toString ();
@@ -818,7 +802,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
818
802
included_schemas .add (schema .getTitle ());
819
803
}
820
804
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 + "]" ;
822
806
} else if (ModelUtils .isMapSchema (schema )) {
823
807
if (StringUtils .isNotBlank (schema .getTitle ()) && !"null" .equals (schema .getTitle ())) {
824
808
included_schemas .add (schema .getTitle ());
@@ -833,7 +817,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
833
817
the_key = "'" + escapeText (the_key ) + "'" ;
834
818
}
835
819
}
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 + "}" ;
837
821
} else {
838
822
example = "{ }" ;
839
823
}
@@ -845,11 +829,11 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
845
829
846
830
// I remove any property that is a discriminator, since it is not well supported by the python generator
847
831
String toExclude = null ;
848
- if (schema .getDiscriminator () != null ) {
832
+ if (schema .getDiscriminator ()!= null ) {
849
833
toExclude = schema .getDiscriminator ().getPropertyName ();
850
834
}
851
835
852
- example = packageName + ".models." + underscore (schema .getTitle ()) + "." + schema .getTitle () + "(" ;
836
+ example = packageName + ".models." + underscore (schema .getTitle ())+ "." + schema .getTitle ()+ "(" ;
853
837
854
838
// if required only:
855
839
// List<String> reqs = schema.getRequired();
@@ -863,8 +847,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
863
847
864
848
Map <String , Schema > properties = schema .getProperties ();
865
849
Set <String > propkeys = null ;
866
- if (properties != null )
867
- propkeys = properties .keySet ();
850
+ if (properties != null ) propkeys = properties .keySet ();
868
851
if (toExclude != null && reqs .contains (toExclude )) {
869
852
reqs .remove (toExclude );
870
853
}
@@ -891,7 +874,7 @@ private String toExampleValueRecursive(Schema schema, List<String> included_sche
891
874
}
892
875
}
893
876
}
894
- example += ")" ;
877
+ example +=")" ;
895
878
} else {
896
879
LOGGER .warn ("Type " + schema .getType () + " not handled properly in toExampleValue" );
897
880
}
@@ -919,45 +902,43 @@ public void setParameterExampleValue(CodegenParameter p) {
919
902
type = p .dataType ;
920
903
}
921
904
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" ;
960
930
}
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" );
961
942
}
962
943
963
944
if (example == null ) {
0 commit comments