Skip to content

Commit 2164e82

Browse files
committed
Merge in 'release/7.0' changes
2 parents 744a5d2 + 0384a80 commit 2164e82

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Middleware/OutputCaching/src/OutputCacheAttribute.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ internal IOutputCachePolicy BuildPolicy()
6666
return _builtPolicy;
6767
}
6868

69-
var builder = new OutputCachePolicyBuilder();
69+
OutputCachePolicyBuilder builder;
7070

7171
if (PolicyName != null)
7272
{
73+
// Don't add the default policy if a named one is used as it could already contain it
74+
builder = new OutputCachePolicyBuilder(excludeDefaultPolicy: true);
7375
builder.AddPolicy(new NamedPolicy(PolicyName));
7476
}
77+
else
78+
{
79+
builder = new();
80+
}
7581

7682
if (_noCache != null && _noCache.Value)
7783
{

src/Middleware/OutputCaching/test/OutputCacheAttributeTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task Attribute_CreatesNamedPolicy()
4848
var options = new OutputCacheOptions();
4949
options.AddPolicy("MyPolicy", b => b.Expire(TimeSpan.FromSeconds(42)));
5050

51-
var context = TestUtils.CreateTestContext(options: options);
51+
var context = TestUtils.CreateUninitializedContext(options: options);
5252

5353
var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName));
5454
await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default);
@@ -57,6 +57,20 @@ public async Task Attribute_CreatesNamedPolicy()
5757
Assert.Equal(42, context.ResponseExpirationTimeSpan?.TotalSeconds);
5858
}
5959

60+
[Fact]
61+
public async Task Attribute_NamedPolicyDoesNotInjectDefaultPolicy()
62+
{
63+
var options = new OutputCacheOptions();
64+
options.AddPolicy("MyPolicy", b => b.With(x => false).Cache());
65+
66+
var context = TestUtils.CreateUninitializedContext(options: options);
67+
68+
var attribute = OutputCacheMethods.GetAttribute(nameof(OutputCacheMethods.PolicyName));
69+
await attribute.BuildPolicy().CacheRequestAsync(context, cancellation: default);
70+
71+
Assert.False(context.EnableOutputCaching);
72+
}
73+
6074
[Fact]
6175
public async Task Attribute_CreatesVaryByHeaderPolicy()
6276
{

0 commit comments

Comments
 (0)