Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ public virtual bool TryGet(string key, out string? value)

LogTryGet(GetType().Name, key);

value = ConfigurationRoot?.GetValue<string>(key);
bool found = value != null;
if (ConfigurationRoot == null)
{
value = null;
return false;
}

bool found = InnerTryGet(ConfigurationRoot, key, out value);

if (found)
{
Expand All @@ -95,6 +100,31 @@ public virtual bool TryGet(string key, out string? value)
return found;
}

private static bool InnerTryGet(IConfigurationRoot root, string key, out string? value)
{
IList<IConfigurationProvider> providers = root.Providers as IList<IConfigurationProvider> ?? root.Providers.ToList();

for (int index = providers.Count - 1; index >= 0; index--)
{
IConfigurationProvider provider = providers[index];

try
{
if (provider.TryGet(key, out value))
{
return true;
}
}
catch (ObjectDisposedException)
{
// Skip disposed providers to avoid exceptions during access.
}
}

value = null;
return false;
}

public void Set(string key, string? value)
{
ArgumentNullException.ThrowIfNull(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override bool TryGet(string key, out string? value)
}
}

return value != null;
return found;
}

private ITextDecryptor EnsureDecryptor(IConfigurationRoot configurationRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override bool TryGet(string key, out string? value)
}
}

return value != null;
return found;
}

[LoggerMessage(Level = LogLevel.Trace, Message = "Replaced value '{OriginalValue}' at key '{Key}' with '{ReplacementValue}'.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ public void Substitutes_placeholders_in_key_lookups()
["key2"] = "${key1?not-found}",
["key3"] = "${no-key?not-found}",
["key4"] = "${no-key}",
["key5"] = string.Empty
["key5"] = string.Empty,
["key6"] = null
};

var builder = new ConfigurationBuilder();
Expand All @@ -219,6 +220,8 @@ public void Substitutes_placeholders_in_key_lookups()
configuration["key3"].Should().Be("not-found");
configuration["key4"].Should().Be("${no-key}");
configuration["key5"].Should().BeEmpty();
configuration["key6"].Should().BeNull();
configuration["key7"].Should().BeNull();

configuration["no-key"] = "new-key-value";

Expand Down Expand Up @@ -408,6 +411,46 @@ static void AssertTypesInSourceTree(IList<IConfigurationSource> sources, int ind
}
}

[Fact]
public void Binding_property_against_null_overwrites_default_value()
{
var appSettings = new Dictionary<string, string?>
{
["Root:TestOptions:Value"] = null
};

var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(appSettings);
builder.AddPlaceholderResolver();
IConfigurationRoot configuration = builder.Build();

IConfigurationSection section = configuration.GetSection("Root:TestOptions");
var options = section.Get<TestOptions>();

options.Should().NotBeNull();
options.Value.Should().BeNull();
}

[Fact]
public void Binding_property_against_empty_string_overwrites_default_value()
{
var appSettings = new Dictionary<string, string?>
{
["Root:TestOptions:Value"] = string.Empty
};

var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(appSettings);
builder.AddPlaceholderResolver();
IConfigurationRoot configuration = builder.Build();

IConfigurationSection section = configuration.GetSection("Root:TestOptions");
var options = section.Get<TestOptions>();

options.Should().NotBeNull();
options.Value.Should().BeEmpty();
}

public void Dispose()
{
_loggerFactory.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/test/Placeholder.Test/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Steeltoe.Configuration.Placeholder.Test;

internal sealed class TestOptions
{
public string? Value { get; set; }
public string? Value { get; set; } = "DefaultValue";
}
Loading