Skip to content

Commit

Permalink
Merge branch 'main' into update-gateway-images-in-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Jun 15, 2023
2 parents ce190d0 + 7749a83 commit b4ecec6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 35 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@

> Release date: TBD
Nothing yet.
- Fix: handle empty array as nil when filling record defaults
[#345](https://github.com/Kong/go-kong/pull/345)

## [v0.43.0]

Expand Down
14 changes: 11 additions & 3 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,19 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
frequired := value.Get(fname + ".required")
// Recursively fill defaults only if the field is either required or a subconfig is provided
if ftype.String() == "record" && (config[fname] != nil || (frequired.Exists() && frequired.Bool())) {
var fieldConfig Configuration
subConfig := config[fname]
if subConfig == nil {
subConfig = make(map[string]interface{})
switch subConfig.(type) {
case nil, []interface{}:
// We can encounter an empty array here due to an incorrect yaml
// setting or an empty subconfig (like acme.storage_config.kong).
// This should be treated as nil case.
// TODO: https://konghq.atlassian.net/browse/KOKO-1125
fieldConfig = make(map[string]interface{})
default:
fieldConfig = subConfig.(map[string]interface{})
}
newSubConfig := fillConfigRecord(value.Get(fname), subConfig.(map[string]interface{}))
newSubConfig := fillConfigRecord(value.Get(fname), fieldConfig)
res[fname] = map[string]interface{}(newSubConfig)
return true
}
Expand Down
104 changes: 73 additions & 31 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,51 +1656,92 @@ func TestFillConsumerGroupPluginDefaults(T *testing.T) {
}
}

func Test_fillConfigRecord(t *testing.T) {
tests := []struct {
name string
schema gjson.Result
config Configuration
expected Configuration
}{
{
name: "fills defaults for all missing fields",
schema: gjson.Parse(`{
"fields": {
"config":
{
const fillConfigRecordTestSchema = `{
"fields": {
"config": {
"type": "record",
"fields": [
{
"enabled": {
"type": "boolean",
"default": true,
"required": true
}
},
{
"mappings": {
"required": false,
"type": "array",
"elements": {
"type": "record",
"fields":[
"fields": [
{
"enabled":{
"type":"boolean",
"default":true,
"required":true
"name": {
"type": "string",
"required": false
}
},
{
"mappings":{
"required":false,
"type":"array",
"elements":{
"type":"record",
"fields":[
{"name":{"type":"string","required":false}},
{"nationality":{"type":"string","required":false}}
]
}
"nationality": {
"type": "string",
"required": false
}
}
]
}
}
}`),
},
{
"empty_record": {
"type": "record",
"required": true,
"fields": []
}
}
]
}
}
}
`

func Test_fillConfigRecord(t *testing.T) {
tests := []struct {
name string
schema gjson.Result
config Configuration
expected Configuration
}{
{
name: "fills defaults for all missing fields",
schema: gjson.Parse(fillConfigRecordTestSchema),
config: Configuration{
"mappings": []any{
map[string]any{
"nationality": "Ethiopian",
},
},
},
expected: Configuration{
"enabled": true,
"mappings": []any{
Configuration{
"name": nil,
"nationality": "Ethiopian",
},
},
"empty_record": map[string]any{},
},
},
{
name: "handle empty array as nil for a record field",
schema: gjson.Parse(fillConfigRecordTestSchema),
config: Configuration{
"mappings": []interface{}{
map[string]interface{}{
"mappings": []any{
map[string]any{
"nationality": "Ethiopian",
},
},
"empty_record": map[string]any{},
},
expected: Configuration{
"enabled": true,
Expand All @@ -1710,6 +1751,7 @@ func Test_fillConfigRecord(t *testing.T) {
"nationality": "Ethiopian",
},
},
"empty_record": map[string]any{},
},
},
}
Expand Down

0 comments on commit b4ecec6

Please sign in to comment.