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 fddac6cec953..595f868a51b3 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 @@ -3828,21 +3828,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch discriminator.setIsEnum(isEnum); discriminator.setMapping(sourceDiscriminator.getMapping()); - List uniqueDescendants = new ArrayList<>(); - if (sourceDiscriminator.getMapping() != null && !sourceDiscriminator.getMapping().isEmpty()) { - for (Entry e : sourceDiscriminator.getMapping().entrySet()) { - String name; - if (e.getValue().indexOf('/') >= 0) { - name = ModelUtils.getSimpleRef(e.getValue()); - if (ModelUtils.getSchema(openAPI, name) == null) { - once(LOGGER).error("Failed to lookup the schema '{}' when processing the discriminator mapping of oneOf/anyOf. Please check to ensure it's defined properly.", name); - } - } else { - name = e.getValue(); - } - uniqueDescendants.add(new MappedModel(e.getKey(), toModelName(name), name, true)); - } - } + List uniqueDescendants = adjustModelNames(getUniqueDescendants(openAPI, sourceDiscriminator.getDiscriminator())); boolean legacyUseCase = (this.getLegacyDiscriminatorBehavior() && uniqueDescendants.isEmpty()); if (!this.getLegacyDiscriminatorBehavior() || legacyUseCase) { @@ -3906,7 +3892,7 @@ protected String getDiscriminatorPropertyType(Schema schema, String discriminato *

* @param schemas the list of schemas to compare. * - * @Return the comman type + * @return the common type */ protected String getCommonSchemaType(List schemas) { switch (schemas.size()) { @@ -3936,16 +3922,6 @@ protected String getCommonSchemaType(List schemas) { break; } -// boolean allRef = schemas.stream().allMatch(s -> s.get$ref() != null); -// if (allRef) { -// Set modelNames = schemas.stream() -// .map(s -> toModelName(ModelUtils.getSimpleRef(s.get$ref()))) -// .filter(Objects::nonNull) -// .collect(Collectors.toSet()); -// if (modelNames.size() == 1) { -// return modelNames.iterator().next(); -// } -// } schemas = schemas.stream().map(s -> ModelUtils.getReferencedSchema(openAPI, s)).collect(Collectors.toList()); return getCommonTypeMapping(schemas); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java index 9b3a3918504d..bb45abdeb0e7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.models.media.Discriminator; import io.swagger.v3.oas.models.media.Schema; import org.jspecify.annotations.Nullable; +import org.openapitools.codegen.CodegenDiscriminator.MappedModel; import org.openapitools.codegen.CodegenProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +21,8 @@ public class DiscriminatorUtils { private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatorUtils.class); + private static final String FAILED_TO_FIND_SCHEMA = + "Failed to lookup the schema '{}' when processing the discriminator mapping oneOf/anyOf. Please check to ensure it's defined properly."; private static final String CONFLICTING_DISCRIMINATOR_NAMES = "The alternative schemas have conflicting discriminator property names. The schemas must have the same property name, but found {}"; private static final String CONFLICTING_DISCRIMINATOR_TYPES = @@ -185,6 +188,35 @@ public static DiscriminatorData recursiveGetDiscriminator( return null; } + /** + * Returns the discriminator descendants defined explicitly by the source discriminator mapping. + * Each mapping entry is converted into a {@link MappedModel}, using the mapped name when a + * discriminator value is provided, or resolving the referenced model name when the mapping points + * to a schema reference. + * + * @param openAPI the OpenAPI specification + * @param sourceDiscriminator the discriminator containing explicit mappings + * @return the list of unique descendant models defined by the discriminator mapping + */ + public static List getUniqueDescendants(OpenAPI openAPI, Discriminator sourceDiscriminator) { + List uniqueDescendants = new ArrayList<>(); + if (sourceDiscriminator.getMapping() != null && !sourceDiscriminator.getMapping().isEmpty()) { + for (Map.Entry entry : sourceDiscriminator.getMapping().entrySet()) { + String name; + if (entry.getValue().indexOf('/') >= 0) { + name = ModelUtils.getSimpleRef(entry.getValue()); + if (ModelUtils.getSchema(openAPI, name) == null) { + once(LOGGER).error(FAILED_TO_FIND_SCHEMA, name); + } + } else { + name = entry.getValue(); + } + uniqueDescendants.add(new MappedModel(entry.getKey(), name, name, true)); + } + } + return uniqueDescendants; + } + /** * Get the value of the vendor extension {@code x-discriminator-value} from the schema, if present. * @param schema the schema to check for the vendor extension