From 7af58de0bd255781e888593611973586ee4f0fb1 Mon Sep 17 00:00:00 2001 From: Harshita Shankar Date: Thu, 1 May 2025 20:48:29 +0530 Subject: [PATCH 1/2] add nil check for expectedType and actualType Signed-off-by: Harshita Shankar --- jsondiff.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index c1eb52d..83e0e1e 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -33,20 +33,24 @@ func CompareJSON(expectedJSON []byte, actualJSON []byte, noise map[string][]stri var actualType interface{} if err := json.Unmarshal(expectedJSON, &expectedType); err != nil { - fmt.Println("Error unmarshalling expected JSON") - return Diff{}, err + return Diff{}, fmt.Errorf("error unmarshalling expected JSON: %v", err) } if err := json.Unmarshal(actualJSON, &actualType); err != nil { - fmt.Println("Error unmarshalling actual JSON") - return Diff{}, err + return Diff{}, fmt.Errorf("error unmarshalling actual JSON: %v", err) } // Check if types of expected and actual JSON are the same. + expectedTypeInfo := reflect.TypeOf(expectedType) + actualTypeInfo := reflect.TypeOf(actualType) + + if expectedTypeInfo == nil || actualTypeInfo == nil { + return Diff{}, fmt.Errorf("invalid type information: expectedType=%v, actualType=%v", expectedTypeInfo, actualTypeInfo) + } - if reflect.TypeOf(expectedType) != reflect.TypeOf(actualType) { - expectedJSONString := `Type of expected body: ` + reflect.TypeOf(expectedType).Kind().String() - actualJSONString := `Type of actual body: ` + reflect.TypeOf(actualType).Kind().String() + if expectedTypeInfo != actualTypeInfo { + expectedJSONString := fmt.Sprintf("Type of expected body: %v", expectedTypeInfo.Kind()) + actualJSONString := fmt.Sprintf("Type of actual body: %v", actualTypeInfo.Kind()) offset := []int{4} highlightExpected := color.FgHiRed From 99b6b0b10ff6bbd2530ff9d495a75033aee17739 Mon Sep 17 00:00:00 2001 From: Harshita Shankar Date: Thu, 8 May 2025 14:32:44 +0530 Subject: [PATCH 2/2] fix: sort keys in CompareHeaders for deterministic o/p Signed-off-by: Harshita Shankar --- jsondiff.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index 83e0e1e..fdb535a 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" "regexp" + "sort" "strings" "github.com/fatih/color" @@ -44,8 +45,11 @@ func CompareJSON(expectedJSON []byte, actualJSON []byte, noise map[string][]stri expectedTypeInfo := reflect.TypeOf(expectedType) actualTypeInfo := reflect.TypeOf(actualType) - if expectedTypeInfo == nil || actualTypeInfo == nil { - return Diff{}, fmt.Errorf("invalid type information: expectedType=%v, actualType=%v", expectedTypeInfo, actualTypeInfo) + if expectedTypeInfo == nil { + return Diff{}, fmt.Errorf("invalid type information: expectedType is nil") + } + if actualTypeInfo == nil { + return Diff{}, fmt.Errorf("invalid type information: actualType is nil") } if expectedTypeInfo != actualTypeInfo { @@ -866,11 +870,26 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre func CompareHeaders(expectedHeaders, actualHeaders map[string]string) Diff { var expectAll, actualAll strings.Builder // Builders for the resulting strings. - // Iterate over each key-value pair in the expected map. - for key, expValue := range expectedHeaders { - actValue := actualHeaders[key] // Get the corresponding value from the actual map. + // Get all unique keys from both maps and sort them + keys := make(map[string]bool) + for k := range expectedHeaders { + keys[k] = true + } + for k := range actualHeaders { + keys[k] = true + } + + sortedKeys := make([]string, 0, len(keys)) + for k := range keys { + sortedKeys = append(sortedKeys, k) + } + sort.Strings(sortedKeys) + + // Iterate over sorted keys + for _, key := range sortedKeys { + expValue := expectedHeaders[key] + actValue := actualHeaders[key] - // Calculate the offsets of the differences between the expected and actual values. offsetsStr1, offsetsStr2, _ := diffArrayRange(string(expValue), string(actValue)) // Define colors for highlighting differences.