Skip to content

Commit b4273f4

Browse files
refactor: move discriminator MappedModel calculation to DiscriminatorUtils (#25434)
1 parent 5579e3a commit b4273f4

2 files changed

Lines changed: 151 additions & 135 deletions

File tree

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java‎

Lines changed: 3 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,114 +3674,6 @@ private DiscriminatorData recursiveGetDiscriminator(Schema sc, ArrayList<Schema>
36743674
return DiscriminatorUtils.recursiveGetDiscriminator(openAPI, this.getLegacyDiscriminatorBehavior(), sc, visitedSchemas);
36753675
}
36763676

3677-
/**
3678-
* This function is only used for composed schemas which have a discriminator
3679-
* Process oneOf and anyOf models in a composed schema and adds them into
3680-
* a list if the oneOf and anyOf models contain
3681-
* the required discriminator. If they don't contain the required
3682-
* discriminator or the discriminator is the wrong type then an error is
3683-
* thrown
3684-
*
3685-
* @param composedSchemaName The String model name of the composed schema where we are setting the discriminator map
3686-
* @param discPropName The String that is the discriminator propertyName in the schema
3687-
* @param c The ComposedSchema that contains the discriminator and oneOf/anyOf schemas
3688-
* @return the list of oneOf and anyOf MappedModel that need to be added to the discriminator map
3689-
*/
3690-
protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName, String discPropName, Schema c) {
3691-
ArrayList<List<Schema>> listOLists = new ArrayList<>();
3692-
listOLists.add(c.getOneOf());
3693-
listOLists.add(c.getAnyOf());
3694-
List<MappedModel> descendentSchemas = new ArrayList<>();
3695-
for (List<Schema> schemaList : listOLists) {
3696-
if (schemaList == null) {
3697-
continue;
3698-
}
3699-
for (Schema sc : schemaList) {
3700-
if (ModelUtils.isNullType(sc)) {
3701-
continue;
3702-
}
3703-
String ref = sc.get$ref();
3704-
if (ref == null) {
3705-
// for schemas with no ref, it is not possible to build the discriminator map
3706-
// because ref is how we get the model name
3707-
// we only hit this use case for a schema with inline composed schemas, and one of those
3708-
// schemas also has inline composed schemas
3709-
// Note: if it is only inline one level, then the inline model resolver will move it into its own
3710-
// schema and make it a $ref schema in the oneOf/anyOf location
3711-
once(LOGGER).warn(
3712-
"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",
3713-
composedSchemaName);
3714-
}
3715-
CodegenProperty df = DiscriminatorUtils.discriminatorFound(openAPI, composedSchemaName, sc, discPropName, new TreeSet<String>());
3716-
String modelName = ModelUtils.getSimpleRef(ref);
3717-
if (df == null || !df.isString || !df.required) {
3718-
once(LOGGER).warn(getDiscriminatorSchemaError(df, discPropName, modelName, composedSchemaName));
3719-
}
3720-
MappedModel mm = new MappedModel(modelName, modelName, modelName, false);
3721-
descendentSchemas.add(mm);
3722-
Schema cs = ModelUtils.getSchema(openAPI, modelName);
3723-
if (cs == null) { // cannot lookup the model based on the name
3724-
once(LOGGER).error("Failed to lookup the schema '{}' when processing oneOf/anyOf. Please check to ensure it's defined properly.", modelName);
3725-
} else {
3726-
discriminatorVendorExtensionValue(cs)
3727-
.ifPresent(discriminatorValue -> descendentSchemas.add(new MappedModel(discriminatorValue, modelName, modelName, true)));
3728-
}
3729-
}
3730-
}
3731-
return descendentSchemas;
3732-
}
3733-
3734-
protected List<MappedModel> getAllOfDescendants(String thisSchemaName) {
3735-
ArrayList<String> queue = new ArrayList();
3736-
List<MappedModel> descendentSchemas = new ArrayList();
3737-
Map<String, Schema> schemas = ModelUtils.getSchemas(openAPI);
3738-
String currentSchemaName = thisSchemaName;
3739-
Set<String> keys = schemas.keySet();
3740-
3741-
int count = 0;
3742-
// hack: avoid infinite loop on potential self-references in event our checks fail.
3743-
while (100000 > count++) {
3744-
for (String childName : keys) {
3745-
if (childName.equals(thisSchemaName)) {
3746-
continue;
3747-
}
3748-
Schema child = schemas.get(childName);
3749-
if (ModelUtils.isComposedSchema(child)) {
3750-
List<Schema> parents = child.getAllOf();
3751-
if (parents != null) {
3752-
for (Schema parent : parents) {
3753-
String ref = parent.get$ref();
3754-
if (ref == null) {
3755-
// for schemas with no ref, it is not possible to build the discriminator map
3756-
// because ref is how we get the model name
3757-
// we hit this use case when an allOf composed schema contains an inline schema
3758-
continue;
3759-
}
3760-
String parentName = ModelUtils.getSimpleRef(ref);
3761-
if (parentName != null && parentName.equals(currentSchemaName)) {
3762-
if (queue.contains(childName) || descendentSchemas.stream().anyMatch(i -> childName.equals(i.getMappingName()))) {
3763-
throw new RuntimeException("Stack overflow hit when looking for " + thisSchemaName + " an infinite loop starting and ending at " + childName + " was seen");
3764-
}
3765-
queue.add(childName);
3766-
break;
3767-
}
3768-
}
3769-
}
3770-
}
3771-
}
3772-
if (queue.size() == 0) {
3773-
break;
3774-
}
3775-
currentSchemaName = queue.remove(0);
3776-
Schema cs = schemas.get(currentSchemaName);
3777-
String mappingName = discriminatorVendorExtensionValue(cs)
3778-
.orElse(currentSchemaName);
3779-
MappedModel mm = new MappedModel(mappingName, currentSchemaName, currentSchemaName, !mappingName.equals(currentSchemaName));
3780-
descendentSchemas.add(mm);
3781-
}
3782-
return descendentSchemas;
3783-
}
3784-
37853677
protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema) {
37863678
DiscriminatorData sourceDiscriminator = recursiveGetDiscriminator(schema, new ArrayList<Schema>());
37873679
if (sourceDiscriminator == null) {
@@ -3822,7 +3714,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
38223714

38233715
// check to see if the discriminator property is an enum string
38243716
boolean isEnum = Optional
3825-
.ofNullable(DiscriminatorUtils.discriminatorFound(openAPI, schemaName, schema, discriminatorPropertyName, new TreeSet<>()))
3717+
.ofNullable(discriminatorFound(openAPI, schemaName, schema, discriminatorPropertyName, new TreeSet<>()))
38263718
.map(CodegenProperty::getIsEnum)
38273719
.orElse(false);
38283720
discriminator.setIsEnum(isEnum);
@@ -3833,8 +3725,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
38333725
boolean legacyUseCase = (this.getLegacyDiscriminatorBehavior() && uniqueDescendants.isEmpty());
38343726
if (!this.getLegacyDiscriminatorBehavior() || legacyUseCase) {
38353727
// for schemas that allOf inherit from this schema, add those descendants to this discriminator map
3836-
List<MappedModel> otherDescendants =
3837-
adjustModelNames(getAllOfDescendants(schemaName));
3728+
List<MappedModel> otherDescendants = adjustModelNames(getAllOfDescendants(openAPI, schemaName));
38383729
for (MappedModel otherDescendant : otherDescendants) {
38393730
// add only if the mapping names are not the same and the model names are not the same
38403731
boolean matched = false;
@@ -3854,7 +3745,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
38543745
// if there are composed oneOf/anyOf schemas, add them to this discriminator
38553746
if (ModelUtils.isComposedSchema(schema) && !this.getLegacyDiscriminatorBehavior()) {
38563747
List<MappedModel> otherDescendants =
3857-
adjustModelNames(getOneOfAnyOfDescendants(schemaName, discriminatorPropertyName, schema));
3748+
adjustModelNames(getOneOfAnyOfDescendants(openAPI, schemaName, discriminatorPropertyName, schema));
38583749
for (MappedModel otherDescendant : otherDescendants) {
38593750
// add only if the model names are not the same
38603751
if (uniqueDescendants.stream().map(MappedModel::getModelName).noneMatch(it -> it.equals(otherDescendant.getModelName()))) {

0 commit comments

Comments
 (0)