Skip to content

Commit a4d8b12

Browse files
committed
fix: handle null input in deleteStatusAndTidyMetadata
- Return nil without panic when JSON input is null - Safely handle missing metadata field with type assertion - Add test case for null JSON input Signed-off-by: yxxhero <aiopsclub@163.com>
1 parent f5b13c2 commit a4d8b12

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

manifest/generate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ func cleanMetadataForPatch(data []byte) ([]byte, error) {
248248
if err != nil {
249249
return nil, err
250250
}
251+
if objMap == nil {
252+
return []byte("null"), nil
253+
}
251254
return json.Marshal(objMap)
252255
}
253256

manifest/util.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ func deleteStatusAndTidyMetadata(obj []byte) (map[string]interface{}, error) {
1313
return nil, fmt.Errorf("could not unmarshal byte sequence: %w", err)
1414
}
1515

16+
if objectMap == nil {
17+
return nil, nil
18+
}
19+
1620
delete(objectMap, "status")
1721

18-
metadata := objectMap["metadata"].(map[string]interface{})
22+
metadata, ok := objectMap["metadata"].(map[string]interface{})
23+
if !ok {
24+
return objectMap, nil
25+
}
1926

2027
delete(metadata, "managedFields")
2128
delete(metadata, "generation")
2229
delete(metadata, "creationTimestamp")
2330
delete(metadata, "resourceVersion")
2431
delete(metadata, "uid")
2532

26-
// See the below for the goal of this metadata tidy logic.
27-
// https://github.com/databus23/helm-diff/issues/326#issuecomment-1008253274
2833
if a := metadata["annotations"]; a != nil {
2934
annotations := a.(map[string]interface{})
3035
delete(annotations, "meta.helm.sh/release-name")

manifest/util_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func Test_deleteStatusAndTidyMetadata(t *testing.T) {
1919
want: nil,
2020
wantErr: true,
2121
},
22+
{
23+
name: "null json",
24+
obj: []byte("null"),
25+
want: nil,
26+
wantErr: false,
27+
},
2228
{
2329
name: "valid json",
2430
obj: []byte(`

0 commit comments

Comments
 (0)