mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user