Skip to content

Commit f3b370c

Browse files
authored
Annotate Authorization.Core, Authorization.Policy with nullable (#22990)
Contributes to #5680
1 parent a173be2 commit f3b370c

30 files changed

+132
-114
lines changed

eng/targets/CSharp.Common.targets

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
The code block that follows sets it up so projects in this repo that target ns2.0 or netfx can compile when Nullable is configured.
3636
Based on https://github.com/dotnet/runtime/blob/93b6c449d4f31ddd7d573d1d3769e681d5ebceb9/src/libraries/Directory.Build.targets#L215-L222
3737
-->
38-
<When Condition="'$(Nullable)' != '' AND ('$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
39-
<PropertyGroup>
40-
<DefineConstants>$(DefineConstants),INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
41-
</PropertyGroup>
42-
<ItemGroup>
43-
<Compile Include="$(SharedSourceRoot)Nullable\NullableAttributes.cs" />
44-
</ItemGroup>
38+
<When Condition="'$(Nullable)' != '' AND ('$(TargetFrameworkIdentifier)' == '.NETStandard' OR '$(TargetFrameworkIdentifier)' == '.NETFramework')">
39+
<PropertyGroup>
40+
<DefineConstants>$(DefineConstants),INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
41+
<NoWarn>$(NoWarn);nullable</NoWarn>
42+
</PropertyGroup>
43+
<ItemGroup>
44+
<Compile Include="$(SharedSourceRoot)Nullable\NullableAttributes.cs" />
45+
</ItemGroup>
4546
</When>
4647
</Choose>
4748

src/Security/Authorization/Core/ref/Microsoft.AspNetCore.Authorization.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
55
<TargetFrameworks Condition="'$(DotNetBuildFromSource)' == 'true'">$(DefaultNetCoreTargetFramework)</TargetFrameworks>
6+
<Nullable>annotations</Nullable>
67
</PropertyGroup>
78
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
89
<Compile Include="Microsoft.AspNetCore.Authorization.netstandard2.0.cs" />

src/Security/Authorization/Core/ref/Microsoft.AspNetCore.Authorization.netcoreapp.cs

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

src/Security/Authorization/Core/ref/Microsoft.AspNetCore.Authorization.netstandard2.0.cs

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

src/Security/Authorization/Core/src/AuthorizationFailure.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Security.Claims;
67

@@ -21,7 +22,7 @@ private AuthorizationFailure() { }
2122
/// <summary>
2223
/// Failure was due to these requirements not being met via <see cref="AuthorizationHandlerContext.Succeed(IAuthorizationRequirement)"/>.
2324
/// </summary>
24-
public IEnumerable<IAuthorizationRequirement> FailedRequirements { get; private set; }
25+
public IEnumerable<IAuthorizationRequirement> FailedRequirements { get; private set; } = Array.Empty<IAuthorizationRequirement>();
2526

2627
/// <summary>
2728
/// Return a failure due to <see cref="AuthorizationHandlerContext.Fail"/> being called.
@@ -31,7 +32,6 @@ public static AuthorizationFailure ExplicitFail()
3132
=> new AuthorizationFailure
3233
{
3334
FailCalled = true,
34-
FailedRequirements = new IAuthorizationRequirement[0]
3535
};
3636

3737
/// <summary>

src/Security/Authorization/Core/src/AuthorizationHandlerContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AuthorizationHandlerContext
2626
public AuthorizationHandlerContext(
2727
IEnumerable<IAuthorizationRequirement> requirements,
2828
ClaimsPrincipal user,
29-
object resource)
29+
object? resource)
3030
{
3131
if (requirements == null)
3232
{
@@ -52,7 +52,7 @@ public AuthorizationHandlerContext(
5252
/// <summary>
5353
/// The optional resource to evaluate the <see cref="AuthorizationHandlerContext.Requirements"/> against.
5454
/// </summary>
55-
public virtual object Resource { get; }
55+
public virtual object? Resource { get; }
5656

5757
/// <summary>
5858
/// Gets the requirements that have not yet been marked as succeeded.
@@ -95,4 +95,4 @@ public virtual void Succeed(IAuthorizationRequirement requirement)
9595
_pendingRequirements.Remove(requirement);
9696
}
9797
}
98-
}
98+
}

