Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

Applying QueryParamsMulti last makes a spec list override a scalar that ApplyQueryParamsFn or InjectPaging writes later in request assembly, even though those writes previously took precedence over the spec. This can send the spec's filter instead of the callback's value or keep the page index fixed while the CLI advances it. Other locations where this applies: pkg/endpoint/paging.go:74, pkg/registry/endpoint.go:268

Severity: medium


🤖 Was this useful? React with 👍 or 👎

for _, v := range vs {
if v != "" {
q.Add(k, v)
}
}
}
u.RawQuery = q.Encode()

var bodyReader io.Reader
Expand Down
21 changes: 21 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -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"))
}
}
17 changes: 8 additions & 9 deletions pkg/endpoint/paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package endpoint

import (
"fmt"
"maps"
"net/http"
"strings"

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions pkg/endpoint/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

ResultString treats the literal strings <nil> and map[] as empty, so EvalQueryParams drops those values even when they are explicitly supplied as list items. A multi-value search for either string can therefore lose its filter entirely.

Severity: low


🤖 Was this useful? React with 👍 or 👎

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
}
53 changes: 53 additions & 0 deletions pkg/endpoint/query_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
11 changes: 8 additions & 3 deletions pkg/exprenv/exprenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ func EvalExpr(env map[string]any, s string) string {
if !ok {
return ""
}
s2 := strutil.Stringify(out)
if s2 == "<nil>" || 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 == "<nil>" || s == "map[]" {
return ""
}
return s2
return s
}

// EvalHeaders evaluates pipeline_auth's header expressions in a restricted env
Expand Down
20 changes: 12 additions & 8 deletions pkg/registry/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

callEndpointFull passes qpm to the client only when extraHeaders is non-empty; the headerless Get, Post, Put, and Delete helpers receive only the scalar map. A list-valued filter on an ordinary GET or non-paged list therefore goes missing from the URL, and previews of headerless writes also omit the values.

Severity: medium


🤖 Was this useful? React with 👍 or 👎

if method == "GET" {
if err := endpoint.ApplyQueryParamsFn(ctx, ep, qp); err != nil {
return nil, nil, err
Expand All @@ -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)
}
Expand All @@ -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":
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep Code Review Agent🐛

evalQueryParams now discards list results, so callers that only use this helper—including file-body requests and both legs of get-then-put—omit keys they previously sent as strings. Those branches are described as unchanged, but supplying --file can now silently remove a query parameter from the same command.

Severity: medium


🤖 Was this useful? React with 👍 or 👎

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 {
Expand Down