From e6e0fd2d9c12ea156f380d0a95a766d9bf622fbb Mon Sep 17 00:00:00 2001 From: Vlasta Hajek Date: Wed, 9 Nov 2022 18:05:21 +0100 Subject: [PATCH] fix: listBuckets properly returns wrapped response (#23879) closes https://github.com/influxdata/influxdb/issues/23861 --- services/httpd/handler.go | 7 ++++++- services/httpd/handler_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/services/httpd/handler.go b/services/httpd/handler.go index 936b6a3e51..9c1cee78d7 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -1082,6 +1082,10 @@ type Bucket struct { UpdatedAt time.Time `json:"updatedAt"` } +type Buckets struct { + Buckets []Bucket `json:"buckets"` +} + func (h *Handler) servePostCreateBucketV2(w http.ResponseWriter, r *http.Request, user meta.User) { var bs []byte if r.ContentLength > 0 { @@ -1457,7 +1461,8 @@ func findBucketIndex(dbs []meta.DatabaseInfo, db string, rp string) (dbIndex int } func (h *Handler) sendBuckets(w http.ResponseWriter, buckets []Bucket) { - b, err := json.Marshal(buckets) + bucketsObj := Buckets{buckets} + b, err := json.Marshal(bucketsObj) if err != nil { h.httpError(w, fmt.Sprintf("list buckets marshaling error: %s", err.Error()), http.StatusInternalServerError) return diff --git a/services/httpd/handler_test.go b/services/httpd/handler_test.go index 2c35bea0c7..b67b2bd1a4 100644 --- a/services/httpd/handler_test.go +++ b/services/httpd/handler_test.go @@ -2212,18 +2212,18 @@ func TestHandler_ListBuckets(t *testing.T) { t.Fatalf("incorrect error message, expected: %q, got: %q", ct.errMsg, errMsg) } } else { - var got []httpd.Bucket + var got httpd.Buckets exp := getBuckets(ct.skip, ct.limit) if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatalf("unmarshaling buckets: %s", err.Error()) } - if len(exp) != len(got) { - t.Fatalf("expected %d buckets returned, got %d", len(exp), len(got)) + if len(exp) != len(got.Buckets) { + t.Fatalf("expected %d buckets returned, got %d", len(exp), len(got.Buckets)) } - for i := 0; i < len(got); i++ { - if exp[i] != got[i].Name { - t.Fatalf("expected %q, got %q", exp[i], got[i].Name) + for i := 0; i < len(got.Buckets); i++ { + if exp[i] != got.Buckets[i].Name { + t.Fatalf("expected %q, got %q", exp[i], got.Buckets[i].Name) } } }