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: don't overwrite fields set to empty arrays when default exists #374

Merged
merged 2 commits into from
Oct 17, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@

## Unreleased

- Fix a bug preventing users to set fields to emtpy arrays when
a default for those fields exist.
[#374](https://github.com/Kong/go-kong/pull/374)

## [v0.47.0]

> Release date: 2023/08/29
Expand Down
5 changes: 2 additions & 3 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
}
case []interface{}:
if value.Get(fname).Get("elements.type").String() != "record" &&
config[fname] != nil &&
len(config[fname].([]interface{})) > 0 {
// Non empty array with elements which are not of type record
config[fname] != nil {
// Non nil array with elements which are not of type record
// this means field is already set
return true
}
Expand Down
133 changes: 133 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2212,3 +2212,136 @@ func Test_FillPluginsDefaults_Acme(t *testing.T) {
})
}
}

const NonEmptyDefaultArrayFieldSchema = `{
"fields": [
{
"protocols": {
"default": [
"grpc",
"grpcs",
"http",
"https"
],
"elements": {
"len_min": 1,
"one_of": [
"grpc",
"grpcs",
"http",
"https"
],
"required": true,
"type": "string"
},
"required": true,
"type": "set"
}
},
{
"config": {
"fields": [
{
"issuer": {
"required": true,
"type": "string"
}
},
{
"login_tokens": {
"default": [
"id_token"
],
"elements": {
"one_of": [
"id_token",
"access_token",
"refresh_token",
"tokens",
"introspection"
],
"type": "string"
},
"required": false,
"type": "array"
}
}
],
"type": "record"
}
}
]
}`

func Test_FillPluginsDefaults_NonEmptyDefaultArrayField(t *testing.T) {
client, err := NewTestClient(nil, nil)
require.NoError(t, err)
require.NotNil(t, client)

tests := []struct {
name string
plugin *Plugin
expected *Plugin
}{
{
name: "not setting login_tokens should be overwritten by default value",
plugin: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
},
},
expected: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
"login_tokens": []any{"id_token"},
},
},
},
{
name: "setting empty array for login_tokens should not be overwritten by default value",
plugin: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
"login_tokens": []any{},
},
},
expected: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
"login_tokens": []any{},
},
},
},
{
name: "setting non-empty login_tokens should not be overwritten by default value",
plugin: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
"login_tokens": []any{"access_token", "refresh_token"},
},
},
expected: &Plugin{
Config: Configuration{
"issuer": "https://accounts.google.com",
"login_tokens": []any{"access_token", "refresh_token"},
},
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
plugin := tc.plugin
var fullSchema map[string]interface{}
require.NoError(t, json.Unmarshal([]byte(NonEmptyDefaultArrayFieldSchema), &fullSchema))

require.NotNil(t, fullSchema)
assert.NoError(t, FillPluginsDefaults(plugin, fullSchema))
opts := cmpopts.IgnoreFields(*plugin, "Enabled", "Protocols")
if diff := cmp.Diff(plugin, tc.expected, opts); diff != "" {
t.Errorf(diff)
}
})
}
}