-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ModelState.IsValid reports valid model while it is null #4750
Comments
? #4607 remains open. In any case, I believe your request is for validating properties of |
It is, but the discussion seemed to venture more into 'documenting and error reporting'. My line of thinking would be that if I have a model that has required properties, if I would feed it one property it 'fails' because it doesn't have the other. So, with above code, the following fails/succeeds: Body: '' (empty) Body: '{}' Body: '{"email": "[email protected]","password": "12345677Asasd"}' The first one just doesn't make sense to me, if it 'works' when I'm feeding it an empty object, I think it should also 'work' while I (or an external party) feed it an empty body, since it is not fulfilling the required properties, at all. I'm not sure where it should stop? I feel that certainly for API's having one way of describing required props and therefore errors makes it incredibly easy to pass these over to other people and maintain the same error quality. I'm probably missing complexities in the implementation and other consequences, but this is how I see it for the use cases I have. |
The cases you've described sound correct. The JSON deserializer returned |
Sweet, so code wise it is correct. Do you see the more abstracted 'issue' though? Not looking to what actually happens under the hood, but simply what happens in the controller? So to maybe put it in straight words, I'd expect Validation to also check if an object actually exists if it has things like required on one or more of it's properties. |
If you add you can see here how to apply it application-wise: #2615 (comment) |
@Bartmax just tested, the same issue persists with BindRequired. |
you did apply the
right? |
No, I added BindRequired to the Class and properties (in all possible configs). Just tried your full suggestion, but it threw an error in
That I had to change to
since somehow it noted that LoginViewModel didn't have any property attributes (At least on one pass through). Obviously after this it didn't work either since it simply always skipped it ;) |
That's unfortunate :( ok ... well ... it was worth a shot |
@janpieterz are you using RC2 or 1.0.0? You may be now hitting #4652. That was fixed in 1.0.0. |
1.0.0 |
MVC does not perform validation (of any kind) for parameters unless they are bound. This behaviour hasn't changed in forever as far as I know. (My mention of #4652 was wrong; that's not relevant here.) @janpieterz should we turn this issue into a request for at least Could also change One potential workaround would be to check |
I'm curious to know if anyone would ever want |
Personally, I wish C# generally didn't allow nulls unless I went out of my way to allow for them, so when it comes to validation, I think at the least it should default to not null and maybe provide an option for allowing null. In the case of |
I'm happy turning this issue into something else @dougbu, but agree with @rynowak and I'm really curious to the use cases for it not being a validation error. I surely understand that it means diverting from previous ways (seems like a perfect moment to do it now), I get that it might rip open abstraction layers that are put in place (seems like 'new' insight shows they might need to be different), or any other kind of annoying / unpleasant consequence. For me as an API developer using this, and probably everyone else, I had to find this out the hard way, and now have to extend the validation myself (the errors are not automatically generated etc etc). I'd prefer to not have to change anything in my code in the above example to get it to work. If this really is impossible something like BindRequired enforcing this so it's at least as simple as throwing that on a parameter certainly will work! As a side note, I just made a new project to look at the templates and even the default templates don't seem to take this into account. Most actions ( |
Cleared the Investigate label. @triage the decisions here are whether to make a change, whether validation of a See #4971 for a few cases where /cc @danroth27 @Eilon |
Cleared assignee to make it clear @danroth27 needs to have a look. |
BTW this is how it worked in Web API 2. |
BTW, if you like the sound of null-body-is-validation-error you can step into that alternate universe pretty easily like so:
Add the convention to |
@rynowak @Eilon my proposal is to update |
The global flag as The only thing that won't be supported is the option to have a binding as |
One more thing, |
A few clarifications:
|
Following a quick discussion just now, we're going to ensure that if you use As a secondary goal, if it turns out that we can also extend This is the raw ingredient that will let you control whether or not POST/PUT bodies are actually required (versus it being legit to supply no input, making the parameter value is |
@SteveSandersonMS I didn't mention |
I realize that I missed the discussion about this, but I have bone to pick over:
I don't think the current behavior is in any way reasonable or useful and the reactions on my comment corroborate this. No one has put forth a scenario where an API has a optional body. This is 2.0.0 so we should make the default experience be what we think is the best for our users. /cc @Eilon |
Also FYI for the discussion around |
I'd be ok with having this be on by default. I think this is an acceptable breaking change, and there's a way to turn it off. |
Slight clarification @rynowak: It's entirely possible to associate most validation attributes with a parameter; their (Separately, But, we're not changing anything about |
@dougbu I don't think anything thinks there's an issue with applying the attribute; the problem is an inability to retrieve the metadata using the existing pipes that we have. |
OK, having started on implementation, I see that we're not currently in a position to have per-usage-site overrides for this, because:
ProposalSince virtually in all cases we think people will want to just keep the default behaviour on all actions (and the new default behaviour will be "[FromBody] means body is required"), we probably don't desperately need per-usage-site overrides right now. So I just propose to do the flag on In the future, if we implement parameter-level model metadata, then it would be trivial to use that to control @Eilon / @dougbu / @rynowak - does this sound reasonable and sufficient? |
I think this is perfectly acceptable, considering that it's all we initially wanted 😄 It's unfortunate that there's no straightforward way to make it per-usage, but I won't lose sleep over that. |
Sounds sad but reasonable. Will |
If you mean properties on the type that is being bound via |
Remove |
Ah I see. Done! |
hi all. i have problem. i hope everybody can help me. i want validate for each attribute of a object and notifi error for each attribute. thanks you! |
@minhhuynh1812 please open a new issue if you have a specific scenario that is not working as you wish. You may want to start by reviewing the docs e.g. https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding or asking a question on https://stackoverflow.com. In fact, your question may match something already asked, perhaps in the https://stackoverflow.com/questions/tagged/asp.net-core-mvc tag. |
I found this link and I hope it can guide you in the right direction: https://stackoverflow.com/questions/43694106/asp-net-core-webapi-modelstate-not-working |
Possibly a dupe of #4607 but since it seemed to be (somehow) resolved there, I thought it smart to file another issue to validate if there is a difference.
Relevant bits:
When calling the method without any parameters in the body, the model rightfully becomes null. I can make this a manual check of course, but it seems if there is a model state, that has parameters that are null but contain properties with the required attribute, that the model state shouldn't succeed.
Thoughts? Easy to circumvent with an extra null check, but it feels a bit counter intuitive (maybe I'm missing something though!)
The text was updated successfully, but these errors were encountered: