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

WriteConfig fails with JSON filetype when encountering arrays of objects and overwrites the config file with 0 bytes #730

Open
aeneasr opened this issue Jul 13, 2019 · 5 comments

Comments

@aeneasr
Copy link
Contributor

aeneasr commented Jul 13, 2019

Given the following JSON config file

foo:
  bar:
    - baz: 1
    - baz: 2

and the following go program

package main

import "github.com/ory/viper"

func main() {
	viper.SetConfigFile("./config.yaml")
	must(viper.ReadInConfig())
	viper.SetConfigFile("./config.json")
	must(viper.WriteConfig())
}

func must(err error) {
	if err != nil {
		panic(err)
	}
}

If executed, the program will fail with error

panic: While marshaling config: json: unsupported type: map[interface {}]interface {}

and even replace the config file's contents with an empty file. This works without issues when using YAML files.

Tested on latest master.

@aeneasr
Copy link
Contributor Author

aeneasr commented Jul 15, 2019

I've resolved this on our fork: https://github.com/ory/viper

aeneasr added a commit to ory/viper that referenced this issue Jul 15, 2019
aeneasr added a commit to ory/viper that referenced this issue Jul 15, 2019
ttys3 pushed a commit to ttys3/viper that referenced this issue Aug 25, 2019
@lukemarsden
Copy link

@spf13 hi, thank you for viper and cobra!

Any chance the fix for this in ory@5bace2a could be applied to spf13/viper please?

Basically, you can't json serialize viper configs that have been saved as yaml without this.

@sagikazarmark
Copy link
Collaborator

Should be a trivial fix once #1167 is merged.

@lukemarsden
Copy link

ok, looks like there's some other good-sounding fixes in ory@5bace2a as well, FWIW.

Thank you!

@sagikazarmark
Copy link
Collaborator

BTW using yaml v3 could also be solution for this problem. As of the next release you can do that by adding viper_yaml3 to your build tags. Let me know if that helps @lukemarsden

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

3 participants