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

Incorrect nullable annotation on Validator.TryValidateValue's value? #91162

Closed
stephentoub opened this issue Aug 26, 2023 · 2 comments · Fixed by #91286
Closed

Incorrect nullable annotation on Validator.TryValidateValue's value? #91162

stephentoub opened this issue Aug 26, 2023 · 2 comments · Fixed by #91286

Comments

@stephentoub
Copy link
Member

stephentoub commented Aug 26, 2023

It's defined like:

public static bool TryValidateValue(object value, ValidationContext validationContext, ICollection<ValidationResult>? validationResults, IEnumerable<ValidationAttribute> validationAttributes)

and the XML doc comment states:

/// <param name="value">The value to test.  It cannot be null.</param>

However, the implementation appears to permit null (the only place it's used is being passed to an object? argument in a helper method), the purpose of an attribute like [Required] is to validate one isn't null (which suggests null could otherwise be valid, and the new .NET 8 source generator for options is having to emit code like:

if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.Path!, context, validationResults, validationAttributes))
{
    builder.AddResults(validationResults);
}

(note the ! on the first argument) because the input might be null but the method is defined to not accept null.

cc: @jeffhandley, @eiriktsarpalis

@ghost
Copy link

ghost commented Aug 26, 2023

Tagging subscribers to this area: @dotnet/area-system-componentmodel-dataannotations
See info in area-owners.md if you want to be subscribed.

Issue Details

It's defined like:

public static bool TryValidateValue(object value, ValidationContext validationContext, ICollection<ValidationResult>? validationResults, IEnumerable<ValidationAttribute> validationAttributes)

and the XML doc comment states:

/// <param name="value">The value to test.  It cannot be null.</param>

However, the implementation appears to permit null, the purpose of an attribute like [Required] is to validate one isn't null (which suggests null could otherwise be valid, and the new .NET 8 source generator for options is having to emit code like:

            if (!global::System.ComponentModel.DataAnnotations.Validator.TryValidateValue(options.Path!, context, validationResults, validationAttributes))
            {
                builder.AddResults(validationResults);
            }

(note the ! on the first argument) because the input might be null but the method is defined to not accept null.

cc: @jeffhandley, @eiriktsarpalis

Author: stephentoub
Assignees: -
Labels:

area-System.ComponentModel.DataAnnotations

Milestone: 8.0.0

@jeffhandley
Copy link
Member

I tried doing some archaeology to figure out where the mismatch between the comment and the behavior came from, but I came up empty-handed.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 29, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 29, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants