Skip to content

Commit

Permalink
APIGOV-28691 - fix to not serialize deleted subresources (#857)
Browse files Browse the repository at this point in the history
* APIGOV-28691 - fix to not serialize deleted subresources

* APIGOV-28691 - fix test
  • Loading branch information
vivekschauhan authored Nov 12, 2024
1 parent 92bbe14 commit 727f972
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/apic/apiserver/clients/api/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ func TestUpdateMerge(t *testing.T) {
Tags: []string{"old"},
},
}
oldAPISvcRI, _ := oldAPISvc.AsInstance()

newAPISvc := &management.APIService{
ResourceMeta: v1.ResourceMeta{
Expand All @@ -581,6 +582,7 @@ func TestUpdateMerge(t *testing.T) {
Tags: []string{"new"},
},
}
newAPISvcRI, _ := newAPISvc.AsInstance()

thisBytes, _ := json.Marshal(newAPISvc)
t.Log(string(thisBytes))
Expand All @@ -598,7 +600,7 @@ func TestUpdateMerge(t *testing.T) {
Tags: []string{"old", "new"},
},
}

mergedTagsRI, _ := mergedTags.AsInstance()
mergeError := fmt.Errorf("merge errror")

getError := InternalServerError{
Expand Down Expand Up @@ -626,7 +628,7 @@ func TestUpdateMerge(t *testing.T) {
return new, nil
},
expectedErr: nil,
expectedResource: newAPISvc,
expectedResource: newAPISvcRI,
},
{
name: "overwriting update",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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())

Expand Down
13 changes: 13 additions & 0 deletions pkg/apic/apiserver/models/api/v1/resourceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 727f972

Please sign in to comment.