1616
1717package org .openapitools .codegen .languages ;
1818
19- import com .google .common .collect .Sets ;
2019import io .swagger .v3 .core .util .Json ;
2120import io .swagger .v3 .oas .models .media .*;
2221import io .swagger .v3 .oas .models .media .ArraySchema ;
@@ -880,7 +879,7 @@ private String ensureQuotes(String in) {
880879
881880 public String toExampleValue (Schema schema , Object objExample ) {
882881 String modelName = getModelName (schema );
883- return toExampleValueRecursive (modelName , schema , objExample , 1 , "" , 0 , Sets . newHashSet () );
882+ return toExampleValueRecursive (modelName , schema , objExample , 1 , "" , 0 );
884883 }
885884
886885 private Boolean simpleStringSchema (Schema schema ) {
@@ -926,12 +925,9 @@ private MappedModel getDiscriminatorMappedModel(CodegenDiscriminator disc) {
926925 * ModelName( line 0
927926 * some_property='some_property_example' line 1
928927 * ) line 2
929- * @param seenSchemas This set contains all the schemas passed into the recursive function. It is used to check
930- * if a schema was already passed into the function and breaks the infinite recursive loop. The
931- * only schemas that are not added are ones that contain $ref != null
932928 * @return the string example
933929 */
934- private String toExampleValueRecursive (String modelName , Schema schema , Object objExample , int indentationLevel , String prefix , Integer exampleLine , Set < Schema > seenSchemas ) {
930+ private String toExampleValueRecursive (String modelName , Schema schema , Object objExample , int indentationLevel , String prefix , Integer exampleLine ) {
935931 final String indentionConst = " " ;
936932 String currentIndentation = "" ;
937933 String closingIndentation = "" ;
@@ -955,27 +951,6 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
955951 if (objExample != null ) {
956952 example = objExample .toString ();
957953 }
958- // checks if the current schema has already been passed in. If so, breaks the current recursive pass
959- if (seenSchemas .contains (schema )){
960- if (modelName != null ) {
961- return fullPrefix + modelName + closeChars ;
962- } else {
963- // this is a recursive schema
964- // need to add a reasonable example to avoid
965- // infinite recursion
966- if (ModelUtils .isNullable (schema )) {
967- // if the schema is nullable, then 'None' is a valid value
968- return fullPrefix + "None" + closeChars ;
969- } else if (ModelUtils .isArraySchema (schema )) {
970- // the schema is an array, add an empty array
971- return fullPrefix + "[]" + closeChars ;
972- } else {
973- // the schema is an object, make an empty object
974- return fullPrefix + "{}" + closeChars ;
975- }
976- }
977- }
978-
979954 if (null != schema .get$ref ()) {
980955 Map <String , Schema > allDefinitions = ModelUtils .getSchemas (this .openAPI );
981956 String ref = ModelUtils .getSimpleRef (schema .get$ref ());
@@ -985,7 +960,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
985960 return fullPrefix + "None" + closeChars ;
986961 }
987962 String refModelName = getModelName (schema );
988- return toExampleValueRecursive (refModelName , refSchema , objExample , indentationLevel , prefix , exampleLine , seenSchemas );
963+ return toExampleValueRecursive (refModelName , refSchema , objExample , indentationLevel , prefix , exampleLine );
989964 } else if (ModelUtils .isNullType (schema ) || isAnyTypeSchema (schema )) {
990965 // The 'null' type is allowed in OAS 3.1 and above. It is not supported by OAS 3.0.x,
991966 // though this tooling supports it.
@@ -1083,8 +1058,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
10831058 ArraySchema arrayschema = (ArraySchema ) schema ;
10841059 Schema itemSchema = arrayschema .getItems ();
10851060 String itemModelName = getModelName (itemSchema );
1086- seenSchemas .add (schema );
1087- example = fullPrefix + "[" + "\n " + toExampleValueRecursive (itemModelName , itemSchema , objExample , indentationLevel + 1 , "" , exampleLine + 1 , seenSchemas ) + ",\n " + closingIndentation + "]" + closeChars ;
1061+ example = fullPrefix + "[" + "\n " + toExampleValueRecursive (itemModelName , itemSchema , objExample , indentationLevel + 1 , "" , exampleLine + 1 ) + ",\n " + closingIndentation + "]" + closeChars ;
10881062 return example ;
10891063 } else if (ModelUtils .isMapSchema (schema )) {
10901064 if (modelName == null ) {
@@ -1106,8 +1080,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
11061080 addPropPrefix = ensureQuotes (key ) + ": " ;
11071081 }
11081082 String addPropsModelName = getModelName (addPropsSchema );
1109- seenSchemas .add (schema );
1110- example = fullPrefix + "\n " + toExampleValueRecursive (addPropsModelName , addPropsSchema , addPropsExample , indentationLevel + 1 , addPropPrefix , exampleLine + 1 , seenSchemas ) + ",\n " + closingIndentation + closeChars ;
1083+ example = fullPrefix + "\n " + toExampleValueRecursive (addPropsModelName , addPropsSchema , addPropsExample , indentationLevel + 1 , addPropPrefix , exampleLine + 1 ) + ",\n " + closingIndentation + closeChars ;
11111084 } else {
11121085 example = fullPrefix + closeChars ;
11131086 }
@@ -1130,12 +1103,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
11301103 return fullPrefix + closeChars ;
11311104 }
11321105 }
1133- // Adds schema to seenSchemas before running example model function. romoves schema after running
1134- // the function. It also doesnt keep track of any schemas within the ObjectModel.
1135- seenSchemas .add (schema );
1136- String exampleForObjectModel = exampleForObjectModel (schema , fullPrefix , closeChars , null , indentationLevel , exampleLine , closingIndentation , seenSchemas );
1137- seenSchemas .remove (schema );
1138- return exampleForObjectModel ;
1106+ return exampleForObjectModel (schema , fullPrefix , closeChars , null , indentationLevel , exampleLine , closingIndentation );
11391107 } else if (ModelUtils .isComposedSchema (schema )) {
11401108 // TODO add examples for composed schema models without discriminators
11411109
@@ -1149,12 +1117,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
11491117 CodegenProperty cp = new CodegenProperty ();
11501118 cp .setName (disc .getPropertyName ());
11511119 cp .setExample (discPropNameValue );
1152- // Adds schema to seenSchemas before running example model function. romoves schema after running
1153- // the function. It also doesnt keep track of any schemas within the ObjectModel.
1154- seenSchemas .add (modelSchema );
1155- String exampleForObjectModel = exampleForObjectModel (modelSchema , fullPrefix , closeChars , cp , indentationLevel , exampleLine , closingIndentation , seenSchemas );
1156- seenSchemas .remove (modelSchema );
1157- return exampleForObjectModel ;
1120+ return exampleForObjectModel (modelSchema , fullPrefix , closeChars , cp , indentationLevel , exampleLine , closingIndentation );
11581121 } else {
11591122 return fullPrefix + closeChars ;
11601123 }
@@ -1167,7 +1130,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o
11671130 return example ;
11681131 }
11691132
1170- private String exampleForObjectModel (Schema schema , String fullPrefix , String closeChars , CodegenProperty discProp , int indentationLevel , int exampleLine , String closingIndentation , Set < Schema > seenSchemas ) {
1133+ private String exampleForObjectModel (Schema schema , String fullPrefix , String closeChars , CodegenProperty discProp , int indentationLevel , int exampleLine , String closingIndentation ) {
11711134 Map <String , Schema > requiredAndOptionalProps = schema .getProperties ();
11721135 if (requiredAndOptionalProps == null || requiredAndOptionalProps .isEmpty ()) {
11731136 return fullPrefix + closeChars ;
@@ -1187,7 +1150,7 @@ private String exampleForObjectModel(Schema schema, String fullPrefix, String cl
11871150 propModelName = getModelName (propSchema );
11881151 propExample = exampleFromStringOrArraySchema (propSchema , null , propName );
11891152 }
1190- example += toExampleValueRecursive (propModelName , propSchema , propExample , indentationLevel + 1 , propName + "=" , exampleLine + 1 , seenSchemas ) + ",\n " ;
1153+ example += toExampleValueRecursive (propModelName , propSchema , propExample , indentationLevel + 1 , propName + "=" , exampleLine + 1 ) + ",\n " ;
11911154 }
11921155 // TODO handle additionalProperties also
11931156 example += closingIndentation + closeChars ;
0 commit comments