mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-07 00:55:55 +00:00
Return empty result set when query empty. Fixes #1840
This commit is contained in:
parent
b973f8c963
commit
992a075cfb
2 changed files with 19 additions and 10 deletions
21
handler.go
21
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 {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue