Skip to content
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: 2 additions & 2 deletions pkg/json/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func MergeAPIExtensionsJSON(a, b *apiextensionsv1.JSON) (*apiextensionsv1.JSON,
}

aMap := map[string]interface{}{}
if a != nil {
if a != nil && len(a.Raw) > 0 {
if err := json.Unmarshal(a.Raw, &aMap); err != nil {
return nil, fmt.Errorf("unmarshalling first JSON variable: %w", err)
}
}

var bMap map[string]interface{}
if b != nil {
if b != nil && len(b.Raw) > 0 {
if err := json.Unmarshal(b.Raw, &bMap); err != nil {
return nil, fmt.Errorf("unmarshalling second JSON variable: %w", err)
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/json/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ func TestMergeMaps(t *testing.T) {
Raw: nil,
},
},
{
name: "handle non-nil struct with nil Raw (first arg)",
a: &apiextensionsv1.JSON{},
b: nil,
expected: &apiextensionsv1.JSON{
Raw: []byte(`{}`),
},
},
{
name: "handle non-nil struct with nil Raw (second arg)",
a: nil,
b: &apiextensionsv1.JSON{},
expected: &apiextensionsv1.JSON{
Raw: []byte(`{}`),
},
},
{
name: "handle both non-nil structs with nil Raw",
a: &apiextensionsv1.JSON{},
b: &apiextensionsv1.JSON{},
expected: &apiextensionsv1.JSON{
Raw: []byte(`{}`),
},
},
{
name: "handle non-nil struct with empty Raw bytes",
a: &apiextensionsv1.JSON{Raw: []byte{}},
b: &apiextensionsv1.JSON{Raw: []byte{}},
expected: &apiextensionsv1.JSON{
Raw: []byte(`{}`),
},
},
}

for _, tc := range stringStringCases {
Expand Down