Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnmarshalKey normalizes all map keys to lower case #373

Open
lnashier opened this issue Jul 28, 2017 · 26 comments
Open

UnmarshalKey normalizes all map keys to lower case #373

lnashier opened this issue Jul 28, 2017 · 26 comments

Comments

@lnashier
Copy link

lnashier commented Jul 28, 2017

{
   "someKey": 1,
   "myMap": {
       "A": "v1",
       "B": "v2"
   }
}
config := viper.New()
config.AddConfigPath("myconfigpath")
config.SetConfigName("myconfig")
var myMap map[string]string
config.UnmarshalKey("myMap", &myMap)

myMap has all lower-cased keys.

Can somebody comment if it is expected or is it a bug ?

@knadh
Copy link

knadh commented Oct 17, 2017

This doesn't seem to be documented anywhere but internally, Viper lowercases all keys which is weird:

viper/viper.go

Line 1303 in d9cca5e

func (v *Viper) insensitiviseMaps() {

This goes against the YAML, TOML, and JSON specs where keys are case sensitive.

@lnashier
Copy link
Author

I will request viper authors to comment.

@lnashier
Copy link
Author

lnashier commented Apr 9, 2018

@spf13 any comment?

@lnashier
Copy link
Author

lnashier commented Apr 9, 2018

#482

@wjdp
Copy link

wjdp commented Apr 15, 2018

Just came across this today. Is there an option or workaround to disable this behaviour?

@lnashier
Copy link
Author

knadh added a commit to knadh/viper that referenced this issue Jan 25, 2019
YAML, TOML, and JSON dictate keys to be case-sensitive. Viper's default
behaviour of lowercasing the keys for key insensitivity is incompatible
with these standards and has the side effect of making it difficult for
use cases such as case sensitive API credentials in configuration.
For eg: MyApiKey=MySecret (in TOML).

See spf13#131, spf13#260, spf13#293, spf13#371, spf13#373

This commit adds a global function `viper.SetKeyCaseSensitivity()` that
enables this behaviour to be turned off, after which, all keys, irrespective
of nesting, retain their cases. This respects all configuration operations
including getting, setting, and merging.
knadh added a commit to knadh/viper that referenced this issue Jan 29, 2019
YAML, TOML, and JSON dictate keys to be case-sensitive. Viper's default
behaviour of lowercasing the keys for key insensitivity is incompatible
with these standards and has the side effect of making it difficult for
use cases such as case sensitive API credentials in configuration.
For eg: MyApiKey=MySecret (in TOML).

See spf13#131, spf13#260, spf13#293, spf13#371, spf13#373

This commit adds a global function `viper.SetKeyCaseSensitivity()` that
enables this behaviour to be turned off, after which, all keys, irrespective
of nesting, retain their cases. This respects all configuration operations
including getting, setting, and merging.
knadh added a commit to knadh/viper that referenced this issue Jan 29, 2019
YAML, TOML, and JSON dictate keys to be case-sensitive. Viper's default
behaviour of lowercasing the keys for key insensitivity is incompatible
with these standards and has the side effect of making it difficult for
use cases such as case sensitive API credentials in configuration.
For eg: MyApiKey=MySecret (in TOML).

See spf13#131, spf13#260, spf13#293, spf13#371, spf13#373

This commit adds a global function `viper.SetKeysCaseSensitive()` that
enables this behaviour to be turned off, after which, all keys, irrespective
of nesting, retain their cases. This respects all configuration operations
including getting, setting, and merging.
mdelapenya added a commit to mdelapenya/viper that referenced this issue Aug 2, 2019
mdelapenya added a commit to mdelapenya/viper that referenced this issue Aug 2, 2019
@lnashier
Copy link
Author

lnashier commented Aug 6, 2019

#666 fixes it

@codemodify
Copy link

This issues is still there

@knadh
Copy link

knadh commented Dec 25, 2019

@codemodify #635 has been pending for almost a year and should give you better context on the state of this issue.

@ackratos
Copy link

ackratos commented Jan 3, 2020

want a fix for this issue as well :(

@sagikazarmark
Copy link
Collaborator

@codemodify @ackratos this is not an easy fix as it goes directly against core Viper principles and goals. I'm working on it, but please keep in mind this is an open source project with a lot of users. A major change like this is not easy to pull in.

@codemodify
Copy link

codemodify commented Jan 3, 2020

Thank you, completely understand, will it be ok to have this highlighted on the front page that can be used for making decisions when picking up tools ? The reason for that is because when you are half way through it is hard to drift away when you understand the limits

@tomqwpl
Copy link

tomqwpl commented Apr 22, 2020

I've just hit this too, and it's most unexpected. I'm demarshalling a struct that contains a map. The struct fields demarshall fine but the map keys all come out lower case, which is unexpected.

@fefe982
Copy link

fefe982 commented Oct 29, 2020

I had to make the map into a slice to work around this, making the key a field value of the slice element. Lucky I have control of the data structure.

But still want to see viper handling case-sensitive map keys locally.

@farukarakaya
Copy link

Still no development on this issue I guess?

@lnashier
Copy link
Author

@farukarakaya Feel free to use https://github.com/lnashier/viper it does not normalize key to lower case.

@sagikazarmark
Copy link
Collaborator

Tracking the issue in #1014

@Ranx0r0x
Copy link

Ranx0r0x commented Jul 4, 2023

Still seems to be an issue and it took me awhile to find the issue.

@datager
Copy link

datager commented Aug 25, 2023

still a issue

@AwesomeAsmit007
Copy link

still an issue

1 similar comment
@li-xunhuan
Copy link

still an issue

@bellondr
Copy link

still an issue, can we config this as option

@jherman4
Copy link

I just want to add that I am running in to this. This issue has been open for almost 7 years and there is still no option to disable this behavior? Why?

@SleepyBrett
Copy link

I imagine 'breaking change' ..aka 'we fucked up but now everyone works around it, if we fix it it breaks all those workarounds.'

It's pig-headedness and they should just pull the bandaid off.

@wwoytenko
Copy link

It is still an issue

GreenmaskIO/greenmask#76

wwoytenko added a commit to GreenmaskIO/greenmask that referenced this issue Apr 27, 2024
This solves problem with map structure described -> spf13/viper#373 that caused issue in Greenmask #76

We need to keep the original keys in the map without lowercasing. To overcome this problem we need use default yaml and json parsers avoiding vaiper or mapstructure usage
@RedCollarPanda
Copy link

This is still an issue. Any updates on a fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests