Repository navigation
feat(spec): send list-valued query_params as repeated keys #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 != "" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
| } | ||
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Severity: medium 🤖 Was this useful? React with 👍 or 👎 |
||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 { | ||
|
|
||
There was a problem hiding this comment.
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
QueryParamsMultilast makes a spec list override a scalar thatApplyQueryParamsFnorInjectPagingwrites 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:268Severity: medium
🤖 Was this useful? React with 👍 or 👎