From 15b9b69bf1193def7f57778a176e971e6f7d147a Mon Sep 17 00:00:00 2001 From: Torin Sandall Date: Mon, 6 Jun 2016 16:59:41 -0700 Subject: [PATCH] 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. --- runtime/server.go | 10 ++++++++++ runtime/server_test.go | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/runtime/server.go b/runtime/server.go index 2c9dd9c52d..63713addcd 100644 --- a/runtime/server.go +++ b/runtime/server.go @@ -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) } diff --git a/runtime/server_test.go b/runtime/server_test.go index d391376ad2..5f53c46f1d 100644 --- a/runtime/server_test.go +++ b/runtime/server_test.go @@ -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 {