-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
ConfigurationBuilder - Configuration.Json: Binding a Dictionary<string,string> error when Key contains an string url like http://www.google.es #42643
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 ! |
The separator char does not look easy to change at run time: https://github.com/dotnet/extensions/blob/1774c15ac24c65513fa9fc1f4fbb69be9a2a4e25/src/Configuration/Config.Abstractions/src/ConfigurationPath.cs#L14-L17 |
By the way, I don't think the Microsoft.Extensions.Configuration.* packages will care about |
Im using the ConfigProperty due other reasons. |
Can you use some other strings as keys? For example: {
"auths": {
"es": {
"uri": "https://www.google.es"
}
}
} Then register an |
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 |
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. aspnet/Configuration#792 dotnet/extensions#782. Sounds like something we should address instead of our usual answer that |
I'm happy to look at this one. PR here: #66886 |
I've had a go here: #66886 |
I do not see the reason for |
@AraHaan - I personally wouldn't have been able to see the use of keys with colons in, but the examples here are quite compelling. |
you're describing an array, but you're writing object. {
"auths": [
{
"name": "http://google",
"uri": "https://www.google.es"
},
{
"name": "http://microsoft",
"uri": "https://www.microsoft.es"
}
]
} public class AuthEntry
{
public string Name {get;set;}
public Uri Uri {get;set;}
}
public class AuthsOptions
{
public const string SectionName = "auths";
public AuthEntry[] Auths {get;set;}
}
public static class AuthsServiceCollectionExtensions
{
public static IServiceCollection AddAuths(this IServiceCollection services)
{
services
.AddOptions<AuthsOptions>()
.Configure<IConfiguration>((options, configuration) => configuration.Bind(AuthsOptions.SectionName, options))
//.Validate(...............)
;
return services;
}
public static IServiceCollection AddAuths(this IServiceCollection services, Action<AuthsOptions>
configure) =>
services
.AddAuths()
.Configure(configure)
; public class MyService
{
private readonly AuthsOptions _options;
private readonly HttpClient _httpClient;
public MyService(IOptions<AuthsOptions> options, HttpClient httpClient)
{
_options = options.Value; //.Value here because there's no reload on change, you could adapt it
// build a dictionary from key ?
}
public async Task<string> GetDataFrom(string key)
{
var authEntry = options.Auths.First(authEntry => authEntry == key); // use stringcomparison ?
await _httpClient.GetStringAsync(new Uri(authEntry.Uri, otherRelativeUri);
}
} |
We will consider this in a future release. Moving this issue out of the 7.0 milestone. |
This is the motivation behind #67616 |
Many times you have to use objects by necessity as arrays are completely useless for everything but the most basic configuration needs when you use environment-specific configuration overrides. This is because arrays use positional (index) based merging across environment-specific config files which is an absolute pain to manage and slip-ups in it are many times impossible to detect. Basically; you're telling people: "You're doing it wrong. You're supposed to step into this minefield here." |
There was never any technical need for the There are multiple other issues where this ill-thought out design is the root of the problem. That was reported in #65885 as well, but ultimately wasn't acted upon (yet) because of fear of breaking existing implementations as well. At least that had a decent workaround. This one doesn't. You simply have to avoid using a dictionary keyed on anything that contains a Another problem with this internal storage of fully composed keys is that it's impossible to have a dictionary of empty objects. That would not be the case with a proper tree-based structure. Because there you'd have a string->node mapping where each node can optionally have a primitive value itself, and can have child nodes. Even if the collection of child nodes is empty; and even if the node has no primitive value itself; the node and its key are still representable in a sound way. And thus binding such empty objects would be feasible. So... |
Collapsing this into #67616 since that would be the fundamental change to enable this scenario. |
Describe the bug
Im reading a json file to a dictionary<string,string> like:
having the POCO class
When i load it using ConfigurationRoot like
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:
Expected behavior
Dictionary Key Item should contains https://www.google.es
Screenshots
The text was updated successfully, but these errors were encountered: