Skip to content

Commit c2fdf79

Browse files
authored
Merge pull request #3818 from omnidynmc/fix/bolt-empty-templates-nil-marshals-as-null
fix(bolt): return empty slice for templates list with no rows (#3245)
2 parents 71e2baf + 8c25a31 commit c2fdf79

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

db/bolt/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func (d *BoltDb) GetTemplatesWithPermissions(projectID int, userID int, filter d
7676
return
7777
}
7878

79+
// Initialize as a non-nil slice so an empty result marshals to `[]` rather than `null`.
80+
// The web UI's templates page hangs when the API returns `null` (issue #3245).
81+
templates = make([]db.TemplateWithPerms, 0, len(res))
82+
7983
for _, tpl := range res {
8084
var tplWithPerms db.TemplateWithPerms
8185
tplWithPerms.Template = tpl

db/bolt/template_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,31 @@ func Test_SetTemplateDescription(t *testing.T) {
4141
t.Fatalf("expected description to be 'New description', got '%s'", *tpl.Description)
4242
}
4343
}
44+
45+
// Regression test for #3245: GetTemplatesWithPermissions used to return a nil
46+
// slice when no templates matched, which JSON-marshals to `null` and wedges
47+
// the web UI's templates page. The API contract is an array; an empty result
48+
// must serialize to `[]`.
49+
func Test_GetTemplatesWithPermissions_EmptyReturnsNonNilSlice(t *testing.T) {
50+
store := CreateTestStore()
51+
52+
proj, err := store.CreateProject(db.Project{
53+
Created: tz.Now(),
54+
Name: "TestProject",
55+
})
56+
if err != nil {
57+
t.Fatal(err.Error())
58+
}
59+
60+
templates, err := store.GetTemplatesWithPermissions(proj.ID, 0, db.TemplateFilter{}, db.RetrieveQueryParams{})
61+
if err != nil {
62+
t.Fatal(err.Error())
63+
}
64+
65+
if templates == nil {
66+
t.Fatal("expected empty (non-nil) slice for project with no templates, got nil")
67+
}
68+
if len(templates) != 0 {
69+
t.Fatalf("expected zero templates, got %d", len(templates))
70+
}
71+
}

0 commit comments

Comments
 (0)