From 92b1eeb43b4beb53704ec3e101d0a95b2a1d6b95 Mon Sep 17 00:00:00 2001 From: Henrik Schmidt Date: Tue, 17 Jun 2025 18:13:41 +0200 Subject: [PATCH] Fix ExtractItems to preserve empty maps and lists Previously, ExtractItems converted empty collections ({} and []) to null. This change preserves the distinction between null and empty values. --- typed/remove.go | 8 ++++++++ typed/remove_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/typed/remove.go b/typed/remove.go index 86de5105..ac47ccdf 100644 --- a/typed/remove.go +++ b/typed/remove.go @@ -58,6 +58,10 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) { defer w.allocator.Free(l) // If list is null or empty just return if l == nil || l.Length() == 0 { + // For extraction, we just return the value as is (which is nil or empty). For extraction the difference matters. + if w.shouldExtract { + w.out = w.value.Unstructured() + } return nil } @@ -113,6 +117,10 @@ func (w *removingWalker) doMap(t *schema.Map) ValidationErrors { } // If map is null or empty just return if m == nil || m.Empty() { + // For extraction, we just return the value as is (which is nil or empty). For extraction the difference matters. + if w.shouldExtract { + w.out = w.value.Unstructured() + } return nil } diff --git a/typed/remove_test.go b/typed/remove_test.go index 2cb469dc..e82ce688 100644 --- a/typed/remove_test.go +++ b/typed/remove_test.go @@ -675,6 +675,38 @@ var removeCases = []removeTestCase{{ ), `{"mapOfMapsRecursive":{"a":{"b":null}}}`, `{"mapOfMapsRecursive": {"a":{"b":{"c":null}}}}`, + }, { + // empty list + `{"listOfLists": []}`, + _NS( + _P("listOfLists"), + ), + ``, + `{"listOfLists": []}`, + }, { + // null list + `{"listOfLists": null}`, + _NS( + _P("listOfLists"), + ), + ``, + `{"listOfLists": null}`, + }, { + // empty map + `{"mapOfMaps": {}}`, + _NS( + _P("mapOfMaps"), + ), + ``, + `{"mapOfMaps": {}}`, + }, { + // nil map + `{"mapOfMaps": null}`, + _NS( + _P("mapOfMaps"), + ), + ``, + `{"mapOfMaps": null}`, }}, }}