-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Skip non-public properties in validations generator #63076
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -65,6 +65,12 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow | |||||||
return false; | ||||||||
} | ||||||||
|
||||||||
// Skip types that are not accessible from generated code | ||||||||
if (typeSymbol.DeclaredAccessibility is not Accessibility.Public) | ||||||||
{ | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
visitedTypes.Add(typeSymbol); | ||||||||
|
||||||||
// Extract validatable types discovered in base types of this type and add them to the top-level list. | ||||||||
|
@@ -148,6 +154,12 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb | |||||||
continue; | ||||||||
} | ||||||||
|
||||||||
// Skip properties that are not accessible from generated code | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a comment explaining why non-public properties are skipped, similar to the type-level check, to maintain consistency in documentation.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
if (correspondingProperty.DeclaredAccessibility is not Accessibility.Public) | ||||||||
{ | ||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
// Check if the property's type is validatable, this resolves | ||||||||
// validatable types in the inheritance hierarchy | ||||||||
var hasValidatableType = TryExtractValidatableType( | ||||||||
|
@@ -186,6 +198,12 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb | |||||||
continue; | ||||||||
} | ||||||||
|
||||||||
// Skip properties that are not accessible from generated code | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a comment explaining why non-public properties are skipped, to maintain consistency with the other accessibility checks in this file.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
if (member.DeclaredAccessibility is not Accessibility.Public) | ||||||||
{ | ||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
var hasValidatableType = TryExtractValidatableType(member.Type, wellKnownTypes, ref validatableTypes, ref visitedTypes); | ||||||||
var attributes = ExtractValidationAttributes(member, wellKnownTypes, out var isRequired); | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
//HintName: ValidatableInfoResolver.g.cs | ||
#nullable enable annotations | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by a tool. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
#nullable enable | ||
#pragma warning disable ASP0029 | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
file sealed class InterceptsLocationAttribute : System.Attribute | ||
{ | ||
public InterceptsLocationAttribute(int version, string data) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
namespace Microsoft.Extensions.Validation.Generated | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
file sealed class GeneratedValidatablePropertyInfo : global::Microsoft.Extensions.Validation.ValidatablePropertyInfo | ||
{ | ||
public GeneratedValidatablePropertyInfo( | ||
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] | ||
global::System.Type containingType, | ||
global::System.Type propertyType, | ||
string name, | ||
string displayName) : base(containingType, propertyType, name, displayName) | ||
{ | ||
ContainingType = containingType; | ||
Name = name; | ||
} | ||
|
||
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] | ||
internal global::System.Type ContainingType { get; } | ||
internal string Name { get; } | ||
|
||
protected override global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetValidationAttributes() | ||
=> ValidationAttributeCache.GetValidationAttributes(ContainingType, Name); | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
file sealed class GeneratedValidatableTypeInfo : global::Microsoft.Extensions.Validation.ValidatableTypeInfo | ||
{ | ||
public GeneratedValidatableTypeInfo( | ||
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.Interfaces)] | ||
global::System.Type type, | ||
ValidatablePropertyInfo[] members) : base(type, members) { } | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
file class GeneratedValidatableInfoResolver : global::Microsoft.Extensions.Validation.IValidatableInfoResolver | ||
{ | ||
public bool TryGetValidatableTypeInfo(global::System.Type type, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::Microsoft.Extensions.Validation.IValidatableInfo? validatableInfo) | ||
{ | ||
validatableInfo = null; | ||
if (type == typeof(global::AccessibilityTestType)) | ||
{ | ||
validatableInfo = new GeneratedValidatableTypeInfo( | ||
type: typeof(global::AccessibilityTestType), | ||
members: [ | ||
new GeneratedValidatablePropertyInfo( | ||
containingType: typeof(global::AccessibilityTestType), | ||
propertyType: typeof(string), | ||
name: "PublicProperty", | ||
displayName: "PublicProperty" | ||
), | ||
] | ||
); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// No-ops, rely on runtime code for ParameterInfo-based resolution | ||
public bool TryGetValidatableParameterInfo(global::System.Reflection.ParameterInfo parameterInfo, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::Microsoft.Extensions.Validation.IValidatableInfo? validatableInfo) | ||
{ | ||
validatableInfo = null; | ||
return false; | ||
} | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
file static class GeneratedServiceCollectionExtensions | ||
{ | ||
[InterceptsLocation] | ||
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddValidation(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services, global::System.Action<global::Microsoft.Extensions.Validation.ValidationOptions>? configureOptions = null) | ||
{ | ||
// Use non-extension method to avoid infinite recursion. | ||
return global::Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation(services, options => | ||
{ | ||
options.Resolvers.Insert(0, new GeneratedValidatableInfoResolver()); | ||
if (configureOptions is not null) | ||
{ | ||
configureOptions(options); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Validation.ValidationsGenerator, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60", "42.42.42.42")] | ||
file static class ValidationAttributeCache | ||
{ | ||
private sealed record CacheKey( | ||
[param: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] | ||
[property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] | ||
global::System.Type ContainingType, | ||
string PropertyName); | ||
private static readonly global::System.Collections.Concurrent.ConcurrentDictionary<CacheKey, global::System.ComponentModel.DataAnnotations.ValidationAttribute[]> _cache = new(); | ||
|
||
public static global::System.ComponentModel.DataAnnotations.ValidationAttribute[] GetValidationAttributes( | ||
[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties | global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] | ||
global::System.Type containingType, | ||
string propertyName) | ||
{ | ||
var key = new CacheKey(containingType, propertyName); | ||
return _cache.GetOrAdd(key, static k => | ||
{ | ||
var results = new global::System.Collections.Generic.List<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(); | ||
|
||
// Get attributes from the property | ||
var property = k.ContainingType.GetProperty(k.PropertyName); | ||
if (property != null) | ||
{ | ||
var propertyAttributes = global::System.Reflection.CustomAttributeExtensions | ||
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(property, inherit: true); | ||
|
||
results.AddRange(propertyAttributes); | ||
} | ||
|
||
// Check constructors for parameters that match the property name | ||
// to handle record scenarios | ||
foreach (var constructor in k.ContainingType.GetConstructors()) | ||
{ | ||
// Look for parameter with matching name (case insensitive) | ||
var parameter = global::System.Linq.Enumerable.FirstOrDefault( | ||
constructor.GetParameters(), | ||
p => string.Equals(p.Name, k.PropertyName, global::System.StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (parameter != null) | ||
{ | ||
var paramAttributes = global::System.Reflection.CustomAttributeExtensions | ||
.GetCustomAttributes<global::System.ComponentModel.DataAnnotations.ValidationAttribute>(parameter, inherit: true); | ||
|
||
results.AddRange(paramAttributes); | ||
|
||
break; | ||
} | ||
} | ||
|
||
return results.ToArray(); | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment explaining why non-public types are skipped, as this is a significant behavioral change that affects which types get validation generation.
Copilot uses AI. Check for mistakes.