Skip to content
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

Fix empty strings with default func #408

Merged
merged 7 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Unreleased changes are available as `avenga/couper:edge` container.
* exclude file descriptor limit startup-logs for Windows ([#396](https://github.com/avenga/couper/pull/396), [#383](https://github.com/avenga/couper/pull/383))
* possible race conditions while updating JWKS for the [JWT access control](./docs/REFERENCE.md#jwt-block) ([#398](https://github.com/avenga/couper/pull/398))
* panic while accessing primitive variables with a key ([#377](https://github.com/avenga/couper/issues/377))
* `default()` function continues to their fallback value if this is a string type and an argument evaluates to an empty string ([#408](https://github.com/avenga/couper/issues/408))

* **Dependencies**
* Update modules for [OpenAPI](./docs/REFERENCE.md#openapi-block) validation ([#399](https://github.com/avenga/couper/pull/399))
Expand Down
4 changes: 2 additions & 2 deletions eval/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ func newFunctionsMap() map[string]function.Function {
return map[string]function.Function{
"base64_decode": lib.Base64DecodeFunc,
"base64_encode": lib.Base64EncodeFunc,
"coalesce": lib.CoalesceFunc,
"default": lib.CoalesceFunc,
"coalesce": lib.DefaultFunc,
"default": lib.DefaultFunc,
"json_decode": stdlib.JSONDecodeFunc,
"json_encode": stdlib.JSONEncodeFunc,
"merge": lib.MergeFunc,
Expand Down
7 changes: 6 additions & 1 deletion eval/lib/coalesce.go → eval/lib/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/zclconf/go-cty/cty/function"
)

var CoalesceFunc = function.New(&function.Spec{
var DefaultFunc = function.New(&function.Spec{
VarParam: &function.Parameter{
Name: "vals",
Type: cty.DynamicPseudoType,
Expand All @@ -32,6 +32,11 @@ var CoalesceFunc = function.New(&function.Spec{
continue
}

// If the fallback type is a string and this argument too but an empty one, consider them as unset.
if argVal.Type() == cty.String && argVal.AsString() == "" && retType == cty.String {
continue
}

return convert.Convert(argVal, retType)
}
return cty.NilVal, fmt.Errorf("no non-null or nil-type arguments")
Expand Down
13 changes: 11 additions & 2 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3694,7 +3694,16 @@ func TestFunctions(t *testing.T) {
for _, tc := range []testCase{
{"merge", "/v1/merge", map[string]string{"X-Merged-1": "{\"foo\":[1,2]}", "X-Merged-2": "{\"bar\":[3,4]}", "X-Merged-3": "[\"a\",\"b\"]"}, http.StatusOK},
{"coalesce", "/v1/coalesce?q=a", map[string]string{"X-Coalesce-1": "/v1/coalesce", "X-Coalesce-2": "default", "X-Coalesce-3": "default", "X-Coalesce-4": "default"}, http.StatusOK},
{"default", "/v1/default?q=a", map[string]string{"X-Default-1": "/v1/default", "X-Default-2": "default", "X-Default-3": "default", "X-Default-4": "default", "X-Default-5": "prefix-default", "X-Default-6": "default"}, http.StatusOK},
{"default", "/v1/default?q=a", map[string]string{
"X-Default-1": "/v1/default",
"X-Default-2": "default",
"X-Default-3": "default",
"X-Default-4": "default",
"X-Default-5": "prefix-default",
"X-Default-6": "default",
"X-Default-7": "default",
"X-Default-8": "default-8",
}, http.StatusOK},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand All @@ -3711,7 +3720,7 @@ func TestFunctions(t *testing.T) {

for k, v := range tc.header {
if v1 := res.Header.Get(k); v1 != v {
subT.Fatalf("%q: unexpected %s response header %#v, got: %#v", tc.name, k, v, v1)
subT.Fatalf("%q: unexpected header value for %q: got: %q, want: %q", tc.name, k, v1, v)
}
}
})
Expand Down
2 changes: 2 additions & 0 deletions server/testdata/integration/functions/01_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ server "api" {
x-default-4 = default(request.cookies.undef, request.query.q[1], "default", request.path)
x-default-5 = "prefix-${default(request.cookies.undef, "default")}" # template expr
x-default-6 = "${default(request.cookies.undef, "default")}" # template wrap expr
x-default-7 = default(env.MY_UNSET_ENV, "default")
x-default-8 = default(request.query.r, ["default-8"])[0]
}
}
}
Expand Down