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 @@ -1209,10 +1209,25 @@ public String toString() {
return sb.toString();
}

public void addDiscriminatorMappedModelsImports() {
/*
* To clean up mapped models if needed and add mapped models to imports
*
* @param cleanUpMappedModels Clean up mapped models if set to true
*/
public void addDiscriminatorMappedModelsImports(boolean cleanUpMappedModels) {
if (discriminator == null || discriminator.getMappedModels() == null) {
return;
}

if (cleanUpMappedModels && !this.hasChildren && // no child
(this.oneOf == null || this.oneOf.isEmpty()) && // not oneOf
(this.anyOf == null || this.anyOf.isEmpty())) { // not anyOf
//clear the mapping
discriminator.setMappedModels(null);
return;
}

// import child schemas defined in mapped models
for (CodegenDiscriminator.MappedModel mm : discriminator.getMappedModels()) {
if (!"".equals(mm.getModelName())) {
imports.add(mm.getModelName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.openapitools.codegen.api.TemplatingEngineAdapter;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.examples.ExampleGenerator;
import org.openapitools.codegen.languages.PythonClientCodegen;
import org.openapitools.codegen.languages.RustServerCodegen;
import org.openapitools.codegen.meta.FeatureSet;
import org.openapitools.codegen.meta.GeneratorMetadata;
Expand Down Expand Up @@ -611,6 +612,7 @@ public Map<String, CodegenModel> getAllModels(Map<String, ModelsMap> objs) {

/**
* Loop through all models to update different flags (e.g. isSelfReference), children models, etc
* and update mapped models for import.
*
* @param objs Map of models
* @return maps of models with various updates
Expand Down Expand Up @@ -661,10 +663,17 @@ public Map<String, ModelsMap> updateAllModels(Map<String, ModelsMap> objs) {
}

// loop through properties of each model to detect self-reference
// and update mapped models for import
for (ModelsMap entry : objs.values()) {
for (ModelMap mo : entry.getModels()) {
CodegenModel cm = mo.getModel();
removeSelfReferenceImports(cm);

if (!this.getLegacyDiscriminatorBehavior()) {
// skip cleaning up mapped models for python client generator
// which uses its own logic
cm.addDiscriminatorMappedModelsImports(!(this instanceof PythonClientCodegen));
}
}
}
setCircularReferences(allModels);
Expand Down Expand Up @@ -2655,9 +2664,6 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
if (m.discriminator == null && innerSchema.getDiscriminator() != null) {
LOGGER.debug("discriminator is set to null (not correctly set earlier): {}", m.name);
m.setDiscriminator(createDiscriminator(m.name, innerSchema, this.openAPI));
if (!this.getLegacyDiscriminatorBehavior()) {
m.addDiscriminatorMappedModelsImports();
}
modelDiscriminators++;
}

Expand Down Expand Up @@ -2812,6 +2818,7 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
if (Boolean.TRUE.equals(schema.getNullable())) {
m.isNullable = Boolean.TRUE;
}

// end of code block for composed schema
}

Expand Down Expand Up @@ -2999,9 +3006,6 @@ public CodegenModel fromModel(String name, Schema schema) {
m.isAlias = (typeAliases.containsKey(name)
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
m.setDiscriminator(createDiscriminator(name, schema, this.openAPI));
if (!this.getLegacyDiscriminatorBehavior()) {
m.addDiscriminatorMappedModelsImports();
}

if (schema.getDeprecated() != null) {
m.isDeprecated = schema.getDeprecated();
Expand Down Expand Up @@ -3455,15 +3459,15 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName, OpenAPI o
break;
}
currentSchemaName = queue.remove(0);
MappedModel mm = new MappedModel(currentSchemaName, toModelName(currentSchemaName));
descendentSchemas.add(mm);
Schema cs = schemas.get(currentSchemaName);
Map<String, Object> vendorExtensions = cs.getExtensions();
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
mm = new MappedModel(xDiscriminatorValue, toModelName(currentSchemaName));
descendentSchemas.add(mm);
}
String mappingName =
Optional.ofNullable(vendorExtensions)
.map(ve -> ve.get("x-discriminator-value"))
.map(discriminatorValue -> (String) discriminatorValue)
.orElse(currentSchemaName);
MappedModel mm = new MappedModel(mappingName, toModelName(currentSchemaName));
descendentSchemas.add(mm);
}
return descendentSchemas;
}
Expand Down Expand Up @@ -3513,10 +3517,11 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
// for schemas that allOf inherit from this schema, add those descendants to this discriminator map
List<MappedModel> otherDescendants = getAllOfDescendants(schemaName, openAPI);
for (MappedModel otherDescendant : otherDescendants) {
// add only if the mapping names are not the same
// add only if the mapping names are not the same and the model names are not the same
boolean matched = false;
for (MappedModel uniqueDescendant : uniqueDescendants) {
if (uniqueDescendant.getMappingName().equals(otherDescendant.getMappingName())) {
if (uniqueDescendant.getMappingName().equals(otherDescendant.getMappingName())
|| (uniqueDescendant.getModelName().equals(otherDescendant.getModelName()))) {
matched = true;
break;
}
Expand Down Expand Up @@ -7756,9 +7761,7 @@ public void addOneOfInterfaceModel(ComposedSchema cs, String type, OpenAPI openA
CodegenModel cm = new CodegenModel();

cm.setDiscriminator(createDiscriminator("", cs, openAPI));
if (!this.getLegacyDiscriminatorBehavior()) {
cm.addDiscriminatorMappedModelsImports();
}

for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
if (o.get$ref() == null) {
if (cm.discriminator != null && o.get$ref() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
objs = super.postProcessModels(objs);
List<ModelMap> models = objs.getModels();
ProcessUtils.addIndexToProperties(models, 1);

for (ModelMap mo : models) {
CodegenModel cm = mo.getModel();
cm.imports = rewriteImports(cm.imports, true);
cm.vendorExtensions.put("x-has-vars", !cm.vars.isEmpty());
}
return objs;
}

Expand Down Expand Up @@ -583,6 +577,16 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
adaptToDartInheritance(objs);
syncRootTypesWithInnerVars(objs);
}

// loop through models to update the imports
for (ModelsMap entry : objs.values()) {
for (ModelMap mo : entry.getModels()) {
CodegenModel cm = mo.getModel();
cm.imports = rewriteImports(cm.imports, true);
cm.vendorExtensions.put("x-has-vars", !cm.vars.isEmpty());
}
}

return objs;
}

Expand Down Expand Up @@ -730,6 +734,10 @@ private Set<String> rewriteImports(Set<String> originalImports, boolean isModel)
resultImports.add(i);
} else if (importMapping().containsKey(modelImport)) {
resultImports.add(importMapping().get(modelImport));
} else if (modelImport.startsWith("dart:")) { // import dart:* directly
resultImports.add(modelImport);
} else if (modelImport.startsWith("package:")) { // e.g. package:openapi/src/model/child.dart
resultImports.add(modelImport);
} else {
resultImports.add("package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/" + underscore(modelImport) + ".dart");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2438,8 +2438,17 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
if (m.discriminator == null && innerSchema.getDiscriminator() != null) {
LOGGER.debug("discriminator is set to null (not correctly set earlier): {}", m.name);
m.setDiscriminator(createDiscriminator(m.name, innerSchema, this.openAPI));
// directly include the function `addDiscriminatorMappedModelsImports` inline below
// as the function has been updated
//m.addDiscriminatorMappedModelsImports();
if (!this.getLegacyDiscriminatorBehavior()) {
m.addDiscriminatorMappedModelsImports();
if (m.discriminator != null && m.discriminator.getMappedModels() != null) {
for (CodegenDiscriminator.MappedModel mm : m.discriminator.getMappedModels()) {
if (!"".equals(mm.getModelName())) {
m.getImports().add(mm.getModelName());
}
}
}
}
modelDiscriminators++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@ public void testComposedSchemaAllOfDiscriminatorMap() {
cm = codegen.fromModel(modelName, sc);
hs.clear();
hs.add(new CodegenDiscriminator.MappedModel("b", codegen.toModelName("B")));
hs.add(new CodegenDiscriminator.MappedModel("B", codegen.toModelName("B")));
hs.add(new CodegenDiscriminator.MappedModel("C", codegen.toModelName("C")));
Assert.assertEquals(cm.getHasDiscriminatorWithNonEmptyMapping(), true);
Assert.assertEquals(cm.discriminator.getMappedModels(), hs);
Expand Down Expand Up @@ -1585,8 +1584,6 @@ public void verifyXDiscriminatorValue() {
discriminator.setPropertyBaseName(prop);
discriminator.setMapping(null);
discriminator.setMappedModels(new HashSet<CodegenDiscriminator.MappedModel>() {{
add(new CodegenDiscriminator.MappedModel("DailySubObj", "DailySubObj"));
add(new CodegenDiscriminator.MappedModel("SubObj", "SubObj"));
add(new CodegenDiscriminator.MappedModel("daily", "DailySubObj"));
add(new CodegenDiscriminator.MappedModel("sub-obj", "SubObj"));
}});
Expand Down Expand Up @@ -1984,8 +1981,6 @@ private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
test.getMapping().put("c", "Child");
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("a", "Adult"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("c", "Child"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("Adult", "Adult"));
test.getMappedModels().add(new CodegenDiscriminator.MappedModel("Child", "Child"));
Assert.assertEquals(discriminator, test);
}

Expand Down
Loading