From 4fa02d45de948974e205b49ac54cc98ab650aa2d Mon Sep 17 00:00:00 2001 From: ZLoo Date: Sun, 21 Jul 2024 16:32:21 +0300 Subject: [PATCH] Fix warning CA1062#DefaultCacheKeyStrategy --- src/Polly/Caching/DefaultCacheKeyStrategy.cs | 12 +++++++++--- .../Caching/DefaultCacheKeyStrategySpecs.cs | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Polly/Caching/DefaultCacheKeyStrategy.cs b/src/Polly/Caching/DefaultCacheKeyStrategy.cs index 12272f73eff..5cff753f529 100644 --- a/src/Polly/Caching/DefaultCacheKeyStrategy.cs +++ b/src/Polly/Caching/DefaultCacheKeyStrategy.cs @@ -4,7 +4,6 @@ namespace Polly.Caching; /// /// The default cache key strategy for . Returns the property . /// -#pragma warning disable CA1062 // Validate arguments of public methods public class DefaultCacheKeyStrategy : ICacheKeyStrategy { /// @@ -12,8 +11,15 @@ public class DefaultCacheKeyStrategy : ICacheKeyStrategy /// /// The execution context. /// The cache key. - public string GetCacheKey(Context context) => - context.OperationKey; + public string GetCacheKey(Context context) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + return context.OperationKey; + } /// /// Gets an instance of the . diff --git a/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs b/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs index c81162040d4..0158dc06a22 100644 --- a/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs +++ b/test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs @@ -2,6 +2,14 @@ public class DefaultCacheKeyStrategySpecs { + [Fact] + public void Should_throw_when_context_is_null() + { + Context context = null!; + Action action = () => DefaultCacheKeyStrategy.Instance.GetCacheKey(context); + action.Should().Throw().And.ParamName.Should().Be("context"); + } + [Fact] public void Should_return_Context_OperationKey_as_cache_key() {