Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3828,21 +3828,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
discriminator.setIsEnum(isEnum);

discriminator.setMapping(sourceDiscriminator.getMapping());
List<MappedModel> uniqueDescendants = new ArrayList<>();
if (sourceDiscriminator.getMapping() != null && !sourceDiscriminator.getMapping().isEmpty()) {
for (Entry<String, String> 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<MappedModel> uniqueDescendants = adjustModelNames(getUniqueDescendants(openAPI, sourceDiscriminator.getDiscriminator()));

boolean legacyUseCase = (this.getLegacyDiscriminatorBehavior() && uniqueDescendants.isEmpty());
if (!this.getLegacyDiscriminatorBehavior() || legacyUseCase) {
Expand Down Expand Up @@ -3906,7 +3892,7 @@ protected String getDiscriminatorPropertyType(Schema schema, String discriminato
* <p>
* @param schemas the list of schemas to compare.
*
* @Return the comman type
* @return the common type
*/
protected String getCommonSchemaType(List<Schema> schemas) {
switch (schemas.size()) {
Expand Down Expand Up @@ -3936,16 +3922,6 @@ protected String getCommonSchemaType(List<Schema> schemas) {
break;
}

// boolean allRef = schemas.stream().allMatch(s -> s.get$ref() != null);
// if (allRef) {
// Set<String> 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();
// }
// }
Comment thread
wing328 marked this conversation as resolved.
schemas = schemas.stream().map(s -> ModelUtils.getReferencedSchema(openAPI, s)).collect(Collectors.toList());
return getCommonTypeMapping(schemas);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,8 @@ public class DiscriminatorUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatorUtils.class);

private static final String FAILED_TO_FIND_SCHEMA =
Comment thread
Mattias-Sehlstedt marked this conversation as resolved.
"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 =
Expand Down Expand Up @@ -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<MappedModel> getUniqueDescendants(OpenAPI openAPI, Discriminator sourceDiscriminator) {
List<MappedModel> uniqueDescendants = new ArrayList<>();
if (sourceDiscriminator.getMapping() != null && !sourceDiscriminator.getMapping().isEmpty()) {
for (Map.Entry<String, String> 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
Expand Down
Loading