Skip to content

Commit 1490f4e

Browse files
committed
No-op code gen under more cases
1 parent a976778 commit 1490f4e

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public sealed partial class ValidationsGenerator : IIncrementalGenerator
2121

2222
internal static void Emit(SourceProductionContext context, (InterceptableLocation? AddValidation, ImmutableArray<ValidatableType> ValidatableTypes) emitInputs)
2323
{
24+
if (emitInputs.AddValidation is null)
25+
{
26+
// Avoid generating code if no AddValidation call was found.
27+
return;
28+
}
2429
var source = Emit(emitInputs.AddValidation, emitInputs.ValidatableTypes);
2530
context.AddSource("ValidatableInfoResolver.g.cs", SourceText.From(source, Encoding.UTF8));
2631
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static ITypeSymbol UnwrapType(this ITypeSymbol type, INamedTypeSymbol enu
4848
type = type.OriginalDefinition;
4949
}
5050

51-
if (type is INamedTypeSymbol namedType && namedType.IsEnumerable(enumerable))
51+
if (type is INamedTypeSymbol namedType && namedType.IsEnumerable(enumerable) && namedType.TypeArguments.Length == 1)
5252
{
5353
// Extract the T from an IEnumerable<T> or List<T>
5454
type = namedType.TypeArguments[0];
@@ -86,4 +86,11 @@ internal static bool ImplementsInterface(this ITypeSymbol type, ITypeSymbol inte
8686

8787
return derivedTypes.Count == 0 ? null : derivedTypes.ToImmutable();
8888
}
89+
90+
internal static bool IsExemptType(this ITypeSymbol type, RequiredSymbols requiredSymbols)
91+
{
92+
return SymbolEqualityComparer.Default.Equals(type, requiredSymbols.HttpContext)
93+
|| SymbolEqualityComparer.Default.Equals(type, requiredSymbols.HttpRequest)
94+
|| SymbolEqualityComparer.Default.Equals(type, requiredSymbols.HttpResponse);
95+
}
8996
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ internal sealed record class RequiredSymbols(
1212
INamedTypeSymbol IValidatableObject,
1313
INamedTypeSymbol JsonDerivedTypeAttribute,
1414
INamedTypeSymbol RequiredAttribute,
15-
INamedTypeSymbol CustomValidationAttribute
15+
INamedTypeSymbol CustomValidationAttribute,
16+
INamedTypeSymbol HttpContext,
17+
INamedTypeSymbol HttpRequest,
18+
INamedTypeSymbol HttpResponse
1619
);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ internal RequiredSymbols ExtractRequiredSymbols(Compilation compilation, Cancell
1717
compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.IValidatableObject")!,
1818
compilation.GetTypeByMetadataName("System.Text.Json.Serialization.JsonDerivedTypeAttribute")!,
1919
compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.RequiredAttribute")!,
20-
compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.CustomValidationAttribute")!
20+
compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.CustomValidationAttribute")!,
21+
compilation.GetTypeByMetadataName("Microsoft.AspNetCore.Http.HttpContext")!,
22+
compilation.GetTypeByMetadataName("Microsoft.AspNetCore.Http.HttpRequest")!,
23+
compilation.GetTypeByMetadataName("Microsoft.AspNetCore.Http.HttpResponse")!
2124
);
2225
}
2326
}

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

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Linq;
7-
using System.Threading;
87
using Microsoft.AspNetCore.Analyzers.Infrastructure;
98
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
109
using Microsoft.CodeAnalysis;
@@ -46,6 +45,11 @@ internal bool TryExtractValidatableType(ITypeSymbol typeSymbol, RequiredSymbols
4645
return true;
4746
}
4847

48+
if (typeSymbol.IsExemptType(requiredSymbols))
49+
{
50+
return false;
51+
}
52+
4953
visitedTypes.Add(typeSymbol);
5054

5155
// Extract validatable types discovered in base types of this type and add them to the top-level list.
@@ -105,34 +109,6 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
105109
return [.. members];
106110
}
107111

108-
public ImmutableArray<ITypeSymbol> ExtractPropertyTypes(ITypeSymbol type, CancellationToken cancellationToken)
109-
{
110-
var builder = ImmutableArray.CreateBuilder<ITypeSymbol>();
111-
var processed = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default);
112-
113-
void Traverse(ITypeSymbol currentType)
114-
{
115-
if (currentType == null || currentType.SpecialType != SpecialType.None || processed.Contains(currentType))
116-
{
117-
return;
118-
}
119-
120-
processed.Add(currentType);
121-
builder.Add(currentType);
122-
123-
foreach (var member in currentType.GetMembers().OfType<IPropertySymbol>())
124-
{
125-
if (member.Type is ITypeSymbol propertyType)
126-
{
127-
Traverse(propertyType);
128-
}
129-
}
130-
}
131-
132-
Traverse(type);
133-
return builder.ToImmutable();
134-
}
135-
136112
internal static ImmutableArray<ValidationAttribute> ExtractValidationAttributes(ISymbol symbol, RequiredSymbols requiredSymbols, out bool isRequired)
137113
{
138114
var attributes = symbol.GetAttributes();

0 commit comments

Comments
 (0)