-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix handling of implicitly declared EqualityContract on record types in validation generator #62511
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 2 commits
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 |
---|---|---|
|
@@ -105,6 +105,11 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow | |
return true; | ||
} | ||
|
||
private static bool IsEqualityContract(IPropertySymbol prop) => | ||
prop.Name == "EqualityContract" | ||
&& prop.Type.ToDisplayString() == "System.Type" | ||
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. Add System.Type to https://github.com/dotnet/aspnetcore/blob/main/src/Shared/RoslynUtils/WellKnownTypeData.cs then use WellKnownTypes to do the type comparison here. 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. Added System.Type to WellKnownTypeData.cs and updated the method to use WellKnownTypes for type comparison instead of string comparison. a6e7085 |
||
&& prop.DeclaredAccessibility == Accessibility.Protected; | ||
|
||
internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymbol typeSymbol, WellKnownTypes wellKnownTypes, ref HashSet<ValidatableType> validatableTypes, ref List<ITypeSymbol> visitedTypes) | ||
{ | ||
var members = new List<ValidatableProperty>(); | ||
|
@@ -162,7 +167,9 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb | |
{ | ||
// Skip compiler generated properties and properties already processed via | ||
// the record processing logic above. | ||
if (member.IsImplicitlyDeclared || resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default)) | ||
if (member.IsImplicitlyDeclared | ||
|| IsEqualityContract(member) | ||
|| resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default)) | ||
{ | ||
continue; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,4 +370,31 @@ async Task ValidInputProducesNoWarnings(Endpoint endpoint) | |
}); | ||
|
||
} | ||
|
||
[Fact] | ||
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. Remove this test. 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. Removed the test as requested. a6e7085 |
||
public async Task RecordTypes_DoNotIncludeEqualityContractProperty() | ||
{ | ||
// Arrange | ||
var source = """ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Validation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.Services.AddValidation(); | ||
var app = builder.Build(); | ||
|
||
app.MapPost("/test-record", (TestRecord record) => Results.Ok("Passed")); | ||
|
||
app.Run(); | ||
|
||
public record TestRecord([Required] string Name); | ||
"""; | ||
|
||
// Act & Assert - Verify the generated code doesn't include EqualityContract | ||
await Verify(source, out var compilation); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
//HintName: ValidatableInfoResolver.g.cs | ||
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. Remove this file. 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. Removed the snapshot file as requested. a6e7085 |
||
#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.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)] | ||
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::TestRecord)) | ||
{ | ||
validatableInfo = new GeneratedValidatableTypeInfo( | ||
type: typeof(global::TestRecord), | ||
members: [ | ||
new GeneratedValidatablePropertyInfo( | ||
containingType: typeof(global::TestRecord), | ||
propertyType: typeof(string), | ||
name: "Name", | ||
displayName: "Name" | ||
), | ||
] | ||
); | ||
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([property: global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] 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.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.
Move this method to src/Validation/gen/Extensions/ISymbolExtensions.cs.
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.
Moved the method to ISymbolExtensions.cs as requested and updated it to use WellKnownTypes for type comparison. a6e7085