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: ensure defaults are properly filled for a set #333

Merged
merged 2 commits into from
Jun 6, 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
3 changes: 2 additions & 1 deletion kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
// If this array is non-nil and non-empty (in Config), go through all the records in this array and add defaults
// If the array has only primitives like string/number/boolean then the value is already set
// If the array is empty or nil, then no defaults need to be set for its elements
if ftype.String() == "array" {
// The same logic should be applied if field is of type set of records (in Schema)
if ftype.String() == "array" || ftype.String() == "set" {
if value.Get(fname).Get("elements.type").String() == "record" {
if config[fname] != nil {
// Check sub config is of type array and it is non-empty
Expand Down
162 changes: 162 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,47 @@ const StatsDSchema = `{
]
}`

// TestSchemaSetType (
const TestSchemaSetType = `{
"fields": [{
"config": {
"type": "record",
"fields": [{
"bootstrap_servers": {
"default": [
{
"host": "127.0.0.1",
"port": 42
}
],
"elements": {
"fields": [{
"host": {
"required": true,
"type": "string"
}
},
{
"port": {
"between": [
0,
65535
],
"required": true,
"type": "integer",
"default": 42
}
}
],
"type": "record"
},
"type": "set"
}
}]
}
}]
}`

func TestStringArrayToString(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -1509,3 +1550,124 @@ func Test_FillPluginsDefaults_RequestTransformer(t *testing.T) {
})
}
}

func Test_FillPluginsDefaults_SetType(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: "does not fill defaults when provided",
plugin: &Plugin{
Config: Configuration{
"bootstrap_servers": []interface{}{
map[string]interface{}{
"host": "192.168.2.100",
"port": float64(3500),
},
map[string]any{
"host": "192.168.2.101",
"port": float64(3500),
},
},
},
},
expected: &Plugin{
Config: Configuration{
"bootstrap_servers": []interface{}{
Configuration{
"host": "192.168.2.100",
"port": float64(3500),
},
Configuration{
"host": "192.168.2.101",
"port": float64(3500),
},
},
},
},
},
{
name: "fills defaults for all missing fields",
plugin: &Plugin{
Config: Configuration{
"bootstrap_servers": []interface{}{
map[string]interface{}{
"host": "127.0.0.1",
},
},
},
},
expected: &Plugin{
Config: Configuration{
"bootstrap_servers": []interface{}{
Configuration{
"host": "127.0.0.1",
"port": float64(42),
},
},
},
},
},
{
name: "fills defaults when empty set of records in config",
plugin: &Plugin{
Config: Configuration{
"bootstrap_servers": []any{},
},
},
expected: &Plugin{
Config: Configuration{
"bootstrap_servers": []any{
map[string]any{
"host": "127.0.0.1",
"port": float64(42),
},
},
},
},
},
{
name: "fills defaults when nil set of records in config",
plugin: &Plugin{
Config: Configuration{
"bootstrap_servers": nil,
},
},
expected: &Plugin{
Config: Configuration{
"bootstrap_servers": []any{
map[string]any{
"host": "127.0.0.1",
"port": float64(42),
},
},
},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
plugin := tc.plugin

var fullSchema map[string]interface{}
err := json.Unmarshal([]byte(TestSchemaSetType), &fullSchema)
require.NoError(t, err)
require.NotNil(t, fullSchema)

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