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

Added check of RequiresValidationContext before calling IsValid #110647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public override string FormatErrorMessage(string name) =>
Justification = "The ctor is marked with RequiresUnreferencedCode informing the caller to preserve the other property.")]
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
ArgumentNullException.ThrowIfNull(validationContext);

var otherPropertyInfo = validationContext.ObjectType.GetRuntimeProperty(OtherProperty);
if (otherPropertyInfo == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public static void Validate_PropertyHasDisplayName_UpdatesFormatErrorMessageToCo
Assert.Contains("CustomDisplayName", newErrorMessage);
}

[Fact]
public static void IsValid_ValidationContextNull_ThrowsArgumentNullException()
{
var attribute = new CompareAttribute(nameof(CompareObject.ComparePropertyWithDisplayName));
var compareObject = new CompareObject("test");

Assert.Throws<ArgumentNullException>(() => attribute.IsValid(compareObject.CompareProperty));
}

private class DerivedCompareAttribute : CompareAttribute
{
public DerivedCompareAttribute(string otherProperty) : base(otherProperty) { }
Expand Down