-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature: Ability to automatically skip null checking for requests DTO in ASP.NET Core #8
Comments
I don't know how I feel about "implicitly" removing null checks from some classes because they are used as an action method parameter. That seems a little too magical and unexpected for me. If null checks are implicitly added everywhere then I think it is necessary to be explicit in removing them otherwise things become unclear. I don't really like the idea of complicating the reasoning required to figure out where null checks are being applied any more than it already is. I would find it unexpected that I need to null check The more appropriate resolution here IMO is to fix ASP.NET's response code when an |
If you add |
That would fix the "bug" and also give you proper model validation, but it would also require remembering to explicitly annotate each non-null property with Really I think the optimal solution is to add a |
Hi @mikernet ! Did you use ASP.NET Core 5+ with enabled nullable references types? It validates non-nullable fields automatically, so you don't need to specify I don't ask to ignore null-checking for all request classes, I just suggest to consider some mechanism that will allow to exclude specific classes from null checking. For example I usually derive all requests from |
How is that different than applying |
Oh I see, you're saying you already derive it from that interface so it wouldn't require an extra step.
My concern here is that in addition to creating conditions that would make it difficult to ascertain what is null checked when looking at a code file, this adds layers of complexity for relatively minor gain (IMO). For example, what happens if you say I still think fixing or tapping into ASP.NET's deserialization mechanism is the clean way to accomplish this without any of the headaches above. |
Well, I don't want to make some request's fields nullable, because they are not. And it's great that I have ability to make it not nullable, since ASP.NET automatically check them after serialization.
Well, it's easy actually - if one of the ignored interfaces is implemented then the tool will ignore null checking injection from these constructors.
I'm not sure about popularity of this case (multi-inheritance for a request), but even if someone will use it, I believe that the tool should ignore null checking exactly for a class that implements an ignored interface ( interface INoNullChecking {}
record BaseClass(string Something); //null checking is enabled
record DerivedClass(string Something, string Something2)
: BaseClass(Something), INoNullChecking; // null checking in this ctor is disabled So in this case, if we pass to |
FYI: I have an issue open on aspnetcore to address this issue: dotnet/aspnetcore#54197 |
First of all thank you very much for a great library!
Here is a case:
RuntimeNullables
packageSomething
cannot be null), but had to return 400 (Bad Request). So, as you see this is kind of a bug actually.Of course we can add
[NullChecks(false)]
attribute to the request class and the bug will disappear, but what if we forget to do that? Again 500? And it's actually not so convenient to addNullChecks
to requests explicitly all times.I suggest to add an ability to automatically skip null checking for requests DTO in ASP.NET Core. Let's discuss an implementation here.
PS. It's interesting that
NullGuard.Fody
just crashed my app at all when I sent an incorrect request like in the example, butRuntimeNullables
only throws an exception and generates 500 (and also customizable). So, I fully migrated fromNullGuard.Fody
toRuntimeNullables
. Thanks again for your work!The text was updated successfully, but these errors were encountered: