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(utils): do not fill empty records #467

Merged
merged 1 commit into from
Sep 12, 2024
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
7 changes: 6 additions & 1 deletion kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,12 @@ func fillConfigRecord(schema gjson.Result, config Configuration, opts FillRecord
fieldConfig = subConfig.(map[string]interface{})
}
newSubConfig := fillConfigRecord(value.Get(fname), fieldConfig, opts)
res[fname] = map[string]interface{}(newSubConfig)
// When we are not filling defaults, only assign the subconfig if it's not empty.
// This is to avoid having records that are assigned empty map values when defaults
// are not supposed to be filled.
if opts.FillDefaults || len(newSubConfig) > 0 {
res[fname] = map[string]interface{}(newSubConfig)
}
return true
}

Expand Down
56 changes: 56 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,48 @@ const fillConfigRecordTestSchemaWithAutoFields = `{
}
`

const fillConfigRecordTestSchemaWithRecord = `{
"fields": {
"config": {
"type": "record",
"fields": [
{
"some_record": {
"required": true,
"fields": [
{
"some_field": {
"default": "kong",
"type": "string"
}
}
],
"type": "record"
}
},
{
"some_other_record": {
"fields": [
{
"some_field": {
"type": "string"
}
}
],
"type": "record"
}
},
{
"string_1": {
"type": "string",
}
}
]
}
}
}
`

func Test_fillConfigRecord_shorthand_fields(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -2098,6 +2140,20 @@ func Test_fillConfigRecord_auto_only(t *testing.T) {
// defalt_string missing
},
},
{
name: "not passing record field leaves field unset",
schema: gjson.Parse(fillConfigRecordTestSchemaWithRecord),
config: Configuration{
// some_record missing
"some_other_record": map[string]any{}, // explicitly set to empty record
"string_1": "abc",
},
expected: Configuration{
// some_record was not filled
"some_other_record": map[string]any{}, // empty record remained unchanged
"string_1": "abc",
},
},
}

for _, tc := range tests {
Expand Down
Loading