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 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
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()`](./docs/REFERENCE.md#functions) 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
2 changes: 1 addition & 1 deletion docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ To access the HTTP status code of the `default` response use `backend_responses.
| :----------------------------- | :-------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------- | :--------------------------------------------------- |
| `base64_decode` | string | Decodes Base64 data, as specified in RFC 4648. | `encoded` (string) | `base64_decode("Zm9v")` |
| `base64_encode` | string | Encodes Base64 data, as specified in RFC 4648. | `decoded` (string) | `base64_encode("foo")` |
| `default` | string | Returns the first of the given arguments that is not null. | `arg...` (various) | `default(request.cookies.foo, "bar")` |
| `default` | string | Returns the first of the given arguments that is not null or an empty string. If no argument matches, the last argument is returned. | `arg...` (various) | `default(request.cookies.foo, "bar")` |
| `json_decode` | various | Parses the given JSON string and, if it is valid, returns the value it represents. | `encoded` (string) | `json_decode("{\"foo\": 1}")` |
| `json_encode` | string | Returns a JSON serialization of the given value. | `val` (various) | `json_encode(request.context.myJWT)` |
| `jwt_sign` | string | jwt_sign creates and signs a JSON Web Token (JWT) from information from a referenced [JWT Signing Profile Block](#jwt-signing-profile-block) (or [JWT Block](#jwt-block) with `signing_ttl`) and additional claims provided as a function parameter. | `label` (string), `claims` (object) | `jwt_sign("myJWT")` |
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
9 changes: 7 additions & 2 deletions 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,8 +32,13 @@ 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")
return args[len(args)-1], nil
},
})
17 changes: 15 additions & 2 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3694,7 +3694,20 @@ 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",
"X-Default-9": "",
"X-Default-10": "",
"X-Default-11": "0",
"X-Default-12": "",
}, http.StatusOK},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand All @@ -3711,7 +3724,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
6 changes: 6 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,12 @@ 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]
x-default-9 = default(request.cookies.undef, "")
x-default-10 = default(request.cookies.undef, request.cookies.undef)
x-default-11 = default(request.cookies.undef, 0)
x-default-12 = default(request.cookies.undef, false)
}
}
}
Expand Down