From d9a2337b7b21c0a8bd197ffc346ea716b6084bd0 Mon Sep 17 00:00:00 2001 From: wyRainBow Date: Mon, 30 Mar 2026 01:20:30 +0800 Subject: [PATCH 1/2] fix write config after setting override map --- viper.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/viper.go b/viper.go index 2d5158cbd..08fb58d22 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,30 @@ func (v *Viper) getSettings(keys []string) map[string]any { return m } +// isPathShadowedByOverride reports whether a key path is shadowed by a parent +// override map without an explicit override value at the same full path. +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 the full path has a value in overrides, keep it. + 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) } From 3819c130d6221c75da2011a81245d88b6491e76a Mon Sep 17 00:00:00 2001 From: wyRainBow Date: Mon, 30 Mar 2026 10:57:35 +0800 Subject: [PATCH 2/2] remove extra comments in override shadow handling --- viper.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/viper.go b/viper.go index 08fb58d22..2a73c94da 100644 --- a/viper.go +++ b/viper.go @@ -2076,8 +2076,6 @@ func (v *Viper) getSettings(keys []string) map[string]any { return m } -// isPathShadowedByOverride reports whether a key path is shadowed by a parent -// override map without an explicit override value at the same full path. func (v *Viper) isPathShadowedByOverride(path []string) bool { if len(path) < 2 { return false @@ -2089,7 +2087,6 @@ func (v *Viper) isPathShadowedByOverride(path []string) bool { continue } - // If the full path has a value in overrides, keep it. if v.searchMap(v.override, path) != nil { return false }