Handle topdown.Undefined Query result in API

If the caller GETs a completely defined virtual doc that evaluates to
undefined, the API should return an error instead of 2xx.
This commit is contained in:
Torin Sandall
2016-06-06 16:59:41 -07:00
parent 9444b16cd6
commit 15b9b69bf1
2 changed files with 16 additions and 0 deletions
+10
View File
@@ -36,6 +36,11 @@ func (err *apiErrorV1) Bytes() []byte {
return nil
}
// undefinedV1 models the an undefined query result.
type undefinedV1 struct {
IsUndefined bool
}
// patchV1 models a single patch operation against a document.
type patchV1 struct {
Op string `json:"op"`
@@ -213,6 +218,11 @@ func (s *Server) v1DataGet(w http.ResponseWriter, r *http.Request) {
return
}
if _, ok := result.(topdown.Undefined); ok {
handleResponseJSON(w, 404, undefinedV1{true})
return
}
handleResponseJSON(w, 200, result)
}
+6
View File
@@ -52,6 +52,8 @@ func TestDataV1(t *testing.T) {
import req1
import req2 as reqx
g :- req1.a[0] = 1, reqx.b[i] = 1
undef :- false
`
tests := []struct {
note string
@@ -110,6 +112,10 @@ func TestDataV1(t *testing.T) {
"Message": "evaluation error (code: 1): unbound variable req2: req2.b[i]"
}`},
}},
{"get undefined", []tr{
tr{"PUT", "/policies/test", testMod, 200, ""},
tr{"GET", "/data/testmod/undef", "", 404, `{"IsUndefined": true}`},
}},
}
for i, tc := range tests {