diff --git a/.chloggen/confmap-fix-non-string-nil-case.yaml b/.chloggen/confmap-fix-non-string-nil-case.yaml new file mode 100644 index 00000000000..7f9a9871640 --- /dev/null +++ b/.chloggen/confmap-fix-non-string-nil-case.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: Fix bug where an unset env var used with a non-string field resulted in a panic + +# One or more tracking issues or pull requests related to the change +issues: [10950] + +# (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: [] diff --git a/confmap/confmap.go b/confmap/confmap.go index 781ad68ef36..a340dabbd1c 100644 --- a/confmap/confmap.go +++ b/confmap/confmap.go @@ -250,13 +250,13 @@ func caseSensitiveMatchName(a, b string) bool { return a == b } -func castTo(exp expandedValue, useOriginal bool) (any, error) { +func castTo(exp expandedValue, useOriginal bool) any { // If the target field is a string, use `exp.Original` or fail if not available. if useOriginal { - return exp.Original, nil + return exp.Original } // Otherwise, use the parsed value (previous behavior). - return exp.Value, nil + return exp.Value } // Check if a reflect.Type is of the form T, where: @@ -284,7 +284,14 @@ func useExpandValue() mapstructure.DecodeHookFuncType { to reflect.Type, data any) (any, error) { if exp, ok := data.(expandedValue); ok { - return castTo(exp, to.Kind() == reflect.String) + v := castTo(exp, to.Kind() == reflect.String) + // See https://github.com/open-telemetry/opentelemetry-collector/issues/10949 + // If the `to.Kind` is not a string, then expandValue's original value is useless and + // the casted-to value will be nil. In that scenario, we need to use the default value of `to`'s kind. + if v == nil { + return reflect.Zero(to).Interface(), nil + } + return v, nil } switch to.Kind() { diff --git a/confmap/internal/e2e/types_test.go b/confmap/internal/e2e/types_test.go index 875fd44bc4c..ed84d4faef5 100644 --- a/confmap/internal/e2e/types_test.go +++ b/confmap/internal/e2e/types_test.go @@ -645,3 +645,14 @@ func TestIssue10937_ComplexType(t *testing.T) { require.NoError(t, err) require.Equal(t, []map[string][]any{{"key": {1234}}}, cfgNotStringy.Field) } + +func TestIssue10949_UnsetVar(t *testing.T) { + resolver := NewResolver(t, "types_expand.yaml") + conf, err := resolver.Resolve(context.Background()) + require.NoError(t, err) + + var cfg TargetConfig[int] + err = conf.Unmarshal(&cfg) + require.NoError(t, err) + require.Equal(t, 0, cfg.Field) +}