Runtime: ConfigurationBuilder - Configuration.Json: Binding a Dictionary<string,string> error when Key contains an string url like http://www.google.es

Created on 21 Sep 2020  路  10Comments  路  Source: dotnet/runtime

Describe the bug

Im reading a json file to a dictionary like:

{
  "auths": {
    "https://www.google.es": {
      "uri": "https://www.google.es"
    }
  }
}

having the POCO class

public class MyClass
{
        [ConfigurationProperty("auths", IsRequired = false)]
        public Dictionary<string, OtherObject> Auths { get; set; }
}

When i load it using ConfigurationRoot like

public MyClass Read()
        {
            IConfigurationRoot configuration =
                new ConfigurationBuilder()
                    .AddJsonFile("path_to_json_file", optional: true, reloadOnChange: true)
                .Build();

            // Try Parse and return (if will exception in case of binding error)
            var _settings= new MyClass();
            configuration.Bind(_settings);

            return _settings;
        }

The issue is that the Dictionary item contains https only on the Key property. Looks like it's ignoring or having issues with https://.....

Not sure if it's an intended feature or a bug.

To Reproduce

Steps to reproduce the behavior:

  1. Using version '3.1.8' of package 'Microsoft.Extensions.Configuration.Json'
  2. Run this code 'with files in description'
  3. See error in the screenshot attached

Expected behavior

Dictionary Key Item should contains https://www.google.es

Screenshots

image

area-Extensions-Configuration enhancement

Most helpful comment

Looks like this issue has been encountered previously, e.g. https://github.com/aspnet/Configuration/issues/792 https://github.com/dotnet/extensions/issues/782. Sounds like something we should address instead of our usual answer that : is a reserved character and this issue is by design.

All 10 comments

That seems deliberate to me. The configuration system flattens the key hierarchy to a string with colons as delimiters so it becomes "auths:https://www.google.es:uri", and binding then cannot know where the boundaries were originally.

It is documented in Hierarchical configuration data.

That makes sense, even if i don't agree on using : as hierarchy... but, i will try to look into the code to see if the separator char can be changed / configured somewhere.

Thanks for the reply i completely missed that point !

By the way, I don't think the Microsoft.Extensions.Configuration.* packages will care about [ConfigurationProperty("auths", IsRequired = false)] in your POCO class.

Im using the ConfigProperty due other reasons.
About the limiter... i will need to use JsonSerializer :

Can you use some other strings as keys? For example:

{
  "auths": {
    "es": {
      "uri": "https://www.google.es"
    }
  }
}

Then register an IPostConfigureOptions<MyClass> implementation that reads the Dictionary<string, OtherObject> Auths property that was populated from configuration binding, and copies the objects to a separate dictionary with the keys that you really want. The "es" key would be used only in configuration files or environment variables, not for looking up OtherObject instances later.

Well, not in this case, i need to use the Uri as key. But anyway, i can do exactly the same using the JsonSerializer with a custom serializer, and works well.

Without the need of : in the key, the classical approach works pretty well.

Thank you for your help, i think we can close the issue ?

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Maybe we want to have an escape sequence for json config so this is allowed? Or a flag to disable the ':' behavior just for Json?

If any change is made, we should port it to the Newtonsoft.Json config provider in Extensions.

Looks like this issue has been encountered previously, e.g. https://github.com/aspnet/Configuration/issues/792 https://github.com/dotnet/extensions/issues/782. Sounds like something we should address instead of our usual answer that : is a reserved character and this issue is by design.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yahorsi picture yahorsi  路  3Comments

btecu picture btecu  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

omariom picture omariom  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments