From 52fc9d61f3efb33a27afbf6e7faa64d36f0da7a2 Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Sat, 21 Nov 2026 07:13:43 -0800 Subject: [PATCH] fix /compile endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix POST /compile, which failed with type "ast.Assignment" redefined. The AST is now returned without type names, so the endpoint’s output matches super compile. Also, Respond now serializes the body before writing the status, so a serialization error returns an error status instead of HTTP 200. Fixes the Content-Type of these responses, which was previously text/plain because the status was written before the header was set. Adds a success case to service/ztests/compile.yaml. --- service/handlers.go | 13 ++++++++++-- service/request.go | 40 ++++++++++++++++++------------------- service/ztests/compile.yaml | 8 ++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/service/handlers.go b/service/handlers.go index f27162b84..d40fb4bf4 100644 --- a/service/handlers.go +++ b/service/handlers.go @@ -178,7 +178,16 @@ func handleCompile(c *Core, w *ResponseWriter, r *Request) { w.Error(srverr.ErrInvalid(err)) return } - w.Respond(http.StatusOK, ast.Parsed()) + // The AST is marshaled without type names, as by "super compile", + // since a named struct type like ast.Assignment would be bound to + // different record types as the concrete types of its interface + // fields vary. + val, err := super.NewMarshaler(w.sctx).Marshal(ast.Parsed()) + if err != nil { + w.Error(err) + return + } + w.RespondValue(http.StatusOK, val) } func handleQueryDescribe(c *Core, w *ResponseWriter, r *Request) { @@ -616,7 +625,7 @@ func handleDelete(c *Core, w *ResponseWriter, r *Request) { w.Error(err) return } - w.Marshal(api.CommitResponse{Commit: commit}) + w.Respond(http.StatusOK, api.CommitResponse{Commit: commit}) c.publishEvent(w, "branch-commit", api.EventBranchCommit{ CommitID: commit, TableID: table.ID, diff --git a/service/request.go b/service/request.go index b87739c03..33f2d954a 100644 --- a/service/request.go +++ b/service/request.go @@ -266,8 +266,27 @@ func (w *ResponseWriter) Write(b []byte) (int, error) { } func (w *ResponseWriter) Respond(status int, body any) bool { + val, err := w.marshaler.Marshal(body) + if err != nil { + w.Error(err) + return false + } + return w.RespondValue(status, val) +} + +// RespondValue writes the status and then val in the response format. +func (w *ResponseWriter) RespondValue(status int, val super.Value) bool { + zw := w.ZioWriter() + if zw == nil { + return false + } w.WriteHeader(status) - return w.Marshal(body) + if err := zw.Push(sbuf.Dematerialize(w.sctx, val)); err != nil { + w.Error(err) + return false + } + zw.Close() + return true } func (w *ResponseWriter) Error(err error) { @@ -290,25 +309,6 @@ func (w *ResponseWriter) Error(err error) { } } -func (w *ResponseWriter) Marshal(body any) bool { - val, err := w.marshaler.Marshal(body) - if err != nil { - // XXX If status header has not been sent this should send error. - w.Error(err) - return false - } - zw := w.ZioWriter() - if zw == nil { - return false - } - if err := zw.Push(sbuf.Dematerialize(w.sctx, val)); err != nil { - w.Error(err) - return false - } - zw.Close() - return true -} - func errorResponse(e error) (status int, ae *api.Error) { status = http.StatusInternalServerError ae = &api.Error{Type: "Error"} diff --git a/service/ztests/compile.yaml b/service/ztests/compile.yaml index 88e21dfa2..29aec661d 100644 --- a/service/ztests/compile.yaml +++ b/service/ztests/compile.yaml @@ -1,6 +1,11 @@ script: | source service.sh curl -d '{"query":"count("}' $DB_URL/compile | super -s -c 'cut compilation_errors' - + echo === + curl -s -o /dev/null -w '%{http_code}\n' -d '{"query":"from t | count() by x"}' $DB_URL/compile + curl -s -d '{"query":"from t | count() by x"}' $DB_URL/compile > server.sup + super compile -s -c 'from t | count() by x' > local.sup + cmp server.sup local.sup && echo same inputs: - name: service.sh @@ -9,3 +14,6 @@ outputs: - name: stdout data: | {compilation_errors:[{Msg:"parse error",Pos:6,End:-1}]} + === + 200 + same