src/Security/Authorization/Core/src/AuthorizationOptions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Authorization
1111
/// </summary>
1212
public class AuthorizationOptions
1313
{
14-
private IDictionary<string, AuthorizationPolicy> PolicyMap { get; } = new Dictionary<string, AuthorizationPolicy>(StringComparer.OrdinalIgnoreCase);
14+
private Dictionary<string, AuthorizationPolicy> PolicyMap { get; } = new Dictionary<string, AuthorizationPolicy>(StringComparer.OrdinalIgnoreCase);
1515

1616
/// <summary>
1717
/// Determines whether authentication handlers should be invoked after a failure.
@@ -35,7 +35,7 @@ public class AuthorizationOptions
3535
/// effect unless you have the AuthorizationMiddleware in your pipeline. It is not used in any way by the
3636
/// default <see cref="IAuthorizationService"/>.
3737
/// </summary>
38-
public AuthorizationPolicy FallbackPolicy { get; set; }
38+
public AuthorizationPolicy? FallbackPolicy { get; set; }
3939

4040
/// <summary>
4141
/// Add an authorization policy with the provided name.
@@ -84,14 +84,19 @@ public void AddPolicy(string name, Action<AuthorizationPolicyBuilder> configureP
8484
/// </summary>
8585
/// <param name="name">The name of the policy to return.</param>
8686
/// <returns>The policy for the specified name, or null if a policy with the name does not exist.</returns>
87-
public AuthorizationPolicy GetPolicy(string name)
87+
public AuthorizationPolicy? GetPolicy(string name)
8888
{
8989
if (name == null)
9090
{
9191
throw new ArgumentNullException(nameof(name));
9292
}
9393

94-
return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null;
94+
if (PolicyMap.TryGetValue(name, out var value))
95+
{
96+
return value;
97+
}
98+
99+
return null;
95100
}
96101
}
97102
}

src/Security/Authorization/Core/src/AuthorizationPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static AuthorizationPolicy Combine(IEnumerable<AuthorizationPolicy> polic
108108
/// A new <see cref="AuthorizationPolicy"/> which represents the combination of the
109109
/// authorization policies provided by the specified <paramref name="policyProvider"/>.
110110
/// </returns>
111-
public static async Task<AuthorizationPolicy> CombineAsync(IAuthorizationPolicyProvider policyProvider, IEnumerable<IAuthorizeData> authorizeData)
111+
public static async Task<AuthorizationPolicy?> CombineAsync(IAuthorizationPolicyProvider policyProvider, IEnumerable<IAuthorizeData> authorizeData)
112112
{
113113
if (policyProvider == null)
114114
{
@@ -127,7 +127,7 @@ public static async Task<AuthorizationPolicy> CombineAsync(IAuthorizationPolicyP
127127
skipEnumeratingData = dataList.Count == 0;
128128
}
129129

130-
AuthorizationPolicyBuilder policyBuilder = null;
130+
AuthorizationPolicyBuilder? policyBuilder = null;
131131
if (!skipEnumeratingData)
132132
{
133133
foreach (var authorizeDatum in authorizeData)

src/Security/Authorization/Core/src/AuthorizationResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Security.Claims;
67

78
namespace Microsoft.AspNetCore.Authorization
@@ -21,7 +22,7 @@ private AuthorizationResult() { }
2122
/// <summary>
2223
/// Contains information about why authorization failed.
2324
/// </summary>
24-
public AuthorizationFailure Failure { get; private set; }
25+
public AuthorizationFailure? Failure { get; private set; }
2526

2627
/// <summary>
2728
/// Returns a successful result.

src/Security/Authorization/Core/src/AuthorizationServiceCollectionExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ public static IServiceCollection AddAuthorizationCore(this IServiceCollection se
5151
throw new ArgumentNullException(nameof(services));
5252
}
5353

54-
if (configure != null)
55-
{
56-
services.Configure(configure);
57-
}
58-
54+
services.Configure(configure);
5955
return services.AddAuthorizationCore();
6056
}
6157
}

0 commit comments

Comments
 (0)