dotnet / dotnet/aspnetcore

RDG does not support generic types from outer scope

Open
#47,338 6 comments 2 reactions 1 assignee Claimed by @Youssef1313 View on GitHub
area-minimal bug feature-rdg
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.