Skip to content

src/Identity/Extensions.Stores/src/IdentityRole.cs Name Should be required and not nullable #44497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luizfbicalho opened this issue Oct 12, 2022 · 5 comments
Assignees
Labels
area-identity Includes: Identity and providers design-proposal This issue represents a design proposal for a different issue, linked in the description ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved

Comments

@luizfbicalho
Copy link

luizfbicalho commented Oct 12, 2022

Summary

I didn't understand why IdentityRole.Name, IdentityRole.NormalizedName , IdentityUser.UserName and IdentityUser.NormalizedUserName are nullable, this are required properties and should be required

Motivation and goals

The major problem that I see is that i every place that I use the IdentityRole and IdentityUser I'll need to test if the name is null, and doesn't seem to be a reason for that.

The issue is in the #40007 change

@luizfbicalho luizfbicalho added the design-proposal This issue represents a design proposal for a different issue, linked in the description label Oct 12, 2022
@blowdart blowdart added the area-identity Includes: Identity and providers label Oct 12, 2022
@HaoK
Copy link
Member

HaoK commented Oct 12, 2022

We have a constructor that leaves UserName null for example here https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Extensions.Stores/src/IdentityUser.cs#L19

@HaoK HaoK closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@HaoK HaoK added the ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. label Oct 12, 2022
@ghost ghost added the Status: Resolved label Oct 12, 2022
@luizfbicalho
Copy link
Author

Couldn't this constructor leave it as string.empty?

this will make every part of the code to check this properties if they are null

@davidkarlsson
Copy link

davidkarlsson commented Nov 9, 2022

I don't understand why you made these properties nullable either. These properties should not be nullable for us and making these changes at this late stage seems rather reckless in my opinion considering they are used for database entities.

Are there even any use cases where it makes sense that these properties are nullable? What good is a role without a name here for instance?

Now we have a mismatch between the nullability of the columns in our users and roles tables and our ef core entities and it's incorrect for us to change the nullability of the columns.

@KaisoBits
Copy link

KaisoBits commented Nov 9, 2022

I agree that it seems like an unnecessary change especially for a property such as the username.

I can't think of much use cases where you don't have a username for your user so to me it seems like this way you're forcing 95% of projects to put null forgiving operators everywhere where this property is used because it'll never be null.

I guess the workaround would be to do something like this to silence all warnings

public class MyUserClass : IdentityUser
{
  [AllowNull]
  public override string UserName { get; set; }
}

or this to be safe

[AllowNull]
public override string UserName 
{ 
    get => base.UserName ?? string.Empty;
    set => base.UserName = value ?? string.Empty; 
}

AllowNull is necessary to silence the warning about the nullability of the overriden property not matching the base class' one although it has the effect of allowing null assigments to this property.

So maybe for this case simply suppressing this warning in the MyUserClass is a better solution.

#pragma warning disable CS8765 // Nullability of type of parameter doesn't match overridden member (possibly because of nullability attributes).
set => base.UserName = value ?? string.Empty;
#pragma warning restore CS8765

@luizfbicalho
Copy link
Author

I still don't get it, I mean, is there a scenario where username should be null?

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-identity Includes: Identity and providers design-proposal This issue represents a design proposal for a different issue, linked in the description ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. Status: Resolved
Projects
None yet
Development

No branches or pull requests

5 participants