RDG does not support generic types from outer scope
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
Given the code:
```csharp
public static void TestGenericParam(IEndpointRouteBuilder app) where TUser : class
{
app.MapPost("/test-param", ([FromServices] UserManager userManager) => { });
}
public static void TestGenericResponse(IEndpointRouteBuilder app) where TUser : new()
{
app.MapPost("/test-return", () => new TUser());
}
```
RDG will generate code that fails to compile because `TUser` is undefined. As a short-term mitigation, we can skip generating `MapPost` and similar methods when we see an open generic parameter in the delegate signature. Long term, we can look at flowing the generic parameters through the `Map` methods we generate which can then hopefully be inferred.
```
GeneratedRouteBuilderExtensions.g.cs(73,85): error CS0246: The type or namespace name 'TUser' could not be found (are you missing a using directive or an assembly reference?)
GeneratedRouteBuilderExtensions.g.cs(88,33): error CS0246: The type or namespace name 'TUser' could not be found (are you missing a using directive or an assembly reference?)
```
```csharp
internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapPost(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
[global::System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern,
global::System.Action> handler, // <---- Error!
[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)
{
// ...
handler(ic.GetArgument>(0)!);
// ...
var userManager_local = httpContext.RequestServices.GetRequiredService>();
// ...
var handler = (Func)del;
// ...
var jsonTypeInfo = (JsonTypeInfo)serializerOptions.GetTypeInfo(typeof(TUser));
```
Contributor guide
Assessment
This issue has not been assessed yet.