Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
6 changes: 5 additions & 1 deletion src/Validation/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ Microsoft.Extensions.Validation.ValidationOptions.TryGetValidatableTypeInfo(Syst
Microsoft.Extensions.Validation.ValidationOptions.ValidationOptions() -> void
abstract Microsoft.Extensions.Validation.ValidatableParameterInfo.GetValidationAttributes() -> System.ComponentModel.DataAnnotations.ValidationAttribute![]!
abstract Microsoft.Extensions.Validation.ValidatablePropertyInfo.GetValidationAttributes() -> System.ComponentModel.DataAnnotations.ValidationAttribute![]!
Microsoft.Extensions.Validation.RuntimeValidatableTypeInfoResolver
Microsoft.Extensions.Validation.RuntimeValidatableTypeInfoResolver.RuntimeValidatableTypeInfoResolver() -> void
Microsoft.Extensions.Validation.RuntimeValidatableTypeInfoResolver.TryGetValidatableParameterInfo(System.Reflection.ParameterInfo! parameterInfo, out Microsoft.Extensions.Validation.IValidatableInfo? validatableInfo) -> bool
Microsoft.Extensions.Validation.RuntimeValidatableTypeInfoResolver.TryGetValidatableTypeInfo(System.Type! type, out Microsoft.Extensions.Validation.IValidatableInfo? info) -> bool
static Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services, System.Action<Microsoft.Extensions.Validation.ValidationOptions!>? configureOptions = null) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
virtual Microsoft.Extensions.Validation.ValidatableParameterInfo.ValidateAsync(object? value, Microsoft.Extensions.Validation.ValidateContext! context, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
virtual Microsoft.Extensions.Validation.ValidatablePropertyInfo.ValidateAsync(object? value, Microsoft.Extensions.Validation.ValidateContext! context, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
virtual Microsoft.Extensions.Validation.ValidatableTypeInfo.ValidateAsync(object? value, Microsoft.Extensions.Validation.ValidateContext! context, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
virtual Microsoft.Extensions.Validation.ValidatableTypeInfo.ValidateAsync(object? value, Microsoft.Extensions.Validation.ValidateContext! context, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
43 changes: 30 additions & 13 deletions src/Validation/src/RuntimeValidatableParameterInfoResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Validation;

Expand All @@ -28,6 +29,13 @@ public bool TryGetValidatableParameterInfo(ParameterInfo parameterInfo, [NotNull
throw new InvalidOperationException($"Encountered a parameter of type '{parameterInfo.ParameterType}' without a name. Parameters must have a name.");
}

// Skip parameters marked with [FromService] or [FromKeyedService] attributes
if (HasFromServiceAttributes(parameterInfo.GetCustomAttributes()))
{
validatableInfo = null;
return false;
}

var validationAttributes = parameterInfo
.GetCustomAttributes<ValidationAttribute>()
.ToArray();
Expand Down Expand Up @@ -60,18 +68,6 @@ private static string GetDisplayName(ParameterInfo parameterInfo)
return parameterInfo.Name!;
}

internal sealed class RuntimeValidatableParameterInfo(
Type parameterType,
string name,
string displayName,
ValidationAttribute[] validationAttributes) :
ValidatableParameterInfo(parameterType, name, displayName)
{
protected override ValidationAttribute[] GetValidationAttributes() => _validationAttributes;

private readonly ValidationAttribute[] _validationAttributes = validationAttributes;
}

private static bool IsClass(Type type)
{
// Skip primitives, enums, common built-in types, and types that are specially
Expand Down Expand Up @@ -102,4 +98,25 @@ private static bool IsClass(Type type)

return type.IsClass;
}
}

private static bool HasFromServiceAttributes(IEnumerable<Attribute> attributes)
{
// Note: Use name-based comparison for FromServices attribute defined in
// MVC assemblies.
return attributes.Any(attr =>
attr.GetType().Name == "FromServicesAttribute" ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be checking if the attribute implements IFromServiceMetadata instead? That's what we did in #61895

Also, if you inherit from either of the attributes these checks would fail, we should be doing something like:

if (attribute is IFromServiceMetadata ||
    attribute is FromKeyedServicesAttribute)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to use IFromServiceMetadata interface and handle inheritance properly. Now checks for attr is IFromServiceMetadata || attr is FromKeyedServicesAttribute to support any attributes that implement the interface or inherit from the specific attribute. Commit 05b5152

attr.GetType() == typeof(FromKeyedServicesAttribute));
}

internal sealed class RuntimeValidatableParameterInfo(
Type parameterType,
string name,
string displayName,
ValidationAttribute[] validationAttributes) :
ValidatableParameterInfo(parameterType, name, displayName)
{
protected override ValidationAttribute[] GetValidationAttributes() => _validationAttributes;

private readonly ValidationAttribute[] _validationAttributes = validationAttributes;
}
}
Loading
Loading