Prevent shadowning of keys when a nested key's value is nil.
diff --git a/viper.go b/viper.go
index f17790e..bad121e 100644
--- a/viper.go
+++ b/viper.go
@@ -802,8 +802,10 @@
if source != nil {
if reflect.TypeOf(source).Kind() == reflect.Map {
val := v.searchMap(cast.ToStringMap(source), path[1:])
- jww.TRACE.Println(key, "found in nested config:", val)
- return val
+ if val != nil {
+ jww.TRACE.Println(key, "found in nested config:", val)
+ return val
+ }
}
}
}
diff --git a/viper_test.go b/viper_test.go
index 0c0c7e5..fdbe4d9 100644
--- a/viper_test.go
+++ b/viper_test.go
@@ -907,3 +907,12 @@
SetConfigName("default")
assert.Empty(t, v.getConfigFile())
}
+
+func TestShadowedNestedValue(t *testing.T) {
+ polyester := "polyester"
+ initYAML()
+ SetDefault("clothing.shirt", polyester)
+
+ assert.Equal(t, GetString("clothing.jacket"), "leather")
+ assert.Equal(t, GetString("clothing.shirt"), polyester)
+}