Skip to content

Commit 05b2afb

Browse files
authored
Add nullable annotations to Identity.Extensions (#40007)
* Add nullable annotations to Identity.Extensions Contributes to #5680
1 parent a3bb765 commit 05b2afb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1774
-236
lines changed

src/Identity/EntityFrameworkCore/src/IdentityDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ protected IdentityDbContext() { }
9898
/// <summary>
9999
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User roles.
100100
/// </summary>
101-
public virtual DbSet<TUserRole> UserRoles { get; set; }
101+
public virtual DbSet<TUserRole> UserRoles { get; set; } = default!;
102102

103103
/// <summary>
104104
/// Gets or sets the <see cref="DbSet{TEntity}"/> of roles.
105105
/// </summary>
106-
public virtual DbSet<TRole> Roles { get; set; }
106+
public virtual DbSet<TRole> Roles { get; set; } = default!;
107107

108108
/// <summary>
109109
/// Gets or sets the <see cref="DbSet{TEntity}"/> of role claims.
110110
/// </summary>
111-
public virtual DbSet<TRoleClaim> RoleClaims { get; set; }
111+
public virtual DbSet<TRoleClaim> RoleClaims { get; set; } = default!;
112112

113113
/// <summary>
114114
/// Configures the schema needed for the identity framework.

src/Identity/EntityFrameworkCore/src/IdentityEntityFrameworkBuilderExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static IdentityBuilder AddEntityFrameworkStores<TContext>(this IdentityBu
2626
return builder;
2727
}
2828

29-
private static void AddStores(IServiceCollection services, Type userType, Type roleType, Type contextType)
29+
private static void AddStores(IServiceCollection services, Type userType, Type? roleType, Type contextType)
3030
{
3131
var identityUserType = FindGenericBaseType(userType, typeof(IdentityUser<>));
3232
if (identityUserType == null)
@@ -89,12 +89,11 @@ private static void AddStores(IServiceCollection services, Type userType, Type r
8989
}
9090
services.TryAddScoped(typeof(IUserStore<>).MakeGenericType(userType), userStoreType);
9191
}
92-
9392
}
9493

