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] diff --git a/confmap/confmap.go b/confmap/confmap.go index c21bcc61fc1..9b541e872dd 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)) } diff --git a/confmap/confmaptest/configtest_test.go b/confmap/confmaptest/configtest_test.go index cf7717071cf..33c98d2f665 100644 --- a/confmap/confmaptest/configtest_test.go +++ b/confmap/confmaptest/configtest_test.go @@ -30,11 +30,16 @@ func TestLoadConf(t *testing.T) { assert.Equal(t, map[string]any{"floating": 3.14}, cfg.ToStringMap()) } -func TestToStringMapSanitizeEmptySlice(t *testing.T) { +func TestToStringMapSanitizeNil(t *testing.T) { + cfg, err := LoadConf(filepath.Join("testdata", "nil.yaml")) + require.NoError(t, err) + assert.Equal(t, map[string]any{"slice": nil}, cfg.ToStringMap()) +} + +func TestToStringMapEmptySlice(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": []any{}}, 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: [] diff --git a/confmap/confmaptest/testdata/nil.yaml b/confmap/confmaptest/testdata/nil.yaml new file mode 100644 index 00000000000..d24c2453dc0 --- /dev/null +++ b/confmap/confmaptest/testdata/nil.yaml @@ -0,0 +1 @@ +slice: