From 6cd7ad395236aac62c3c26a80f4e8efbe4914883 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Tue, 15 Dec 2020 16:52:00 +0000 Subject: [PATCH] api: Ensure the internal/ui/service endpoint responds with an array (#9397) In some circumstances this endpoint will have no results in it (dues to ACLs, Namespaces or filtering). This ensures that the response is at least an empty array (`[]`) rather than `null` --- agent/ui_endpoint.go | 3 ++- agent/ui_endpoint_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/agent/ui_endpoint.go b/agent/ui_endpoint.go index f08fb9a96c..cf1206327d 100644 --- a/agent/ui_endpoint.go +++ b/agent/ui_endpoint.go @@ -205,7 +205,8 @@ RPC: summaries, hasProxy := summarizeServices(out.Nodes.ToServiceDump(), nil, "") sorted := prepSummaryOutput(summaries, false) - var result []*ServiceListingSummary + // Ensure at least a zero length slice + result := make([]*ServiceListingSummary, 0) for _, svc := range sorted { sum := ServiceListingSummary{ServiceSummary: *svc} diff --git a/agent/ui_endpoint_test.go b/agent/ui_endpoint_test.go index 2fec77cc20..2357a3c0fa 100644 --- a/agent/ui_endpoint_test.go +++ b/agent/ui_endpoint_test.go @@ -528,6 +528,21 @@ func TestUiServices(t *testing.T) { } require.ElementsMatch(t, expected, summary) }) + t.Run("Filtered without results", func(t *testing.T) { + filterQuery := url.QueryEscape("Service.Service == absent") + req, _ := http.NewRequest("GET", "/v1/internal/ui/services?filter="+filterQuery, nil) + resp := httptest.NewRecorder() + obj, err := a.srv.UIServices(resp, req) + require.NoError(t, err) + assertIndex(t, resp) + + // Ensure the ServiceSummary doesn't output a `null` response when there + // are no matching summaries + require.NotNil(t, obj) + + summary := obj.([]*ServiceListingSummary) + require.Len(t, summary, 0) + }) } func TestUIGatewayServiceNodes_Terminating(t *testing.T) {