Skip to content
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

Issue when calling Validator.TryValidateObject #24237

Closed
jpiquot opened this issue Nov 25, 2017 · 10 comments
Closed

Issue when calling Validator.TryValidateObject #24237

jpiquot opened this issue Nov 25, 2017 · 10 comments
Assignees
Labels
area-System.ComponentModel.DataAnnotations bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@jpiquot
Copy link

jpiquot commented Nov 25, 2017

Hi,

The method TryValidateObject will throw an exception when trying to validate an object. It seems that the GetObjectValidationErrors method does not check if result of alidatable.Validate(validationContext) is not null. Is this a bug or a desired design?

Regards,
Jérôme Piquot

Method : System.ComponentModel.DataAnnotations.GetObjectValidationErrors

      // Step 3: Test for IValidatableObject implementation            
        if (instance is IValidatableObject validatable)
        {
            var results = validatable.Validate(validationContext);

===> foreach (var result in results.Where(r => r != ValidationResult.Success))
{
errors.Add(new ValidationError(null, instance, result));
}
}
Call Stack :
System.ArgumentNullException: Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Where[TSource](IEnumerable1 source, Func2 predicate)
at System.ComponentModel.DataAnnotations.Validator.GetObjectValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
at System.ComponentModel.DataAnnotations.Validator.TryValidateObject(Object instance, ValidationContext validationContext, ICollection`1 validationResults, Boolean validateAllProperties)
at Flexmind.Framework.Database.ApplicationDbContext.ValidateObject(IValidatableObject toValidate) in C:\Dev\Tyndareus\Flexmind.Framework\Flexmind.Framework\src\Core\Flexmind.Framework.Database\ApplicationDbContext.cs:line 148
at Flexmind.Framework.Database.ApplicationDbContext.BeforeSave() in C:\Dev\Tyndareus\Flexmind.Framework\Flexmind.Framework\src\Core\Flexmind.Framework.Database\ApplicationDbContext.cs:line 127
at Flexmind.Framework.Database.ApplicationDbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) in C:\Dev\Tyndareus\Flexmind.Framework\Flexmind.Framework\src\Core\Flexmind.Framework.Database\ApplicationDbContext.cs:line 102
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(CancellationToken cancellationToken)

@ajcvickers
Copy link
Member

Looks like a bug to me. Any thoughts @divega?

@divega
Copy link
Contributor

divega commented Nov 28, 2017

@ajcvickers Sure, seems like it should be an easy fix too.

@FSou1
Copy link

FSou1 commented Dec 2, 2017

I suspect that an issue in these lines of code:
https://github.com/dotnet/corefx/blob/03c7617170661e1312f6ae414225ef72ea792170/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Validator.cs#L432-L442
The variable results could be null and in this case I propose to skip a loop with adding if(results != null) condition.

If it's a proper way from your point of view I'll do it.

@ajcvickers
Copy link
Member

@FSou1 Yes, that looks reasonable. It would be great if you could send a PR for this. Please also add a test.

@FSou1
Copy link

FSou1 commented Dec 4, 2017

Great, I'll do it

@FSou1
Copy link

FSou1 commented Dec 4, 2017

Looks like NETFX x86 Release Build — was failed but i'm not pretty sure if my changes were the root cause.

Let me know if I have to fix it.

@karelz
Copy link
Member

karelz commented Dec 5, 2017

@FSou1 thanks for you interest to fix the bug. I have sent you Collaborator invite, so that we can assign it to you (GH limitation). Please ping me once you accept (assigning to myself for now).

Pro-tip: Disable notifications for the repo (you will be auto-subscribed) as there's plenty (500+ per day) and opt-in into "your mentions / subscriptions" notification setting.

@karelz karelz self-assigned this Dec 5, 2017
@FSou1
Copy link

FSou1 commented Dec 5, 2017

@karelz ok, i've just accepted the invite.

Ok, i got it.

@karelz karelz assigned FSou1 and unassigned karelz Dec 5, 2017
@FSou1
Copy link

FSou1 commented Dec 7, 2017

@karelz Would you mind helping me to determine the proper way to close this issue? As you've already mentioned a label should be assigned and an attribute for test ignoring on NET FX have to be added.

I'm afraid i'm stuck with both of them:

  1. Do not have enough access rules;
  2. Not able to figure out the content of testResult.xml to retrieve helpful information;

@karelz
Copy link
Member

karelz commented Dec 7, 2017

@FSou1 only members have permissions to change labels - it is tied to commit permissions.
@ajcvickers adding netfx-port-consider, please double check it is something you want to eventually consider porting into Desktop.

@FSou1 let's keep PR discussion in the PR (testResults.xml)

ajcvickers referenced this issue in dotnet/corefx Dec 11, 2017
…25693)

* Fix ArgumentNullException in case of IValidatableObject returns null (#25495)

* Skip validatable null check tests on NETFX (#25495)

* Fix an issue with tests ignore
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.ComponentModel.DataAnnotations bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

6 participants