From 400205f95722436ccf3446115d67c2dcdb8d7e80 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:31:13 -0500 Subject: [PATCH 1/6] Differentiate Between Nil And Empty Slice Signed-off-by: Mahad Zaryab --- confmap/confmap.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/confmap/confmap.go b/confmap/confmap.go index a7a99d10352..722890920cb 100644 --- a/confmap/confmap.go +++ b/confmap/confmap.go @@ -133,6 +133,10 @@ func sanitizeExpanded(a any, useOriginal bool) any { return c case []any: var newSlice []any + if m == nil { + return newSlice + } + newSlice = []any{} for _, e := range m { newSlice = append(newSlice, sanitizeExpanded(e, useOriginal)) } From aadb6b6fc8082206f859323269623e4106e876f6 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:31:25 -0500 Subject: [PATCH 2/6] Fix Unit Test Signed-off-by: Mahad Zaryab --- confmap/confmaptest/configtest_test.go | 3 +-- confmap/confmaptest/testdata/empty-slice.yaml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/confmap/confmaptest/configtest_test.go b/confmap/confmaptest/configtest_test.go index cf7717071cf..0ae4009c8eb 100644 --- a/confmap/confmaptest/configtest_test.go +++ b/confmap/confmaptest/configtest_test.go @@ -33,8 +33,7 @@ func TestLoadConf(t *testing.T) { func TestToStringMapSanitizeEmptySlice(t *testing.T) { cfg, err := LoadConf(filepath.Join("testdata", "empty-slice.yaml")) require.NoError(t, err) - var nilSlice []interface{} - assert.Equal(t, map[string]any{"slice": nilSlice}, cfg.ToStringMap()) + assert.Equal(t, map[string]any{"slice": []interface{}{}}, cfg.ToStringMap()) } func TestValidateProviderScheme(t *testing.T) { diff --git a/confmap/confmaptest/testdata/empty-slice.yaml b/confmap/confmaptest/testdata/empty-slice.yaml index ec9c7c062ae..797254b64a9 100644 --- a/confmap/confmaptest/testdata/empty-slice.yaml +++ b/confmap/confmaptest/testdata/empty-slice.yaml @@ -1 +1 @@ -slice: [] # empty slices are sanitized to nil in ToStringMap +slice: [] From 0436e81e9eb8a8494ff21f419431bb398dc7688a Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:39:41 -0500 Subject: [PATCH 3/6] Add Test For Nil Entry Signed-off-by: Mahad Zaryab --- confmap/confmaptest/configtest_test.go | 7 +++++++ confmap/confmaptest/testdata/nil.yaml | 1 + 2 files changed, 8 insertions(+) create mode 100644 confmap/confmaptest/testdata/nil.yaml diff --git a/confmap/confmaptest/configtest_test.go b/confmap/confmaptest/configtest_test.go index 0ae4009c8eb..8f860fb6a1e 100644 --- a/confmap/confmaptest/configtest_test.go +++ b/confmap/confmaptest/configtest_test.go @@ -30,6 +30,13 @@ func TestLoadConf(t *testing.T) { assert.Equal(t, map[string]any{"floating": 3.14}, cfg.ToStringMap()) } +func TestToStringMapSanitizeNil(t *testing.T) { + cfg, err := LoadConf(filepath.Join("testdata", "nil.yaml")) + require.NoError(t, err) + var nilVal interface{} + assert.Equal(t, map[string]any{"slice": nilVal}, cfg.ToStringMap()) +} + func TestToStringMapSanitizeEmptySlice(t *testing.T) { cfg, err := LoadConf(filepath.Join("testdata", "empty-slice.yaml")) require.NoError(t, err) diff --git a/confmap/confmaptest/testdata/nil.yaml b/confmap/confmaptest/testdata/nil.yaml new file mode 100644 index 00000000000..7726ccbfc15 --- /dev/null +++ b/confmap/confmaptest/testdata/nil.yaml @@ -0,0 +1 @@ +slice: \ No newline at end of file From c9b7c4627ff798d252db3482fe70c9fe8c6284a6 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:40:22 -0500 Subject: [PATCH 4/6] Generate Changelog Entry Signed-off-by: Mahad Zaryab --- .chloggen/nil-empty-slice.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/nil-empty-slice.yaml diff --git a/.chloggen/nil-empty-slice.yaml b/.chloggen/nil-empty-slice.yaml new file mode 100644 index 00000000000..de44e55f0fb --- /dev/null +++ b/.chloggen/nil-empty-slice.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'bug_fix' + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confmap + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Correctly differentiate between a nil and empty slice in `ToStringMap`" + +# One or more tracking issues or pull requests related to the change +issues: [11749] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] From b5fd43b0e35f0bd07e3f01ac905ff9b44d75240e Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:44:57 -0500 Subject: [PATCH 5/6] Add Missing Newline Signed-off-by: Mahad Zaryab --- confmap/confmaptest/testdata/nil.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/confmap/confmaptest/testdata/nil.yaml b/confmap/confmaptest/testdata/nil.yaml index 7726ccbfc15..d24c2453dc0 100644 --- a/confmap/confmaptest/testdata/nil.yaml +++ b/confmap/confmaptest/testdata/nil.yaml @@ -1 +1 @@ -slice: \ No newline at end of file +slice: From 370bf34a748a4e4881940bf39fd25b87dab349b1 Mon Sep 17 00:00:00 2001 From: Mahad Zaryab Date: Tue, 26 Nov 2024 08:46:15 -0500 Subject: [PATCH 6/6] Simplify Test Signed-off-by: Mahad Zaryab --- confmap/confmaptest/configtest_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/confmap/confmaptest/configtest_test.go b/confmap/confmaptest/configtest_test.go index 8f860fb6a1e..f746eca72dd 100644 --- a/confmap/confmaptest/configtest_test.go +++ b/confmap/confmaptest/configtest_test.go @@ -33,8 +33,7 @@ func TestLoadConf(t *testing.T) { func TestToStringMapSanitizeNil(t *testing.T) { cfg, err := LoadConf(filepath.Join("testdata", "nil.yaml")) require.NoError(t, err) - var nilVal interface{} - assert.Equal(t, map[string]any{"slice": nilVal}, cfg.ToStringMap()) + assert.Equal(t, map[string]any{"slice": nil}, cfg.ToStringMap()) } func TestToStringMapSanitizeEmptySlice(t *testing.T) {