Skip to content
Open
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
3 changes: 2 additions & 1 deletion docs/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Analyzer Warnings

### ASP (`ASP0000-ASP0039`)
### ASP (`ASP0000-ASP0040`)

| Diagnostic ID | Description |
| :---------------- | :---------- |
Expand Down Expand Up @@ -45,6 +45,7 @@
| __`ASP0037`__ | \[ValidatableType] cannot be used in generated code |
| __`ASP0038`__ | \[ValidatableType] should not be used without a call to 'AddValidation' |
| __`ASP0039`__ | Experimental warning for well-known passkey endpoints APIs |
| __`ASP0040`__ | \[ValidatableType] is applied to an open generic type |

### API (`API1000-API1003`)

Expand Down
1 change: 1 addition & 0 deletions src/Validation/gen/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ ASP0035 | Usage | Warning | ValidationsDiagnosticAnalyzer
ASP0036 | Usage | Warning | ValidationsDiagnosticAnalyzer
ASP0037 | Usage | Warning | ValidatableTypeInGeneratedCodeDiagnosticAnalyzer
ASP0038 | Usage | Warning | ValidationsDiagnosticAnalyzer
ASP0040 | Usage | Warning | ValidationsDiagnosticAnalyzer
21 changes: 21 additions & 0 deletions src/Validation/gen/Analyzers/ValidationsDiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ internal sealed class ValidationsDiagnosticAnalyzer : DiagnosticAnalyzer
helpLinkUri: GetHelpLinkUri("ASP0036"),
WellKnownDiagnosticTags.CompilationEnd);

internal static readonly DiagnosticDescriptor ValidatableTypeIsOpenGeneric = new(
"ASP0040",
"[ValidatableType] is applied to an open generic type",
"The type '{0}' is marked with [ValidatableType] but declares type parameters that cannot be resolved statically. The generated validation code can only reference closed types, so it is skipped by the validation source generator. Validation still applies to closed constructions of this type used as endpoint parameters.",
Usage,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: null,
helpLinkUri: GetHelpLinkUri("ASP0040"));

internal static readonly DiagnosticDescriptor ValidatableTypeIsUsedWithoutAddValidation = new(
"ASP0038",
"[ValidatableType] should not be used without a call to 'AddValidation'",
Expand All @@ -83,6 +93,7 @@ internal sealed class ValidationsDiagnosticAnalyzer : DiagnosticAnalyzer
EndpointParameterTypeIsNotAccessible,
ValidatablePropertyIsNotAccessible,
ValidatablePropertyIsNotAccessibleCompilationEnd,
ValidatableTypeIsOpenGeneric,
ValidatableTypeIsUsedWithoutAddValidation,
];

Expand Down Expand Up @@ -200,6 +211,16 @@ attributeOperation.Operation is IObjectCreationOperation attributeObjectCreation
return;
}

// Mirrors the generator's emit-time guard: a [ValidatableType] whose type tree
// still contains a type parameter (its own, or an enclosing type's) can never be
// referenced from the non-generic generated resolver, so the generator skips it.
// Surface that skip to the author at the declaration instead of staying silent.
if (ValidationsGenerator.ContainsTypeParameter(attributedType))
{
context.ReportDiagnostic(Diagnostic.Create(ValidatableTypeIsOpenGeneric, attributedType.Locations.FirstOrDefault(), attributedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)));
return;
}

AnalyzeType(context.ReportDiagnostic, skipValidationAttribute, jsonIgnoreAttributeSymbol, attributedType, topLevelValidatableTypes, wellKnownTypes, isCalledFromCompilationEnd: false);
}
}, OperationKind.Attribute);
Expand Down
57 changes: 57 additions & 0 deletions src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ internal static bool TryExtractValidatableType(ITypeSymbol incomingTypeSymbol, W
return false;
}

// The generated resolver is non-generic: every typeof(...) it emits must name a
// fully closed type. Skip any symbol whose type tree still contains a type
// parameter; emitting it would produce typeof(T) / typeof(Wrapper<T>), which
// does not compile in the generated code. Closed constructions (Wrapper<string>)
// contain no type parameters and register normally.
if (ContainsTypeParameter(typeSymbol))
{
return false;
}

// Skip file-local types, which are only accessible within their declaring file
// and cannot be referenced from generated code in a different file
// Skip types that are not accessible from generated code
Expand Down Expand Up @@ -366,4 +376,51 @@ internal static bool HasIValidatableObjectInterface(ITypeSymbol typeSymbol, Well
var validatableObjectSymbol = wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_IValidatableObject);
return typeSymbol.ImplementsInterface(validatableObjectSymbol);
}

/// <summary>
/// Returns true if the given type symbol contains an unresolved type parameter
/// anywhere in its type tree. This catches not only bare <c>T</c> but also
/// constructed types like <c>List&lt;T&gt;</c>, <c>T[]</c>, <c>T?</c>, and
/// <c>Dictionary&lt;string, T&gt;</c>, all of which would produce invalid
/// <c>typeof(...)</c> expressions in the emitted code.
/// </summary>
internal static bool ContainsTypeParameter(ITypeSymbol type)
{
// Bare type parameter: T, TSelf, TSelf?
if (type is ITypeParameterSymbol)
{
return true;
}

// Array: T[], T[,], List<T>[]
if (type is IArrayTypeSymbol arrayType)
{
return ContainsTypeParameter(arrayType.ElementType);
}

if (type is INamedTypeSymbol namedType)
{
// Constructed generic: List<T>, Dictionary<string, T>, Nullable<T>, Func<T, bool>
if (namedType.IsGenericType)
{
foreach (var typeArg in namedType.TypeArguments)
{
if (ContainsTypeParameter(typeArg))
{
return true;
}
}
}

// Nested type inside a generic outer: Outer<T>.Inner. Inner has no type
// arguments of its own, but its emitted FQN still carries the outer's
// parameter; typeof(global::Outer<T>.Inner) is not valid C# either.
if (namedType.ContainingType is not null)
{
return ContainsTypeParameter(namedType.ContainingType);
}
}

return false;
}
}
Loading
Loading