diff --git a/viper.go b/viper.go index 2d5158cbd..2a73c94da 100644 --- a/viper.go +++ b/viper.go @@ -2058,6 +2058,9 @@ func (v *Viper) getSettings(keys []string) map[string]any { m := map[string]any{} // start from the list of keys, and construct the map one value at a time for _, k := range keys { + if v.isPathShadowedByOverride(strings.Split(k, v.keyDelim)) { + continue + } value := v.Get(k) if value == nil { // should not happen, since AllKeys() returns only keys holding a value, @@ -2073,6 +2076,27 @@ func (v *Viper) getSettings(keys []string) map[string]any { return m } +func (v *Viper) isPathShadowedByOverride(path []string) bool { + if len(path) < 2 { + return false + } + + for i := 1; i < len(path); i++ { + parentPath := path[0:i] + if v.searchMap(v.override, parentPath) == nil { + continue + } + + if v.searchMap(v.override, path) != nil { + return false + } + + return true + } + + return false +} + // SetFs sets the filesystem to use to read configuration. func SetFs(fs afero.Fs) { v.SetFs(fs) }