diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 7f337d01f772..e48a124aa7d7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -3216,7 +3216,6 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper */ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String discPropName, Set visitedSchemas) { Schema refSchema = ModelUtils.getReferencedSchema(openAPI, sc); - ModelUtils.getSimpleRef(sc.get$ref()); String schemaName = Optional.ofNullable(composedSchemaName) .or(() -> Optional.ofNullable(refSchema.getName())) .or(() -> Optional.ofNullable(sc.get$ref()).map(ModelUtils::getSimpleRef)) @@ -3258,7 +3257,9 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, for (Object oneOf : composedSchema.getOneOf()) { Schema oneOfSchema = (Schema) oneOf; String modelName = ModelUtils.getSimpleRef((oneOfSchema).get$ref()); - CodegenProperty thisCp = discriminatorFound(oneOfSchema.getName(), oneOfSchema, discPropName, visitedSchemas); + // Must use a copied set as the oneOf schemas can point to the same discriminator. + Set visitedSchemasCopy = new TreeSet<>(visitedSchemas); + CodegenProperty thisCp = discriminatorFound(oneOfSchema.getName(), oneOfSchema, discPropName, visitedSchemasCopy); if (thisCp == null) { once(LOGGER).warn( "'{}' defines discriminator '{}', but the referenced OneOf schema '{}' is missing {}", @@ -3282,7 +3283,9 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, for (Object anyOf : composedSchema.getAnyOf()) { Schema anyOfSchema = (Schema) anyOf; String modelName = ModelUtils.getSimpleRef(anyOfSchema.get$ref()); - CodegenProperty thisCp = discriminatorFound(anyOfSchema.getName(), anyOfSchema, discPropName, visitedSchemas); + // Must use a copied set as the anyOf schemas can point to the same discriminator. + Set visitedSchemasCopy = new TreeSet<>(visitedSchemas); + CodegenProperty thisCp = discriminatorFound(anyOfSchema.getName(), anyOfSchema, discPropName, visitedSchemasCopy); if (thisCp == null) { once(LOGGER).warn( "'{}' defines discriminator '{}', but the referenced AnyOf schema '{}' is missing {}", diff --git a/modules/openapi-generator/src/test/resources/3_0/enum_discriminator_inheritance.yaml b/modules/openapi-generator/src/test/resources/3_0/enum_discriminator_inheritance.yaml index e211a96edcac..db89a48eeb09 100644 --- a/modules/openapi-generator/src/test/resources/3_0/enum_discriminator_inheritance.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/enum_discriminator_inheritance.yaml @@ -5,6 +5,7 @@ info: paths: "/animal": get: + operationId: animalGet responses: '200': description: OK