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

Update ResilienceProperties to correctly handle null values #2300

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming a null value is permissible in the first place, doesn't that mean this annotation is wrong? Feels to me that if a null value is legal then we shouldn't need to use ! as the compiler should already consider it a valid possibility.

{
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 is null)
{
value = default!;
Copy link
Author

@iliar-turdushev iliar-turdushev Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use a null-forgiving operator here, otherwise - the compiler generates a warning https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings#nonnullable-reference-not-initialized.

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
Loading