Skip to content

Commit 2ff4b31

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 2ff4b31

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@ import (
66
jsoniter "github.com/json-iterator/go"
77
)
88

9-
func deleteStatusAndTidyMetadata(obj []byte) (map[string]interface{}, error) {
10-
var objectMap map[string]interface{}
9+
func deleteStatusAndTidyMetadata(obj []byte) (map[string]any, error) {
10+
var objectMap map[string]any
1111
err := jsoniter.Unmarshal(obj, &objectMap)
1212
if err != nil {
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]any)
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 {
29-
annotations := a.(map[string]interface{})
34+
annotations := a.(map[string]any)
3035
delete(annotations, "meta.helm.sh/release-name")
3136
delete(annotations, "meta.helm.sh/release-namespace")
3237
delete(annotations, "deployment.kubernetes.io/revision")

manifest/util_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func Test_deleteStatusAndTidyMetadata(t *testing.T) {
1010
tests := []struct {
1111
name string
1212
obj []byte
13-
want map[string]interface{}
13+
want map[string]any
1414
wantErr bool
1515
}{
1616
{
@@ -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(`
@@ -57,21 +63,21 @@ func Test_deleteStatusAndTidyMetadata(t *testing.T) {
5763
}
5864
}
5965
`),
60-
want: map[string]interface{}{
66+
want: map[string]any{
6167
"apiVersion": "apps/v1",
6268
"kind": "Deployment",
63-
"metadata": map[string]interface{}{
64-
"annotations": map[string]interface{}{
69+
"metadata": map[string]any{
70+
"annotations": map[string]any{
6571
"other-annot": "value",
6672
},
6773
"name": "nginx-deployment",
6874
"namespace": "test-ns",
6975
},
70-
"spec": map[string]interface{}{
71-
"template": map[string]interface{}{
72-
"spec": map[string]interface{}{
73-
"containers": []interface{}{
74-
map[string]interface{}{
76+
"spec": map[string]any{
77+
"template": map[string]any{
78+
"spec": map[string]any{
79+
"containers": []any{
80+
map[string]any{
7581
"image": "nginx:1.14.2",
7682
"imagePullPolicy": "IfNotPresent",
7783
"name": "nginx",

0 commit comments

Comments
 (0)