From 727f972f1fe43b5e167a204861b5e32754df2abf Mon Sep 17 00:00:00 2001 From: Vivek Singh Chauhan Date: Tue, 12 Nov 2024 04:26:45 -0700 Subject: [PATCH] APIGOV-28691 - fix to not serialize deleted subresources (#857) * APIGOV-28691 - fix to not serialize deleted subresources * APIGOV-28691 - fix test --- pkg/apic/apiserver/clients/api/v1/client_test.go | 16 +++++++++------- .../apiserver/models/api/v1/resourceinstance.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/apic/apiserver/clients/api/v1/client_test.go b/pkg/apic/apiserver/clients/api/v1/client_test.go index 928a8985c..e549c9426 100644 --- a/pkg/apic/apiserver/clients/api/v1/client_test.go +++ b/pkg/apic/apiserver/clients/api/v1/client_test.go @@ -562,6 +562,7 @@ func TestUpdateMerge(t *testing.T) { Tags: []string{"old"}, }, } + oldAPISvcRI, _ := oldAPISvc.AsInstance() newAPISvc := &management.APIService{ ResourceMeta: v1.ResourceMeta{ @@ -581,6 +582,7 @@ func TestUpdateMerge(t *testing.T) { Tags: []string{"new"}, }, } + newAPISvcRI, _ := newAPISvc.AsInstance() thisBytes, _ := json.Marshal(newAPISvc) t.Log(string(thisBytes)) @@ -598,7 +600,7 @@ func TestUpdateMerge(t *testing.T) { Tags: []string{"old", "new"}, }, } - + mergedTagsRI, _ := mergedTags.AsInstance() mergeError := fmt.Errorf("merge errror") getError := InternalServerError{ @@ -626,7 +628,7 @@ func TestUpdateMerge(t *testing.T) { return new, nil }, expectedErr: nil, - expectedResource: newAPISvc, + expectedResource: newAPISvcRI, }, { name: "overwriting update", @@ -638,11 +640,11 @@ func TestUpdateMerge(t *testing.T) { return new, nil }, expectedErr: nil, - expectedResource: newAPISvc, + expectedResource: newAPISvcRI, }, { name: "merging tags update", - getResponse: oldAPISvc, + getResponse: oldAPISvcRI, newResource: newAPISvc, getStatus: 200, otherStatus: 200, @@ -653,11 +655,10 @@ func TestUpdateMerge(t *testing.T) { } f.SetTags(append(f.GetTags(), new.GetTags()...)) - return f, nil }, expectedErr: nil, - expectedResource: mergedTags, + expectedResource: mergedTagsRI, }, { name: "merge error", @@ -682,7 +683,8 @@ func TestUpdateMerge(t *testing.T) { }, expectedErr: getError, expectedResource: nil, - }} + }, + } logger := WithLogger(noOpLogger{}) c, err := NewClient("http://localhost:8080/apis", logger).ForKind(management.APIServiceGVK()) diff --git a/pkg/apic/apiserver/models/api/v1/resourceinstance.go b/pkg/apic/apiserver/models/api/v1/resourceinstance.go index 918fba120..f04dd8caf 100644 --- a/pkg/apic/apiserver/models/api/v1/resourceinstance.go +++ b/pkg/apic/apiserver/models/api/v1/resourceinstance.go @@ -93,11 +93,24 @@ func (ri *ResourceInstance) MarshalJSON() ([]byte, error) { rawInstance["spec"] = ri.Spec rawInstance["owner"] = ri.Owner + keysToDelete := make([]string, 0) + for key := range rawStruct { + _, ok := rawInstance[key] + if !ok { + keysToDelete = append(keysToDelete, key) + } + } + // override the rawStruct map with the values from the rawInstance map for key, value := range rawInstance { rawStruct[key] = value } + // remove deleted sub-resources + for _, key := range keysToDelete { + delete(rawStruct, key) + } + // return the marshal of the rawStruct return json.Marshal(rawStruct) }