Skip to content

Commit a6e7085

Browse files
Copilotcaptainsafia
andcommitted
Address code review feedback: move IsEqualityContract to ISymbolExtensions and use WellKnownTypes
Co-authored-by: captainsafia <[email protected]>
1 parent 6f9b7b8 commit a6e7085

File tree

5 files changed

+9
-193
lines changed

5 files changed

+9
-193
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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow
105105
return true;
106106
}
107107

108-
private static bool IsEqualityContract(IPropertySymbol prop) =>
109-
prop.Name == "EqualityContract"
110-
&& prop.Type.ToDisplayString() == "System.Type"
111-
&& prop.DeclaredAccessibility == Accessibility.Protected;
112-
113108
internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymbol typeSymbol, WellKnownTypes wellKnownTypes, ref HashSet<ValidatableType> validatableTypes, ref List<ITypeSymbol> visitedTypes)
114109
{
115110
var members = new List<ValidatableProperty>();
@@ -168,7 +163,7 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
168163
// Skip compiler generated properties and properties already processed via
169164
// the record processing logic above.
170165
if (member.IsImplicitlyDeclared
171-
|| IsEqualityContract(member)
166+
|| member.IsEqualityContract(wellKnownTypes)
172167
|| resolvedRecordProperty.Contains(member, SymbolEqualityComparer.Default))
173168
{
174169
continue;

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.RecordType.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -370,31 +370,4 @@ async Task ValidInputProducesNoWarnings(Endpoint endpoint)
370370
});
371371

372372
}
373-
374-
[Fact]
375-
public async Task RecordTypes_DoNotIncludeEqualityContractProperty()
376-
{
377-
// Arrange
378-
var source = """
379-
using System;
380-
using System.ComponentModel.DataAnnotations;
381-
using Microsoft.AspNetCore.Builder;
382-
using Microsoft.AspNetCore.Http;
383-
using Microsoft.Extensions.Validation;
384-
using Microsoft.Extensions.DependencyInjection;
385-
386-
var builder = WebApplication.CreateBuilder();
387-
builder.Services.AddValidation();
388-
var app = builder.Build();
389-
390-
app.MapPost("/test-record", (TestRecord record) => Results.Ok("Passed"));
391-
392-
app.Run();
393-
394-
public record TestRecord([Required] string Name);
395-
""";
396-
397-
// Act & Assert - Verify the generated code doesn't include EqualityContract
398-
await Verify(source, out var compilation);
399-
}
400373
}

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.RecordTypes_DoNotIncludeEqualityContractProperty#ValidatableInfoResolver.g.verified.cs

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)