Skip to content

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

Merged
merged 3 commits into from
Jul 2, 2025
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
2 changes: 2 additions & 0 deletions src/Shared/RoslynUtils/WellKnownTypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public enum WellKnownType
System_ComponentModel_DataAnnotations_ValidationAttribute,
System_ComponentModel_DataAnnotations_RequiredAttribute,
System_ComponentModel_DataAnnotations_CustomValidationAttribute,
System_Type,
}

public static string[] WellKnownTypeNames =
Expand Down Expand Up @@ -243,5 +244,6 @@ public enum WellKnownType
"System.ComponentModel.DataAnnotations.ValidationAttribute",
"System.ComponentModel.DataAnnotations.RequiredAttribute",
"System.ComponentModel.DataAnnotations.CustomValidationAttribute",
"System.Type",
];
}
6 changes: 6 additions & 0 deletions src/Validation/gen/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq;
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
using Microsoft.CodeAnalysis;

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

return property.Name;
}

public static bool IsEqualityContract(this IPropertySymbol prop, WellKnownTypes wellKnownTypes) =>
prop.Name == "EqualityContract"
&& SymbolEqualityComparer.Default.Equals(prop.Type, wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Type))
&& prop.DeclaredAccessibility == Accessibility.Protected;
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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
|| member.IsEqualityContract(wellKnownTypes)
|| resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default))
{
continue;
}
Expand Down
Loading