Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

DataAnnotation validation ignored #3774

Closed
ducalai opened this issue Dec 16, 2015 · 8 comments
Closed

DataAnnotation validation ignored #3774

ducalai opened this issue Dec 16, 2015 · 8 comments
Assignees
Milestone

Comments

@ducalai
Copy link

ducalai commented Dec 16, 2015

DataAnnotation validation is ignored if route parameter has the same name as object variable passed in the body of the HTTP request

Model:

public class TransferInfo
{
        [Range(1, int.MaxValue)] //or CustomValidationAttribute
        public int AccountId { get; set; }
        public double Amount { get; set; }
}

Controller:

[HttpPost("{accountId:int}")]
public IActionResult Transfer(int accountId, [FromBody] TransferInfo transferInfo)
{
    if (!ModelState.IsValid)
    {
        return HttpBadRequest();
    }
    ...
}

Posted data (JSON):

{
     "accountId": 0,
     "amount": 250.0
}

ModelState is valid even if I check that accountId must be greater than 0, using Range or Custom validation attribute.

If I update my TransferInfo class definition and rename "AccountId" by "ToAccountId" and try to send:

{
     "toAccountId: 0,
     "amount": 250.0
}

ModelState Validation will failed

@tuespetre
Copy link
Contributor

IIRC from digging around in the validation class, this will be because a validation state entry with a key of "AccountId" (case-insensitive) has already been populated. If you place the TransferInfo argument first in the signature, does the validation take effect?

@ducalai
Copy link
Author

ducalai commented Dec 16, 2015

I haven't think of that. You are right, validation works if TransferInfo is first in the signature.
Thanks!

@Eilon
Copy link
Member

Eilon commented Dec 29, 2015

@rynowak thoughts on this? This seems orthogonally tangentially related to the issue with the cancellation token with regard to the truth of what's in the model state dictionary...

@Eilon
Copy link
Member

Eilon commented Jan 22, 2016

@rynowak ping

@rynowak
Copy link
Member

rynowak commented Jan 23, 2016

Yep. This is definitely related to #3743

@rynowak
Copy link
Member

rynowak commented Feb 1, 2016

@Eilon - I don't think this is related to my other validation fix. Self-assigning and will fix.

@rynowak rynowak modified the milestones: Backlog, 1.0.0-rc2 Feb 1, 2016
@rynowak rynowak self-assigned this Feb 1, 2016
@Eilon Eilon added the bug label Feb 1, 2016
@Eilon Eilon changed the title DataAnnotation validation DataAnnotation validation ignored Feb 1, 2016
@rynowak
Copy link
Member

rynowak commented Feb 2, 2016

The issue here is that we don't try to validate something that already appears valid. The idea is to avoid duplicating work when we have a cycle. However, cycles are rare, and validation may depend on the container-object for correctness.

The criteria that we're using in this case is the model state dictionary care, which we've seen can definitely overlap between model objects. We should just change this check to proceed if the state is anything other than Invalid rather than requiring it to be Unvalidated.

@Eilon Eilon assigned kichalla and unassigned rynowak Feb 2, 2016
@Eilon
Copy link
Member

Eilon commented Feb 2, 2016

@kichalla donating this bug to you, it is a gift from @rynowak .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants