Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adds logic handling a case when the value being requested is null
  • Loading branch information
iliar-turdushev committed Sep 20, 2024
1 parent 1c6f3aa commit 7249a68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Polly.Core/ResilienceProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ public sealed class ResilienceProperties
/// <returns>True, if a property was retrieved.</returns>
public bool TryGetValue<TValue>(ResiliencePropertyKey<TValue> key, [MaybeNullWhen(false)] out TValue value)
{
if (Options.TryGetValue(key.Key, out object? val) && val is TValue typedValue)
if (Options.TryGetValue(key.Key, out object? val))
{
value = typedValue;
return true;
if (val is TValue typedValue)
{
value = typedValue;
return true;
}
else if (val == null)
{
value = default!;
return true;
}
}

value = default;
Expand Down
23 changes: 23 additions & 0 deletions test/Polly.Core.Tests/ResiliencePropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public void TryGetValue_Ok()
val.Should().Be(12345);
}

[Fact]
public void TryGetValue_ValueIsNull_Ok()
{
var key = new ResiliencePropertyKey<string?>("dummy");
var props = new ResilienceProperties();

props.Set(key, null);

props.TryGetValue(key, out var val).Should().Be(true);
val.Should().Be(null);
}

[Fact]
public void TryGetValue_NotFound_Ok()
{
Expand All @@ -34,6 +46,17 @@ public void GetValue_Ok()
props.GetValue(key, default).Should().Be(12345);
}

[Fact]
public void GetValue_ValueIsNull_Ok()
{
var key = new ResiliencePropertyKey<string?>("dummy");
var props = new ResilienceProperties();

props.Set(key, null);

props.GetValue(key, "default").Should().Be(null);
}

[Fact]
public void GetValue_NotFound_EnsureDefault()
{
Expand Down

0 comments on commit 7249a68

Please sign in to comment.