fixed panic on expanding existing byt non-relation type field

This commit is contained in:
Gani Georgiev 2022-07-30 07:58:42 +03:00
parent 9e3b230c8e
commit bb527be493
2 changed files with 18 additions and 5 deletions

View File

@ -58,11 +58,14 @@ func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetch
// extract the relation field (if exist) // extract the relation field (if exist)
mainCollection := records[0].Collection() mainCollection := records[0].Collection()
relField := mainCollection.Schema.GetFieldByName(parts[0]) relField := mainCollection.Schema.GetFieldByName(parts[0])
if relField == nil { if relField == nil || relField.Type != schema.FieldTypeRelation {
return fmt.Errorf("Couldn't find field %q in collection %q.", parts[0], mainCollection.Name) return fmt.Errorf("Couldn't find relation field %q in collection %q.", parts[0], mainCollection.Name)
} }
relField.InitOptions() 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) relCollection, err := dao.FindCollectionByNameOrId(relFieldOptions.CollectionId)
if err != nil { if err != nil {

View File

@ -63,7 +63,7 @@ func TestExpandRecords(t *testing.T) {
0, 0,
2, 2,
}, },
// invalid missing first level expand // missing relation field
{ {
[]string{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"}, []string{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"},
[]string{"invalid"}, []string{"invalid"},
@ -73,7 +73,17 @@ func TestExpandRecords(t *testing.T) {
0, 0,
1, 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{"b8ba58f9-e2d7-42a0-b0e7-a11efd98236b", "df55c8ff-45ef-4c82-8aed-6e2183fe1125"},
[]string{"manyrels.invalid"}, []string{"manyrels.invalid"},