diff --git a/pkg/client/client.go b/pkg/client/client.go index 837ebcb0..6ac3f28b 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -138,6 +138,9 @@ type Request struct { Method string Path string QueryParams map[string]string + // QueryParamsMulti sends each value under its own repeated key (kind=a&kind=b), + // replacing any QueryParams entry or path query value for the same key. + QueryParamsMulti map[string][]string // Body is always fully materialized: string → sent as-is with BodyContentType; // any other type → JSON-marshaled, BodyContentType defaults to "application/json". Body any @@ -401,6 +404,14 @@ func (c *Client) buildRequest(r Request) (*http.Request, *url.URL, error) { q.Set(k, v) } } + for k, vs := range r.QueryParamsMulti { + q.Del(k) + for _, v := range vs { + if v != "" { + q.Add(k, v) + } + } + } u.RawQuery = q.Encode() var bodyReader io.Reader diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f1aa874a..5ca7da71 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -11,6 +11,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "reflect" "strings" "testing" @@ -218,3 +219,23 @@ func TestDoRequest_EmptyBodyStreamSendsZeroLengthNotChunked(t *testing.T) { t.Fatalf("ContentLength = %d chunked = %v, want 0 and not chunked", gotLen, chunked) } } + +func TestBuildRequest_QueryParamsMulti(t *testing.T) { + c := testClient("https://example.test") + _, u, err := c.buildRequest(Request{ + Method: "GET", + Path: "/items?kind=old&keep=1", + QueryParams: map[string]string{"kind": "single"}, + QueryParamsMulti: map[string][]string{"kind": {"a", "", "b"}}, + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + q := u.Query() + if got := q["kind"]; !reflect.DeepEqual(got, []string{"a", "b"}) { + t.Errorf("kind = %v, want [a b]", got) + } + if q.Get("keep") != "1" { + t.Errorf("keep = %q, want 1", q.Get("keep")) + } +} diff --git a/pkg/endpoint/paging.go b/pkg/endpoint/paging.go index d56eda3a..873d6fb1 100644 --- a/pkg/endpoint/paging.go +++ b/pkg/endpoint/paging.go @@ -5,6 +5,7 @@ package endpoint import ( "fmt" + "maps" "net/http" "strings" @@ -57,11 +58,8 @@ func BuildRequest(ctx *cmdctx.Ctx, ep *spec.EndpointSpec) (*client.Request, erro "orgIdentifier": a.OrgID, "projectIdentifier": a.ProjectID, } - for paramName, exprStr := range ep.QueryParams { - if result := exprenv.EvalExpr(exprEnv, exprStr); result != "" { - qp[paramName] = result - } - } + single, multi := EvalQueryParams(exprEnv, ep.QueryParams) + maps.Copy(qp, single) if err := ApplyQueryParamsFn(ctx, ep, qp); err != nil { return nil, err } @@ -70,10 +68,11 @@ func BuildRequest(ctx *cmdctx.Ctx, ep *spec.EndpointSpec) (*client.Request, erro method = http.MethodGet } req := &client.Request{ - Method: method, - Path: path, - QueryParams: qp, - NoAccountID: ep.NoAccountID, + Method: method, + Path: path, + QueryParams: qp, + QueryParamsMulti: multi, + NoAccountID: ep.NoAccountID, } if len(ep.BodyParams) > 0 { req.Body = buildBody(ep, exprEnv) diff --git a/pkg/endpoint/query.go b/pkg/endpoint/query.go index c78e1b37..9d6753e1 100644 --- a/pkg/endpoint/query.go +++ b/pkg/endpoint/query.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/harness/cli/v3/pkg/cmdctx" + "github.com/harness/cli/v3/pkg/exprenv" "github.com/harness/cli/v3/pkg/spec" ) @@ -34,3 +35,49 @@ func ApplyQueryParamsFn(ctx *cmdctx.Ctx, ep *spec.EndpointSpec, qp map[string]st } return nil } + +// EvalQueryParams evaluates query_params expressions. A list result becomes a repeated key +// (multi); anything else keeps the single-value string form. Nil, empty and all-blank +// results omit the key. +func EvalQueryParams(env map[string]any, exprs map[string]string) (single map[string]string, multi map[string][]string) { + single = map[string]string{} + for name, exprStr := range exprs { + out, ok := exprenv.EvalExprAny(env, exprStr) + if !ok { + continue + } + if list, isList := asList(out); isList { + var vals []string + for _, item := range list { + if s := exprenv.ResultString(item); s != "" { + vals = append(vals, s) + } + } + if len(vals) > 0 { + if multi == nil { + multi = map[string][]string{} + } + multi[name] = vals + } + continue + } + if s := exprenv.ResultString(out); s != "" { + single[name] = s + } + } + return single, multi +} + +func asList(v any) ([]any, bool) { + switch t := v.(type) { + case []any: + return t, true + case []string: + out := make([]any, len(t)) + for i, s := range t { + out[i] = s + } + return out, true + } + return nil, false +} diff --git a/pkg/endpoint/query_test.go b/pkg/endpoint/query_test.go new file mode 100644 index 00000000..e2fa64ad --- /dev/null +++ b/pkg/endpoint/query_test.go @@ -0,0 +1,53 @@ +// Copyright © 2026 Harness Inc. +// SPDX-License-Identifier: Apache-2.0 + +package endpoint + +import ( + "reflect" + "testing" +) + +func TestEvalQueryParams(t *testing.T) { + env := map[string]any{"flags": map[string]any{"ids": []string{"a", "b"}, "none": []string{}, "q": "x"}} + tests := []struct { + name string + exprs map[string]string + wantSingle map[string]string + wantMulti map[string][]string + }{ + { + name: "list literal becomes repeated key", + exprs: map[string]string{"kind": `["comment", "change-comment"]`}, + wantSingle: map[string]string{}, + wantMulti: map[string][]string{"kind": {"comment", "change-comment"}}, + }, + { + name: "string slice flag becomes repeated key", + exprs: map[string]string{"id": `flags.ids`}, + wantSingle: map[string]string{}, + wantMulti: map[string][]string{"id": {"a", "b"}}, + }, + { + name: "empty and blank lists omit the key", + exprs: map[string]string{"a": `flags.none`, "b": `["", ""]`}, + wantSingle: map[string]string{}, + }, + { + name: "joined list stays a single value", + exprs: map[string]string{"id": `join(flags.ids, ",")`, "q": `flags.q`, "nil": `nil`}, + wantSingle: map[string]string{"id": "a,b", "q": "x"}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + single, multi := EvalQueryParams(env, tc.exprs) + if !reflect.DeepEqual(single, tc.wantSingle) { + t.Errorf("single = %v, want %v", single, tc.wantSingle) + } + if !reflect.DeepEqual(multi, tc.wantMulti) { + t.Errorf("multi = %v, want %v", multi, tc.wantMulti) + } + }) + } +} diff --git a/pkg/exprenv/exprenv.go b/pkg/exprenv/exprenv.go index f4641fec..fce1fec6 100644 --- a/pkg/exprenv/exprenv.go +++ b/pkg/exprenv/exprenv.go @@ -216,11 +216,16 @@ func EvalExpr(env map[string]any, s string) string { if !ok { return "" } - s2 := strutil.Stringify(out) - if s2 == "" || s2 == "map[]" { + return ResultString(out) +} + +// ResultString renders an expression result the way EvalExpr does: "" for nil or an empty map. +func ResultString(out any) string { + s := strutil.Stringify(out) + if s == "" || s == "map[]" { return "" } - return s2 + return s } // EvalHeaders evaluates pipeline_auth's header expressions in a restricted env diff --git a/pkg/registry/endpoint.go b/pkg/registry/endpoint.go index 43d51474..20715b39 100644 --- a/pkg/registry/endpoint.go +++ b/pkg/registry/endpoint.go @@ -212,6 +212,7 @@ func callEndpointFull(ctx *cmdctx.Ctx, ep *spec.EndpointSpec, extraQueryParams m // Priority 3: default body_params / body_fn path. qp := evalQueryParams(ctx, ep.QueryParams, true, extraQueryParams) + qpm := evalQueryParamsMulti(ctx, ep.QueryParams) if method == "GET" { if err := endpoint.ApplyQueryParamsFn(ctx, ep, qp); err != nil { return nil, nil, err @@ -235,7 +236,7 @@ func callEndpointFull(ctx *cmdctx.Ctx, ep *spec.EndpointSpec, extraQueryParams m if rb, ok := body.(*cmdctx.RawBody); ok { hlog.Debug("raw body", "content_type", rb.ContentType, "size", len(rb.Content)) if len(extraHeaders) > 0 { - return c.DoRequest(client.Request{Method: method, Path: path, QueryParams: qp, Body: rb.Content, BodyContentType: rb.ContentType, Headers: extraHeaders}) + return c.DoRequest(client.Request{Method: method, Path: path, QueryParams: qp, QueryParamsMulti: qpm, Body: rb.Content, BodyContentType: rb.ContentType, Headers: extraHeaders}) } return c.PostRaw(path, qp, rb.Content, rb.ContentType) } @@ -247,7 +248,7 @@ func callEndpointFull(ctx *cmdctx.Ctx, ep *spec.EndpointSpec, extraQueryParams m body = map[string]any{} } } - return c.DoRequest(client.Request{Method: method, Path: path, QueryParams: qp, Body: body, BodyContentType: ct, Headers: extraHeaders}) + return c.DoRequest(client.Request{Method: method, Path: path, QueryParams: qp, QueryParamsMulti: qpm, Body: body, BodyContentType: ct, Headers: extraHeaders}) } switch method { case "POST": @@ -264,7 +265,7 @@ func callEndpointFull(ctx *cmdctx.Ctx, ep *spec.EndpointSpec, extraQueryParams m } default: if len(extraHeaders) > 0 { - return c.DoRequest(client.Request{Method: "GET", Path: path, QueryParams: qp, Headers: extraHeaders}) + return c.DoRequest(client.Request{Method: "GET", Path: path, QueryParams: qp, QueryParamsMulti: qpm, Headers: extraHeaders}) } return c.Get(path, qp) } @@ -588,17 +589,20 @@ func evalQueryParams(ctx *cmdctx.Ctx, exprs map[string]string, withScope bool, e params["orgIdentifier"] = ctx.Auth.OrgID params["projectIdentifier"] = ctx.Auth.ProjectID } - for paramName, exprStr := range exprs { - if result := exprenv.EvalExpr(exprEnv, exprStr); result != "" { - params[paramName] = result - } - } + single, _ := endpoint.EvalQueryParams(exprEnv, exprs) + maps.Copy(params, single) for _, e := range extra { maps.Copy(params, e) } return params } +// evalQueryParamsMulti returns the query_params expressions that evaluate to a list. +func evalQueryParamsMulti(ctx *cmdctx.Ctx, exprs map[string]string) map[string][]string { + _, multi := endpoint.EvalQueryParams(exprenv.Make(ctx), exprs) + return multi +} + // evalBodyParams evaluates a dot-path→expr map against ctx, returning a nested map[string]any. // Dot-path keys (e.g. "config.type") create nested objects via setDotPath. func evalBodyParams(ctx *cmdctx.Ctx, exprs map[string]string) map[string]any {