From 4d2305260a83e388baaad26a8affcfbaa1f19b3d Mon Sep 17 00:00:00 2001 From: Januth Nimnal Date: Sun, 27 Sep 2026 07:22:28 +0530 Subject: [PATCH] fix: keep nil map values in EqualExportedValues --- assert/assertions.go | 9 +++++++++ assert/assertions_test.go | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/assert/assertions.go b/assert/assertions.go index 166f63726..889aedbe6 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -136,6 +136,15 @@ func copyExportedFields(expected interface{}) interface{} { for _, k := range expectedValue.MapKeys() { index := expectedValue.MapIndex(k) unexportedRemoved := copyExportedFields(index.Interface()) + if unexportedRemoved == nil { + // reflect.ValueOf(nil) is the zero Value, which + // SetMapIndex interprets as "delete this key". Store the + // element type's zero value instead so nil map values are + // preserved and the entry keeps taking part in the + // comparison. + result.SetMapIndex(k, reflect.Zero(expectedType.Elem())) + continue + } result.SetMapIndex(k, reflect.ValueOf(unexportedRemoved)) } return result.Interface() diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 11642e096..7c41431b8 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -508,6 +508,29 @@ func TestEqualExportedValues(t *testing.T) { + Exported: (int) 2, notExported: (interface {}) `, }, + // A map entry holding a nil interface value is still an entry: the + // key must not be dropped while copying, otherwise maps with + // different keys would wrongly compare as equal. + { + value1: map[string]interface{}{"a": nil}, + value2: map[string]interface{}{"a": nil}, + expectedEqual: true, + }, + { + value1: map[string]interface{}{"a": nil}, + value2: map[string]interface{}{}, + expectedEqual: false, + }, + { + value1: map[string]interface{}{"a": nil, "b": 1}, + value2: map[string]interface{}{"b": 1}, + expectedEqual: false, + }, + { + value1: map[string]interface{}{"a": nil}, + value2: map[string]interface{}{"a": 1}, + expectedEqual: false, + }, } for _, c := range cases {