Skip to content

Commit e38ed29

Browse files
committed
Code review fix, test fix
1 parent 48cf20a commit e38ed29

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Http/Http/perf/Microbenchmarks/ValidatableTypesBenchmark.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
244244

245245
private class MockValidatableTypeInfo(Type type, ValidatablePropertyInfo[] members) : ValidatableTypeInfo(type, members)
246246
{
247+
protected override ValidationAttribute[] GetValidationAttributes() => [];
247248
}
248249

249250
private class MockValidatablePropertyInfo(

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ internal bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, WellKnow
8282

8383
visitedTypes.Add(typeSymbol);
8484

85-
var hasValidationAttributes = typeSymbol.GetAttributes()
86-
.Any(attribute => attribute.AttributeClass != null &&
87-
attribute.AttributeClass.ImplementsValidationAttribute(
88-
wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_ValidationAttribute)));
85+
var hasValidationAttributes = HasValidationAttributes(typeSymbol, wellKnownTypes);
8986

9087
// Extract validatable types discovered in base types of this type and add them to the top-level list.
9188
var current = typeSymbol.BaseType;
@@ -288,4 +285,20 @@ internal static ImmutableArray<ValidationAttribute> ExtractValidationAttributes(
288285
NamedArguments: attribute.NamedArguments.ToDictionary(namedArgument => namedArgument.Key, namedArgument => namedArgument.Value.ToCSharpString()),
289286
IsCustomValidationAttribute: SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_CustomValidationAttribute))))];
290287
}
288+
289+
internal static bool HasValidationAttributes(ISymbol symbol, WellKnownTypes wellKnownTypes)
290+
{
291+
var validationAttributeSymbol = wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_ValidationAttribute);
292+
293+
foreach (var attribute in symbol.GetAttributes())
294+
{
295+
if (attribute.AttributeClass is not null &&
296+
attribute.AttributeClass.ImplementsValidationAttribute(validationAttributeSymbol))
297+
{
298+
return true;
299+
}
300+
}
301+
302+
return false;
303+
}
291304
}

0 commit comments

Comments
 (0)