Skip to content

Commit

Permalink
[confmap] Fix for unset env var setting non-string field. (#10950)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Still working on making a new test for this scenario, but all existing
tests pass.

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes
#10949

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Manual testing showed the bug resolved, but I'd still like to get an e2e
test to check it.

---------

Co-authored-by: Pablo Baeyens <[email protected]>
  • Loading branch information
TylerHelmuth and mx-psi authored Aug 26, 2024
1 parent 17b39d1 commit e375da4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .chloggen/confmap-fix-non-string-nil-case.yaml
Original file line number Diff line number Diff line change
@@ -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: []
15 changes: 11 additions & 4 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit e375da4

Please sign in to comment.