diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-04-01 14:29:30 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-04-01 14:29:32 +0200 |
| commit | ca7ccbadbc19c2cb28f1b7edfcc845b9d7888adc (patch) | |
| tree | 78722261410ff806736b3f249cb81bc5732b15d4 | |
| parent | e21e702017a5da3224c93caedf0d51b062627311 (diff) | |
| download | go-gg-ca7ccbadbc19c2cb28f1b7edfcc845b9d7888adc.tar.gz | |
jsonmatch: fix destructuring of Array
| -rw-r--r-- | jsonmatch/diff_test.go | 19 | ||||
| -rw-r--r-- | jsonmatch/machinery.go | 2 |
2 files changed, 20 insertions, 1 deletions
diff --git a/jsonmatch/diff_test.go b/jsonmatch/diff_test.go index f0f4fdd..a59d304 100644 --- a/jsonmatch/diff_test.go +++ b/jsonmatch/diff_test.go @@ -56,7 +56,7 @@ func TestCanonicalizesActualPayload(t *testing.T) { "data": jsonmatch.Object{ "foo": 42, "bar": "hello world", - "qux": []any{5, nil, 15}, + "qux": jsonmatch.Array{5, nil, 15}, }, } AssertEqual(t, match.DiffAgainst(message), nil) @@ -390,3 +390,20 @@ type unmarshalableObject struct{} func (unmarshalableObject) MarshalJSON() ([]byte, error) { return nil, errors.New("this object is unmarshalable") } + +func TestArrayOnArrayAction(t *testing.T) { + message := []byte(`[[1,3]]`) // but we assert against [[1,2]] + expected := []jsonmatch.Diff{{ + Kind: "value mismatch", + Pointer: "/0/1", + ExpectedJSON: "2", + ActualJSON: "3", + }} + + match := jsonmatch.Array{[]any{1, 2}} + AssertEqual(t, match.DiffAgainst(message), expected) + + // this used to fail before the fix in the commit that added this test + match = jsonmatch.Array{jsonmatch.Array{1, 2}} + AssertEqual(t, match.DiffAgainst(message), expected) +} diff --git a/jsonmatch/machinery.go b/jsonmatch/machinery.go index 172c82a..7f91f67 100644 --- a/jsonmatch/machinery.go +++ b/jsonmatch/machinery.go @@ -130,6 +130,8 @@ func getDiffsForValue(path []pathElement, expected, actual any) []Diff { return getDiffsForObject(path, expected, actual) case []any: return getDiffsForArray(path, expected, actual) + case Array: + return getDiffsForArray(path, expected, actual) case []map[string]any: downcasted := make([]any, len(expected)) for idx, val := range expected { |
