-
-
Notifications
You must be signed in to change notification settings - Fork 158
No way to create a resource derived from Identifiable<string> without sending Id from client side #1153
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
Comments
Hi @minhhieugma, thanks for the provided repro. ModelState validation hasn't changed in v5. The error you're observing is caused by enabling nullable reference types. That changes how ASP.NET validates models: it assumes reference types are required by default. To get the same behavior as in v4.x, there are several options:
|
Hi @minhhieugma, does this solve your problem? Do you need this issue to remain open? |
I think we can close the issue for now. Even though, with the same solution of JADNC 4.2 and .NET 3.1 and EF 3.1
Thank you @bkoelman BTW |
Thanks for the extra details. One difference between v4 and v5 is that v5 is compiled with nullable reference types enabled. This changes how ASP.NET ModelState interprets the required-ness of Yesterday I started digging into this some more, but unfortunately my time is limited. I'll see if I can continue my investigation during the weekend. Depending on my observations, I'll reopen this issue. |
I've taken a deep dive and found the root cause. Using a minimal repro project, I was unable to get the same validation error. So I started comparing the compiler-generated nullability attributes in the DLL using ILDASM and found interesting differences. The JsonApiDotNetCore.Annotations assembly contained public abstract class Identifiable<TId>
{
public virtual TId Id { get; set; } = default!;
}
public class Customer : Identifiable<string>
{
public string Name { get; set; } = null!;
[Range(1, 150)]
public int Age { get; set; }
} As described here, this is because the
There's a bug in ASP.NET 6: it duplicates compiler logic to determine nullability, but takes some shortcuts and gets it wrong. This was recently fixed by replacing the custom logic with relying on the But it doesn't stop there. Turns out that So I tried with .NET 7 preview 4, where everything works as expected. The solution I recommend to you: use option 3 from my earlier comment. When updating to .NET 7, you can remove the |
DESCRIPTION
Cannot make a client POST request to create a resource derived from
Identifiable<string>
The Id field is required.
For many reasons, we might want to assign the Id from the server-side. In the 4.x version, I assign an Id at
OnWritingAsync
. However, with the 5.x version, we validate the ModelState before reaching theOnWritingAsync
functionSTEPS TO REPRODUCE
POST request with the request body
EXPECTED BEHAVIOR
ACTUAL BEHAVIOR
The Id field is required.
exceptionVERSIONS USED
The text was updated successfully, but these errors were encountered: