Skip to content

Commit e375da4

Browse files
TylerHelmuthmx-psi
andauthored
[confmap] Fix for unset env var setting non-string field. (#10950)
<!--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]>
1 parent 17b39d1 commit e375da4

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix bug where an unset env var used with a non-string field resulted in a panic
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10950]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

confmap/confmap.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,13 @@ func caseSensitiveMatchName(a, b string) bool {
250250
return a == b
251251
}
252252

253-
func castTo(exp expandedValue, useOriginal bool) (any, error) {
253+
func castTo(exp expandedValue, useOriginal bool) any {
254254
// If the target field is a string, use `exp.Original` or fail if not available.
255255
if useOriginal {
256-
return exp.Original, nil
256+
return exp.Original
257257
}
258258
// Otherwise, use the parsed value (previous behavior).
259-
return exp.Value, nil
259+
return exp.Value
260260
}
261261

262262
// Check if a reflect.Type is of the form T, where:
@@ -284,7 +284,14 @@ func useExpandValue() mapstructure.DecodeHookFuncType {
284284
to reflect.Type,
285285
data any) (any, error) {
286286
if exp, ok := data.(expandedValue); ok {
287-
return castTo(exp, to.Kind() == reflect.String)
287+
v := castTo(exp, to.Kind() == reflect.String)
288+
// See https://github.com/open-telemetry/opentelemetry-collector/issues/10949
289+
// If the `to.Kind` is not a string, then expandValue's original value is useless and
290+
// the casted-to value will be nil. In that scenario, we need to use the default value of `to`'s kind.
291+
if v == nil {
292+
return reflect.Zero(to).Interface(), nil
293+
}
294+
return v, nil
288295
}
289296

290297
switch to.Kind() {

confmap/internal/e2e/types_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,14 @@ func TestIssue10937_ComplexType(t *testing.T) {
645645
require.NoError(t, err)
646646
require.Equal(t, []map[string][]any{{"key": {1234}}}, cfgNotStringy.Field)
647647
}
648+
649+
func TestIssue10949_UnsetVar(t *testing.T) {
650+
resolver := NewResolver(t, "types_expand.yaml")
651+
conf, err := resolver.Resolve(context.Background())
652+
require.NoError(t, err)
653+
654+
var cfg TargetConfig[int]
655+
err = conf.Unmarshal(&cfg)
656+
require.NoError(t, err)
657+
require.Equal(t, 0, cfg.Field)
658+
}

0 commit comments

Comments
 (0)