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

JSON configuration binding of type Dictionary<string, string> fails if a key contains a colon #782

Closed
DumpsterDoofus opened this issue Sep 3, 2018 · 4 comments
Milestone

Comments

@DumpsterDoofus
Copy link

Minimal working example

Create a .NET Core console app project with the following appsettings.json contents:

{
    "Values": {
        "key": "value"
    }
}

Add NuGet references to:

  • Microsoft.Extensions.Configuration.Binder v2.1.1
  • Microsoft.Extensions.Configuration.Json v2.1.1

Run the following:

class Program
{
    static void Main()
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
        var d = configuration.GetSection("Values").Get<Dictionary<string, string>>();
    }
}

The result, d, is a dictionary with one pair, as expected.

However, replacing "key" with "k:ey" in the config and running again causes the following exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot create instance of type 'System.String' because it is missing a public parameterless constructor.
  Source=Microsoft.Extensions.Configuration.Binder
  StackTrace:
   at Microsoft.Extensions.Configuration.ConfigurationBinder.CreateInstance(Type type)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindDictionary(Object dictionary, Type dictionaryType, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration, Action`1 configureOptions)
...

Other info

I know : is a reserved character for IConfiguration paths, so maybe this isn't fixable, although is there not some way the : could be escaped when reading from JSON configuration sources?

I was following someone's example in https://stackoverflow.com/questions/42846296/how-to-load-appsetting-json-section-into-dictionary-in-net-core, and stumbled across this issue by accident (some of my Dictionary keys contain colons).

@aspnet-hello aspnet-hello transferred this issue from aspnet/Configuration Dec 17, 2018
@brockallen
Copy link

Any movement on this? I'd think mapping a dictionary would be a bit more flexible.

@mguinness
Copy link

According to aspnet/Configuration#792 this isn't supported and issue was closed.

Colons are reserved for special meaning in the keys, so they shouldn't be used as part of normal key values.

@ajcvickers ajcvickers added this to the Backlog milestone Apr 18, 2019
@CaCTuCaTu4ECKuu
Copy link

What the heck, it's distinctable key. Fix plz

@analogrelay
Copy link

Closing as @mguinness is correct here. The : is a reserved character (it denotes hierarchy), in fact User Secrets relies on that in it's JSON file.

For example, the following two pieces of JSON are considered equivalent in config (and that's by-design, required for User Secrets to work):

{
	"Parent": { 
		"A": "1",
		"B:" "2"
	}
}
{
	"Parent:A": "1",
	"Parent:B": "2"
}

@ghost ghost locked as resolved and limited conversation to collaborators May 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants