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 nil pointer panic when calculating patches #1768

Merged
merged 1 commit into from
Oct 11, 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
19 changes: 18 additions & 1 deletion provider/pkg/resources/patching.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ func CalcPatch(oldInputs resource.PropertyMap, newInputs resource.PropertyMap, s
createOnlyProps := codegen.NewStringSet(spec.CreateOnly...)
writeOnlyProps := codegen.NewStringSet(spec.WriteOnly...)
mustSendProps := writeOnlyProps.Subtract(createOnlyProps)

isDiffEmpty := diff == nil
if isDiffEmpty {
// If there's no diff and no write-only properties to send, we can return an empty patch.
if len(mustSendProps) == 0 {
return []jsonpatch.JsonPatchOperation{}, nil
}

// Init the diff with empty values so we can safely access it later
diff = &resource.ObjectDiff{
Adds: make(resource.PropertyMap),
Deletes: make(resource.PropertyMap),
Sames: make(resource.PropertyMap),
Updates: make(map[resource.PropertyKey]resource.ValueDiff),
}
}

for _, writeOnlyPropName := range mustSendProps.SortedValues() {
propKey := resource.PropertyKey(writeOnlyPropName)
if _, ok := diff.Sames[propKey]; ok {
if _, ok := diff.Sames[propKey]; ok || isDiffEmpty {
delete(diff.Sames, propKey)
diff.Adds[propKey] = newInputs[propKey]
}
Expand Down
22 changes: 22 additions & 0 deletions provider/pkg/resources/patching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ func TestCalcPatch(t *testing.T) {
}})
assert.Empty(t, patch)
})
t.Run("no diff with must send props", func(t *testing.T) {
expected := []jsonpatch.JsonPatchOperation{
{Operation: "add", Path: "/Prop1", Value: "1"},
}
patch := run(t, args{
oldInputs: resource.PropertyMap{
"prop1": resource.NewStringProperty("1"),
"prop2": resource.NewStringProperty("2"),
},
newInputs: resource.PropertyMap{
"prop1": resource.NewStringProperty("1"),
"prop2": resource.NewStringProperty("2"),
},
spec: metadata.CloudAPIResource{
Inputs: map[string]schema.PropertySpec{
"prop1": {TypeSpec: schema.TypeSpec{Type: "string"}},
"prop2": {TypeSpec: schema.TypeSpec{Type: "string"}},
},
WriteOnly: []string{"prop1"},
}})
assert.Equal(t, expected, patch)
})
t.Run("always sends write-only properties", func(t *testing.T) {
expected := []jsonpatch.JsonPatchOperation{
{Operation: "add", Path: "/Prop1", Value: "1"},
Expand Down
Loading