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

Configuration data allows nullable values, but ChainedConfigurationProvider.TryGet doesn't work correctly with them #65594

Open
eerhardt opened this issue Feb 18, 2022 · 5 comments

Comments

@eerhardt
Copy link
Member

See the conversation here: dotnet/core#7211 (comment).

IConfigurationProvider.TryGet(string key, out string? value); is nullable annotated as

This means that a ConfigurationProvider can have a null value for a key. So it would return true and the value == null.

However, ChainedConfigurationProvider doesn't seem to support this:

public bool TryGet(string key, out string? value)
{
value = _config[key];
return !string.IsNullOrEmpty(value);

If the inner configuration returned null, ChainedConfigurationProvider would return false, even though the it is possible the inner configuration provider returned true and value == null.

This feels like an issue with our API design here. Can null be a valid configuration value or not? If it can, should ChainedConfigurationProvider respect it?

cc @maxkoshevoi @halter73 @HaoK

@ghost
Copy link

ghost commented Feb 18, 2022

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Issue Details

See the conversation here: dotnet/core#7211 (comment).

IConfigurationProvider.TryGet(string key, out string? value); is nullable annotated as

This means that a ConfigurationProvider can have a null value for a key. So it would return true and the value == null.

However, ChainedConfigurationProvider doesn't seem to support this:

public bool TryGet(string key, out string? value)
{
value = _config[key];
return !string.IsNullOrEmpty(value);

If the inner configuration returned null, ChainedConfigurationProvider would return false, even though the it is possible the inner configuration provider returned true and value == null.

This feels like an issue with our API design here. Can null be a valid configuration value or not? If it can, should ChainedConfigurationProvider respect it?

cc @maxkoshevoi @halter73 @HaoK

Author: eerhardt
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: 7.0.0

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Feb 18, 2022
@maxkoshevoi
Copy link
Contributor

Found the reason of why out string? value wasn't annotated with [NotNullWhen(true)]: #57414 (comment)

@halter73
Copy link
Member

I think the right behavior is for TryGet to return false if _config[key] is null but return true if its "". I'm not how much code out there today relies on falling back from chained config sources that return "". Hopefully not a lot.

@eerhardt eerhardt modified the milestones: 7.0.0, Future Jul 14, 2022
@cschulznethaus
Copy link

I just ran into this behavior of the ChainedConfigurationProvider.

We have a configuration system that get its values from a database, but all values can be overwritten per environment or secrets. Environment and secrets come from the ChainedConfigurationProvider, while the database values have their own IConfigurationProvider implementation.

I wanted to overwrite one value with "" in the environment, but that did not happen due to the string.IsNullOrEmpty(value)check of theChainedConfigurationProvider`.

I agree with @halter73 that "" should return true, but we also had an internal discussion that this would probably not be fixed in the near future as it could harm existing code. 🙄

@jamescrosswell
Copy link

Just adding a vote here.

The current design results in a discrepancy between how Web Host and Generic Host read configuration data from appsettings.json files (presumably Generic Host uses ChainedConfigurationProvider). Web Host supports/returns empty strings verbatim as valid configuration values whereas Generic host interprets these as null.

This breaks the Sentry SDK when using Generic Host (which allows you to set the URL of your sentry server to string.Empty to disable the SDK):

Arguably the current behavioural inconsistencies are a bug then.

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

No branches or pull requests

6 participants