[#586] fixed multiple nested relation expansions with shared base path

This commit is contained in:
Gani Georgiev
2022-09-21 13:34:34 +03:00
parent a1ad5004f8
commit 9a8c9dd115
14 changed files with 57 additions and 16 deletions

View File

@@ -114,12 +114,41 @@ func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetch
expandData := model.GetExpand()
// normalize and set the expanded relations
// normalize access to the previously expanded rel records (if any)
var oldExpandedRels []*models.Record
switch v := expandData[relField.Name].(type) {
case nil:
// no old expands
case *models.Record:
oldExpandedRels = []*models.Record{v}
case []*models.Record:
oldExpandedRels = v
}
// merge expands
for _, oldExpandedRel := range oldExpandedRels {
// find a matching rel record
for _, rel := range validRels {
if rel.Id != oldExpandedRel.Id {
continue
}
oldRelExpand := oldExpandedRel.GetExpand()
newRelExpand := rel.GetExpand()
for k, v := range oldRelExpand {
newRelExpand[k] = v
}
rel.SetExpand(newRelExpand)
}
}
// update the expanded data
if relFieldOptions.MaxSelect == 1 {
expandData[relField.Name] = validRels[0]
} else {
expandData[relField.Name] = validRels
}
model.SetExpand(expandData)
}