@@ -3189,8 +3189,16 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
31893189 * @param composedSchemaName The name of the sc Schema
31903190 * @param sc The Schema that may contain the discriminator
31913191 * @param discPropName The String that is the discriminator propertyName in the schema
3192+ * @param visitedSchemas A set of visited schema names
3193+ *
31923194 */
3193- private CodegenProperty discriminatorFound (String composedSchemaName , Schema sc , String discPropName , OpenAPI openAPI ) {
3195+ private CodegenProperty discriminatorFound (String composedSchemaName , Schema sc , String discPropName , Set <String > visitedSchemas ) {
3196+ if (visitedSchemas .contains (composedSchemaName )) { // recursive schema definition found
3197+ return null ;
3198+ } else {
3199+ visitedSchemas .add (composedSchemaName );
3200+ }
3201+
31943202 Schema refSchema = ModelUtils .getReferencedSchema (openAPI , sc );
31953203 if (refSchema .getProperties () != null && refSchema .getProperties ().get (discPropName ) != null ) {
31963204 Schema discSchema = (Schema ) refSchema .getProperties ().get (discPropName );
@@ -3209,7 +3217,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
32093217 if (composedSchema .getAllOf () != null ) {
32103218 // If our discriminator is in one of the allOf schemas break when we find it
32113219 for (Schema allOf : composedSchema .getAllOf ()) {
3212- CodegenProperty cp = discriminatorFound (composedSchemaName , allOf , discPropName , openAPI );
3220+ CodegenProperty cp = discriminatorFound (composedSchemaName , allOf , discPropName , visitedSchemas );
32133221 if (cp != null ) {
32143222 return cp ;
32153223 }
@@ -3220,7 +3228,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
32203228 CodegenProperty cp = new CodegenProperty ();
32213229 for (Schema oneOf : composedSchema .getOneOf ()) {
32223230 String modelName = ModelUtils .getSimpleRef (oneOf .get$ref ());
3223- CodegenProperty thisCp = discriminatorFound (composedSchemaName , oneOf , discPropName , openAPI );
3231+ CodegenProperty thisCp = discriminatorFound (composedSchemaName , oneOf , discPropName , visitedSchemas );
32243232 if (thisCp == null ) {
32253233 LOGGER .warn (
32263234 "'{}' defines discriminator '{}', but the referenced OneOf schema '{}' is missing {}" ,
@@ -3243,7 +3251,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
32433251 CodegenProperty cp = new CodegenProperty ();
32443252 for (Schema anyOf : composedSchema .getAnyOf ()) {
32453253 String modelName = ModelUtils .getSimpleRef (anyOf .get$ref ());
3246- CodegenProperty thisCp = discriminatorFound (composedSchemaName , anyOf , discPropName , openAPI );
3254+ CodegenProperty thisCp = discriminatorFound (composedSchemaName , anyOf , discPropName , visitedSchemas );
32473255 if (thisCp == null ) {
32483256 LOGGER .warn (
32493257 "'{}' defines discriminator '{}', but the referenced AnyOf schema '{}' is missing {}" ,
@@ -3268,12 +3276,11 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
32683276
32693277 /**
32703278 * Recursively look in Schema sc for the discriminator and return it
3271- * Schema sc location
3272- * OpenAPI openAPI the spec where we are searching for the discriminator
32733279 *
32743280 * @param sc The Schema that may contain the discriminator
3281+ * @param visitedSchemas An array list of visited schemas
32753282 */
3276- private Discriminator recursiveGetDiscriminator (Schema sc , OpenAPI openAPI ) {
3283+ private Discriminator recursiveGetDiscriminator (Schema sc , ArrayList < Schema > visitedSchemas ) {
32773284 Schema refSchema = ModelUtils .getReferencedSchema (openAPI , sc );
32783285 Discriminator foundDisc = refSchema .getDiscriminator ();
32793286 if (foundDisc != null ) {
@@ -3283,13 +3290,21 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
32833290 if (this .getLegacyDiscriminatorBehavior ()) {
32843291 return null ;
32853292 }
3293+
3294+ for (Schema s : visitedSchemas ) {
3295+ if (s == refSchema ) {
3296+ return null ;
3297+ }
3298+ }
3299+ visitedSchemas .add (refSchema );
3300+
32863301 Discriminator disc = new Discriminator ();
32873302 if (ModelUtils .isComposedSchema (refSchema )) {
32883303 ComposedSchema composedSchema = (ComposedSchema ) refSchema ;
32893304 if (composedSchema .getAllOf () != null ) {
32903305 // If our discriminator is in one of the allOf schemas break when we find it
32913306 for (Schema allOf : composedSchema .getAllOf ()) {
3292- foundDisc = recursiveGetDiscriminator (allOf , openAPI );
3307+ foundDisc = recursiveGetDiscriminator (allOf , visitedSchemas );
32933308 if (foundDisc != null ) {
32943309 disc .setPropertyName (foundDisc .getPropertyName ());
32953310 disc .setMapping (foundDisc .getMapping ());
@@ -3308,7 +3323,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
33083323 hasNullTypeCnt ++;
33093324 continue ;
33103325 }
3311- foundDisc = recursiveGetDiscriminator (oneOf , openAPI );
3326+ foundDisc = recursiveGetDiscriminator (oneOf , visitedSchemas );
33123327 if (foundDisc != null ) {
33133328 discriminatorsPropNames .add (foundDisc .getPropertyName ());
33143329 hasDiscriminatorCnt ++;
@@ -3337,7 +3352,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) {
33373352 hasNullTypeCnt ++;
33383353 continue ;
33393354 }
3340- foundDisc = recursiveGetDiscriminator (anyOf , openAPI );
3355+ foundDisc = recursiveGetDiscriminator (anyOf , visitedSchemas );
33413356 if (foundDisc != null ) {
33423357 discriminatorsPropNames .add (foundDisc .getPropertyName ());
33433358 hasDiscriminatorCnt ++;
@@ -3398,7 +3413,7 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
33983413 "Invalid inline schema defined in oneOf/anyOf in '{}'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the oneOf/anyOf schemas must use $ref. Change this inline definition to a $ref definition" ,
33993414 composedSchemaName );
34003415 }
3401- CodegenProperty df = discriminatorFound (composedSchemaName , sc , discPropName , openAPI );
3416+ CodegenProperty df = discriminatorFound (composedSchemaName , sc , discPropName , new TreeSet < String >() );
34023417 String modelName = ModelUtils .getSimpleRef (ref );
34033418 if (df == null || !df .isString || df .required != true ) {
34043419 String msgSuffix = "" ;
@@ -3494,7 +3509,7 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
34943509 }
34953510
34963511 protected CodegenDiscriminator createDiscriminator (String schemaName , Schema schema , OpenAPI openAPI ) {
3497- Discriminator sourceDiscriminator = recursiveGetDiscriminator (schema , openAPI );
3512+ Discriminator sourceDiscriminator = recursiveGetDiscriminator (schema , new ArrayList < Schema >() );
34983513 if (sourceDiscriminator == null ) {
34993514 return null ;
35003515 }
0 commit comments