diff options
author | Sébastien Ros <sebastienros@gmail.com> | 2022-11-11 21:22:36 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 21:22:36 +0300 |
commit | 860b12b64d49aa7ffae534601844dd54d0363ebf (patch) | |
tree | d7cabb89a8b19004ddfa5dc3a14d292aa2b99b3d | |
parent | 15a71447bdcb9e3cdcb287c66441598980b817d4 (diff) |
Fix predicate evaluation in OutputCacheAttribute (#45013)
Fixes #44739
-rw-r--r-- | src/Middleware/OutputCaching/src/OutputCacheAttribute.cs | 8 | ||||
-rw-r--r-- | src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs | 16 |
2 files changed, 22 insertions, 2 deletions
diff --git a/src/Middleware/OutputCaching/src/OutputCacheAttribute.cs b/src/Middleware/OutputCaching/src/OutputCacheAttribute.cs index 1409f20f5b..7194b63948 100644 --- a/src/Middleware/OutputCaching/src/OutputCacheAttribute.cs +++ b/src/Middleware/OutputCaching/src/OutputCacheAttribute.cs @@ -66,12 +66,18 @@ public sealed class OutputCacheAttribute : Attribute return _builtPolicy; } - var builder = new OutputCachePolicyBuilder(); + OutputCachePolicyBuilder builder; if (PolicyName != null) { + // Don't add the default policy if a named one is used as it could already contain it + builder = new OutputCachePolicyBuilder(excludeDefaultPolicy: true); builder.AddPolicy(new NamedPolicy(PolicyName)); } + else + { + builder = new(); + } if (_noCache != null && _noCache.Value) { diff --git a/src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs b/src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs index cd43d2d58a..26c4c5bd49 100644 --- a/src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs +++ b/src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs @@ -48,7 +48,7 @@ public class OutputCacheAttributeTests var options = new OutputCacheOptions(); options.AddPolicy("MyPolicy", b => b.Expire(TimeSpan.FromSeconds(42))); - var context = TestUtils.CreateTestContext(options: options); + var context = TestUtils.CreateUninitializedContext(options: options); var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName)); await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default); @@ -58,6 +58,20 @@ public class OutputCacheAttributeTests } [Fact] + public async Task Attribute_NamedPolicyDoesNotInjectDefaultPolicy() + { + var options = new OutputCacheOptions(); + options.AddPolicy("MyPolicy", b => b.With(x => false).Cache()); + + var context = TestUtils.CreateUninitializedContext(options: options); + + var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName)); + await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default); + + Assert.False(context.EnableOutputCaching); + } + + [Fact] public async Task Attribute_CreatesVaryByHeaderPolicy() { var context = TestUtils.CreateUninitializedContext(); |