[#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
+30 -1
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)
}
+12
View File
@@ -108,6 +108,18 @@ func TestExpandRecords(t *testing.T) {
9,
0,
},
// expand multiple relations sharing a common root path
{
[]string{
"i15r5aa28ad06c8",
},
[]string{"manyrels.onerel.manyrels.onerel", "manyrels.onerel.onerel"},
func(c *models.Collection, ids []string) ([]*models.Record, error) {
return app.Dao().FindRecordsByIds(c, ids, nil)
},
4,
0,
},
// single expand
{
[]string{