Skip to content

Commit c18e93a

Browse files
Fix handling of implicitly declared EqualityContract on record types in validation generator (#62511)
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent fb4bb02 commit c18e93a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/Shared/RoslynUtils/WellKnownTypeData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public enum WellKnownType
123123
System_ComponentModel_DataAnnotations_ValidationAttribute,
124124
System_ComponentModel_DataAnnotations_RequiredAttribute,
125125
System_ComponentModel_DataAnnotations_CustomValidationAttribute,
126+
System_Type,
126127
}
127128

128129
public static string[] WellKnownTypeNames =
@@ -243,5 +244,6 @@ public enum WellKnownType
243244
"System.ComponentModel.DataAnnotations.ValidationAttribute",
244245
"System.ComponentModel.DataAnnotations.RequiredAttribute",
245246
"System.ComponentModel.DataAnnotations.CustomValidationAttribute",
247+
"System.Type",
246248
];
247249
}

src/Validation/gen/Extensions/ISymbolExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Linq;
6+
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
67
using Microsoft.CodeAnalysis;
78

89
namespace Microsoft.Extensions.Validation;
@@ -32,4 +33,9 @@ attribute.AttributeClass is { } attributeClass &&
3233

3334
return property.Name;
3435
}
36+
37+
public static bool IsEqualityContract(this IPropertySymbol prop, WellKnownTypes wellKnownTypes) =>
38+
prop.Name == "EqualityContract"
39+
&& SymbolEqualityComparer.Default.Equals(prop.Type, wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Type))
40+
&& prop.DeclaredAccessibility == Accessibility.Protected;
3541
}

src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
162162
{
163163
// Skip compiler generated properties and properties already processed via
164164
// the record processing logic above.
165-
if (member.IsImplicitlyDeclared || resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default))
165+
if (member.IsImplicitlyDeclared
166+
|| member.IsEqualityContract(wellKnownTypes)
167+
|| resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default))
166168
{
167169
continue;
168170
}

0 commit comments

Comments
 (0)