@@ -3147,8 +3147,12 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
3147
3147
* @param sc The Schema that may contain the discriminator
3148
3148
* @param discPropName The String that is the discriminator propertyName in the schema
3149
3149
*/
3150
- private CodegenProperty discriminatorFound (String composedSchemaName , Schema sc , String discPropName , OpenAPI openAPI ) {
3150
+ private CodegenProperty discriminatorFound (String composedSchemaName , Schema sc , String discPropName , OpenAPI openAPI , Set < Schema > visitedSchemas ) {
3151
3151
Schema refSchema = ModelUtils .getReferencedSchema (openAPI , sc );
3152
+ if (visitedSchemas .contains (refSchema )) {
3153
+ return null ;
3154
+ }
3155
+ visitedSchemas .add (refSchema );
3152
3156
if (refSchema .getProperties () != null && refSchema .getProperties ().get (discPropName ) != null ) {
3153
3157
Schema discSchema = (Schema ) refSchema .getProperties ().get (discPropName );
3154
3158
CodegenProperty cp = new CodegenProperty ();
@@ -3166,7 +3170,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
3166
3170
if (composedSchema .getAllOf () != null ) {
3167
3171
// If our discriminator is in one of the allOf schemas break when we find it
3168
3172
for (Schema allOf : composedSchema .getAllOf ()) {
3169
- CodegenProperty cp = discriminatorFound (composedSchemaName , allOf , discPropName , openAPI );
3173
+ CodegenProperty cp = discriminatorFound (composedSchemaName , allOf , discPropName , openAPI , visitedSchemas );
3170
3174
if (cp != null ) {
3171
3175
return cp ;
3172
3176
}
@@ -3177,7 +3181,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
3177
3181
CodegenProperty cp = new CodegenProperty ();
3178
3182
for (Schema oneOf : composedSchema .getOneOf ()) {
3179
3183
String modelName = ModelUtils .getSimpleRef (oneOf .get$ref ());
3180
- CodegenProperty thisCp = discriminatorFound (composedSchemaName , oneOf , discPropName , openAPI );
3184
+ CodegenProperty thisCp = discriminatorFound (composedSchemaName , oneOf , discPropName , openAPI , visitedSchemas );
3181
3185
if (thisCp == null ) {
3182
3186
LOGGER .warn (
3183
3187
"'{}' defines discriminator '{}', but the referenced OneOf schema '{}' is missing {}" ,
@@ -3200,7 +3204,7 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc,
3200
3204
CodegenProperty cp = new CodegenProperty ();
3201
3205
for (Schema anyOf : composedSchema .getAnyOf ()) {
3202
3206
String modelName = ModelUtils .getSimpleRef (anyOf .get$ref ());
3203
- CodegenProperty thisCp = discriminatorFound (composedSchemaName , anyOf , discPropName , openAPI );
3207
+ CodegenProperty thisCp = discriminatorFound (composedSchemaName , anyOf , discPropName , openAPI , visitedSchemas );
3204
3208
if (thisCp == null ) {
3205
3209
LOGGER .warn (
3206
3210
"'{}' defines discriminator '{}', but the referenced AnyOf schema '{}' is missing {}" ,
@@ -3355,7 +3359,8 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
3355
3359
"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" ,
3356
3360
composedSchemaName );
3357
3361
}
3358
- CodegenProperty df = discriminatorFound (composedSchemaName , sc , discPropName , openAPI );
3362
+ HashSet <Schema > visitedSchemas = new HashSet <>();
3363
+ CodegenProperty df = discriminatorFound (composedSchemaName , sc , discPropName , openAPI , visitedSchemas );
3359
3364
String modelName = ModelUtils .getSimpleRef (ref );
3360
3365
if (df == null || !df .isString || df .required != true ) {
3361
3366
String msgSuffix = "" ;
0 commit comments