95-
private static Type FindGenericBaseType(Type currentType, Type genericBaseType)
94+
private static Type? FindGenericBaseType(Type currentType, Type genericBaseType)
9695
{
97-
var type = currentType;
96+
Type? type = currentType;
9897
while (type != null)
9998
{
10099
var genericType = type.IsGenericType ? type.GetGenericTypeDefinition() : null;

src/Identity/EntityFrameworkCore/src/IdentityUserContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ protected IdentityUserContext() { }
7878
/// <summary>
7979
/// Gets or sets the <see cref="DbSet{TEntity}"/> of Users.
8080
/// </summary>
81-
public virtual DbSet<TUser> Users { get; set; }
81+
public virtual DbSet<TUser> Users { get; set; } = default!;
8282

8383
/// <summary>
8484
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User claims.
8585
/// </summary>
86-
public virtual DbSet<TUserClaim> UserClaims { get; set; }
86+
public virtual DbSet<TUserClaim> UserClaims { get; set; } = default!;
8787

8888
/// <summary>
8989
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User logins.
9090
/// </summary>
91-
public virtual DbSet<TUserLogin> UserLogins { get; set; }
91+
public virtual DbSet<TUserLogin> UserLogins { get; set; } = default!;
9292

9393
/// <summary>
9494
/// Gets or sets the <see cref="DbSet{TEntity}"/> of User tokens.
9595
/// </summary>
96-
public virtual DbSet<TUserToken> UserTokens { get; set; }
96+
public virtual DbSet<TUserToken> UserTokens { get; set; } = default!;
9797

98-
private StoreOptions GetStoreOptions() => this.GetService<IDbContextOptions>()
98+
private StoreOptions? GetStoreOptions() => this.GetService<IDbContextOptions>()
9999
.Extensions.OfType<CoreOptionsExtension>()
100100
.FirstOrDefault()?.ApplicationServiceProvider
101101
?.GetService<IOptions<IdentityOptions>>()
@@ -118,7 +118,7 @@ protected override void OnModelCreating(ModelBuilder builder)
118118
var storeOptions = GetStoreOptions();
119119
var maxKeyLength = storeOptions?.MaxLengthForKeys ?? 0;
120120
var encryptPersonalData = storeOptions?.ProtectPersonalData ?? false;
121-
PersonalDataConverter converter = null;
121+
PersonalDataConverter? converter = null;
122122

123123
builder.Entity<TUser>(b =>
124124
{

src/Identity/EntityFrameworkCore/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PackageTags>aspnetcore;entityframeworkcore;identity;membership</PackageTags>
8-
<Nullable>disable</Nullable>
98
</PropertyGroup>
109

1110
<ItemGroup>

src/Identity/EntityFrameworkCore/src/PublicAPI.Unshipped.txt

Lines changed: 251 additions & 1 deletion
Large diffs are not rendered by default.

src/Identity/EntityFrameworkCore/src/RoleStore.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class RoleStore<TRole> : RoleStore<TRole, DbContext, string>
2020
/// </summary>
2121
/// <param name="context">The <see cref="DbContext"/>.</param>
2222
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
23-
public RoleStore(DbContext context, IdentityErrorDescriber describer = null) : base(context, describer) { }
23+
public RoleStore(DbContext context, IdentityErrorDescriber? describer = null) : base(context, describer) { }
2424
}
2525

2626
/// <summary>
@@ -37,7 +37,7 @@ public class RoleStore<TRole, TContext> : RoleStore<TRole, TContext, string>
3737
/// </summary>
3838
/// <param name="context">The <see cref="DbContext"/>.</param>
3939
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
40-
public RoleStore(TContext context, IdentityErrorDescriber describer = null) : base(context, describer) { }
40+
public RoleStore(TContext context, IdentityErrorDescriber? describer = null) : base(context, describer) { }
4141
}
4242

4343
/// <summary>
@@ -58,7 +58,7 @@ public class RoleStore<TRole, TContext, TKey> : RoleStore<TRole, TContext, TKey,
5858
/// </summary>
5959
/// <param name="context">The <see cref="DbContext"/>.</param>
6060
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
61-
public RoleStore(TContext context, IdentityErrorDescriber describer = null) : base(context, describer) { }
61+
public RoleStore(TContext context, IdentityErrorDescriber? describer = null) : base(context, describer) { }
6262
}
6363

6464
/// <summary>
@@ -83,7 +83,7 @@ public class RoleStore<TRole, TContext, TKey, TUserRole, TRoleClaim> :
8383
/// </summary>
8484
/// <param name="context">The <see cref="DbContext"/>.</param>
8585
/// <param name="describer">The <see cref="IdentityErrorDescriber"/>.</param>
86-
public RoleStore(TContext context, IdentityErrorDescriber describer = null)
86+
public RoleStore(TContext context, IdentityErrorDescriber? describer = null)
8787
{
8888
if (context == null)
8989
{
@@ -211,7 +211,7 @@ protected virtual async Task SaveChanges(CancellationToken cancellationToken)
211211
{
212212
throw new ArgumentNullException(nameof(role));
213213
}
214-
return Task.FromResult(ConvertIdToString(role.Id));
214+
return Task.FromResult(ConvertIdToString(role.Id)!);
215215
}
216216

217217
/// <summary>
@@ -220,7 +220,7 @@ protected virtual async Task SaveChanges(CancellationToken cancellationToken)
220220
/// <param name="role">The role whose name should be returned.</param>
221221
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
222222
/// <returns>A <see cref="Task{TResult}"/> that contains the name of the role.</returns>
223-
public virtual Task<string> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
223+
public virtual Task<string?> GetRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
224224
{
225225
cancellationToken.ThrowIfCancellationRequested();
226226
ThrowIfDisposed();
@@ -238,7 +238,7 @@ protected virtual async Task SaveChanges(CancellationToken cancellationToken)
238238
/// <param name="roleName">The name of the role.</param>
239239
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
240240
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
241-
public virtual Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken = default(CancellationToken))
241+
public virtual Task SetRoleNameAsync(TRole role, string? roleName, CancellationToken cancellationToken = default(CancellationToken))
242242
{
243243
cancellationToken.ThrowIfCancellationRequested();
244244
ThrowIfDisposed();
@@ -255,21 +255,21 @@ protected virtual async Task SaveChanges(CancellationToken cancellationToken)
255255
/// </summary>
256256
/// <param name="id">The id to convert.</param>
257257
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided <paramref name="id"/>.</returns>
258-
public virtual TKey ConvertIdFromString(string id)
258+
public virtual TKey? ConvertIdFromString(string id)
259259
{
260260
if (id == null)
261261
{
262262
return default(TKey);
263263
}
264-
return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id);
264+
return (TKey?)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id);
265265
}
266266

267267
/// <summary>
268268
/// Converts the provided <paramref name="id"/> to its string representation.
269269
/// </summary>
270270
/// <param name="id">The id to convert.</param>
271271
/// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns>
272-
public virtual string ConvertIdToString(TKey id)
272+
public virtual string? ConvertIdToString(TKey id)
273273
{
274274
if (id.Equals(default(TKey)))
275275
{
@@ -284,7 +284,7 @@ public virtual string ConvertIdToString(TKey id)
284284
/// <param name="id">The role ID to look for.</param>
285285
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
286286
/// <returns>A <see cref="Task{TResult}"/> that result of the look up.</returns>
287-
public virtual Task<TRole> FindByIdAsync(string id, CancellationToken cancellationToken = default(CancellationToken))
287+
public virtual Task<TRole?> FindByIdAsync(string id, CancellationToken cancellationToken = default(CancellationToken))
288288
{
289289
cancellationToken.ThrowIfCancellationRequested();
290290
ThrowIfDisposed();
@@ -298,7 +298,7 @@ public virtual string ConvertIdToString(TKey id)
298298
/// <param name="normalizedName">The normalized role name to look for.</param>
299299
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
300300
/// <returns>A <see cref="Task{TResult}"/> that result of the look up.</returns>
301-
public virtual Task<TRole> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
301+
public virtual Task<TRole?> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
302302
{
303303
cancellationToken.ThrowIfCancellationRequested();
304304
ThrowIfDisposed();
@@ -311,7 +311,7 @@ public virtual string ConvertIdToString(TKey id)
311311
/// <param name="role">The role whose normalized name should be retrieved.</param>
312312
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
313313
/// <returns>A <see cref="Task{TResult}"/> that contains the name of the role.</returns>
314-
public virtual Task<string> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
314+
public virtual Task<string?> GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken = default(CancellationToken))
315315
{
316316
cancellationToken.ThrowIfCancellationRequested();
317317
ThrowIfDisposed();
@@ -329,7 +329,7 @@ public virtual string ConvertIdToString(TKey id)
329329
/// <param name="normalizedName">The normalized name to set</param>
330330
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
331331
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
332-
public virtual Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken = default(CancellationToken))
332+
public virtual Task SetNormalizedRoleNameAsync(TRole role, string? normalizedName, CancellationToken cancellationToken = default(CancellationToken))
333333
{
334334
cancellationToken.ThrowIfCancellationRequested();
335335
ThrowIfDisposed();
@@ -371,7 +371,7 @@ protected void ThrowIfDisposed()
371371
throw new ArgumentNullException(nameof(role));
372372
}
373373

374-
return await RoleClaims.Where(rc => rc.RoleId.Equals(role.Id)).Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToListAsync(cancellationToken);
374+
return await RoleClaims.Where(rc => rc.RoleId.Equals(role.Id)).Select(c => new Claim(c.ClaimType!, c.ClaimValue!)).ToListAsync(cancellationToken);
375375
}
376376

377377
/// <summary>

0 commit comments

Comments
 (0)