From 992a075cfb89512da9bcc4ee7dd706a237d0cf50 Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Wed, 10 Apr 2019 15:27:12 -0500 Subject: [PATCH] Return empty result set when query empty. Fixes #1840 --- handler.go | 21 +++++++++++---------- server/handler_test.go | 8 ++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/handler.go b/handler.go index 988435a07..5a452ce96 100644 --- a/handler.go +++ b/handler.go @@ -45,18 +45,19 @@ type QueryResponse struct { // MarshalJSON marshals QueryResponse into a JSON-encoded byte slice func (resp *QueryResponse) MarshalJSON() ([]byte, error) { - var output struct { - Results []interface{} `json:"results,omitempty"` - ColumnAttrSets []*ColumnAttrSet `json:"columnAttrs,omitempty"` - Err string `json:"error,omitempty"` - } - output.Results = resp.Results - output.ColumnAttrSets = resp.ColumnAttrSets - if resp.Err != nil { - output.Err = resp.Err.Error() + return json.Marshal(struct { + Err string `json:"error"` + }{Err: resp.Err.Error()}) } - return json.Marshal(output) + + return json.Marshal(struct { + Results []interface{} `json:"results"` + ColumnAttrSets []*ColumnAttrSet `json:"columnAttrs,omitempty"` + }{ + Results: resp.Results, + ColumnAttrSets: resp.ColumnAttrSets, + }) } type Handler interface { diff --git a/server/handler_test.go b/server/handler_test.go index 701d019ae..ed7a2bed6 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -445,6 +445,14 @@ func TestHandler_Endpoints(t *testing.T) { } }) + t.Run("Query empty", func(t *testing.T) { + w := httptest.NewRecorder() + h.ServeHTTP(w, test.MustNewHTTPRequest("POST", "/index/i0/query", strings.NewReader(""))) + if body := w.Body.String(); body != `{"results":[]}`+"\n" { + t.Fatalf("unexpected body: %q", body) + } + }) + t.Run("Method not allowed", func(t *testing.T) { w := httptest.NewRecorder() h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/index/i0/query", nil))