From bb527be493c70d5e4a5fb15271b085d8cfd435d2 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sat, 30 Jul 2022 07:58:42 +0300 Subject: [PATCH] fixed panic on expanding existing byt non-relation type field --- daos/record_expand.go | 9 ++++++--- daos/record_expand_test.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/daos/record_expand.go b/daos/record_expand.go index 91f5f6ea..c21ae71f 100644 --- a/daos/record_expand.go +++ b/daos/record_expand.go @@ -58,11 +58,14 @@ func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetch // extract the relation field (if exist) mainCollection := records[0].Collection() relField := mainCollection.Schema.GetFieldByName(parts[0]) - if relField == nil { - return fmt.Errorf("Couldn't find field %q in collection %q.", parts[0], mainCollection.Name) + if relField == nil || relField.Type != schema.FieldTypeRelation { + return fmt.Errorf("Couldn't find relation field %q in collection %q.", parts[0], mainCollection.Name) } relField.InitOptions() - relFieldOptions, _ := relField.Options.(*schema.RelationOptions) + relFieldOptions, ok := relField.Options.(*schema.RelationOptions) + if !ok { + return fmt.Errorf("Cannot initialize the options of relation field %q.", parts[0]) + } relCollection, err := dao.FindCollectionByNameOrId(relFieldOptions.CollectionId) if err != nil { diff --git a/daos/record_expand_test.go b/daos/record_expand_test.go index 86dd9cf5..227a9b90 100644 --- a/daos/record_expand_test.go +++ b/daos/record_expand_test.go @@ -63,7 +63,7 @@ func TestExpandRecords(t *testing.T) { 0, 2, }, - // invalid missing first level expand + // missing relation field { []string{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"}, []string{"invalid"}, @@ -73,7 +73,17 @@ func TestExpandRecords(t *testing.T) { 0, 1, }, - // invalid missing second level expand + // existing, but non-relation type field + { + []string{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"}, + []string{"title"}, + func(c *models.Collection, ids []string) ([]*models.Record, error) { + return app.Dao().FindRecordsByIds(c, ids, nil) + }, + 0, + 1, + }, + // invalid/missing second level expand { []string{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"}, []string{"manyrels.invalid"},