Skip to content

Commit a976778

Browse files
committed
Don't generate TypeInfo for invalidatable types
1 parent bac0c95 commit a976778

File tree

4 files changed

+17
-35
lines changed

4 files changed

+17
-35
lines changed

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Parsers/ValidationsGenerator.TypesParser.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ internal bool TryExtractValidatableType(ITypeSymbol typeSymbol, RequiredSymbols
5050

5151
// Extract validatable types discovered in base types of this type and add them to the top-level list.
5252
var current = typeSymbol.BaseType;
53+
var hasValidatableBaseType = false;
5354
while (current != null && current.SpecialType != SpecialType.System_Object)
5455
{
55-
_ = TryExtractValidatableType(current, requiredSymbols, ref validatableTypes, ref visitedTypes);
56+
hasValidatableBaseType |= TryExtractValidatableType(current, requiredSymbols, ref validatableTypes, ref visitedTypes);
5657
current = current.BaseType;
5758
}
5859

@@ -61,9 +62,16 @@ internal bool TryExtractValidatableType(ITypeSymbol typeSymbol, RequiredSymbols
6162

6263
// Extract the validatable types discovered in the JsonDerivedTypeAttributes of this type and add them to the top-level list.
6364
var derivedTypes = typeSymbol.GetJsonDerivedTypes(requiredSymbols.JsonDerivedTypeAttribute);
65+
var hasValidatableDerivedTypes = false;
6466
foreach (var derivedType in derivedTypes ?? [])
6567
{
66-
_ = TryExtractValidatableType(derivedType, requiredSymbols, ref validatableTypes, ref visitedTypes);
68+
hasValidatableDerivedTypes |= TryExtractValidatableType(derivedType, requiredSymbols, ref validatableTypes, ref visitedTypes);
69+
}
70+
71+
// No validatable members or derived types found, so we don't need to add this type.
72+
if (members.IsDefaultOrEmpty && !hasValidatableBaseType && !hasValidatableDerivedTypes)
73+
{
74+
return false;
6775
}
6876

6977
// Add the type itself as a validatable type itself.
@@ -81,6 +89,11 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
8189
{
8290
var hasValidatableType = TryExtractValidatableType(member.Type.UnwrapType(requiredSymbols.IEnumerable), requiredSymbols, ref validatableTypes, ref visitedTypes);
8391
var attributes = ExtractValidationAttributes(member, requiredSymbols, out var isRequired);
92+
// If the member has no validation attributes or validatable types and is not required, skip it.
93+
if (attributes.IsDefaultOrEmpty && !hasValidatableType && !isRequired)
94+
{
95+
continue;
96+
}
8497
members.Add(new ValidatableProperty(
8598
ContainingType: member.ContainingType,
8699
Type: member.Type,
@@ -129,7 +142,6 @@ internal static ImmutableArray<ValidationAttribute> ExtractValidationAttributes(
129142
return [];
130143
}
131144

132-
// Continue with existing logic...
133145
var validationAttributes = attributes
134146
.Where(attribute => attribute.AttributeClass != null)
135147
.Where(attribute => attribute.AttributeClass!.ImplementsValidationAttribute(requiredSymbols.ValidationAttribute));

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/ValidationsGenerator.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ public sealed partial class ValidationsGenerator : IIncrementalGenerator
1111
{
1212
public void Initialize(IncrementalGeneratorInitializationContext context)
1313
{
14-
// while (!System.Diagnostics.Debugger.IsAttached)
15-
// {
16-
// System.Threading.Thread.Sleep(1000);
17-
// #pragma warning disable RS1035 // Do not use APIs banned for analyzers
18-
// System.Console.WriteLine($"Waiting for debugger to attach on {System.Diagnostics.Process.GetCurrentProcess().Id}...");
19-
// #pragma warning restore RS1035 // Do not use APIs banned for analyzers
20-
// }
2114
// Resolve the symbols that will be required when making comparisons
2215
// in future steps.
2316
var requiredSymbols = context.CompilationProvider.Select(ExtractRequiredSymbols);

src/Http/Http.Extensions/test/ValidationsGenerator/snapshots/ValidationsGeneratorTests.CanValidateIValidatableObject#ValidatableInfoResolver.g.verified.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,14 @@ private ValidatableTypeInfo CreateValidatableSubType()
108108
{
109109
return new GeneratedValidatableTypeInfo(
110110
type: typeof(global::ValidatableSubType),
111-
members: [
112-
new GeneratedValidatablePropertyInfo(
113-
containingType: typeof(global::ValidatableSubType),
114-
propertyType: typeof(string),
115-
name: "Value3",
116-
displayName: "Value3",
117-
validationAttributes: []
118-
),
119-
]
111+
members: []
120112
);
121113
}
122114
private ValidatableTypeInfo CreateComplexValidatableType()
123115
{
124116
return new GeneratedValidatableTypeInfo(
125117
type: typeof(global::ComplexValidatableType),
126118
members: [
127-
new GeneratedValidatablePropertyInfo(
128-
containingType: typeof(global::ComplexValidatableType),
129-
propertyType: typeof(int),
130-
name: "Value1",
131-
displayName: "Value 1",
132-
validationAttributes: []
133-
),
134119
new GeneratedValidatablePropertyInfo(
135120
containingType: typeof(global::ComplexValidatableType),
136121
propertyType: typeof(string),

src/Http/Http.Extensions/test/ValidationsGenerator/snapshots/ValidationsGeneratorTests.CanValidatePolymorphicTypes#ValidatableInfoResolver.g.verified.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,7 @@ private ValidatableTypeInfo CreateBaseValidatableType()
148148
{
149149
return new GeneratedValidatableTypeInfo(
150150
type: typeof(global::BaseValidatableType),
151-
members: [
152-
new GeneratedValidatablePropertyInfo(
153-
containingType: typeof(global::BaseValidatableType),
154-
propertyType: typeof(int),
155-
name: "Value1",
156-
displayName: "Value 1",
157-
validationAttributes: []
158-
),
159-
]
151+
members: []
160152
);
161153
}
162154
private ValidatableTypeInfo CreateContainerType()

0 commit comments

Comments
 (0)