Skip to content

Support resolving MethodInfo from strongly-typed route handler delegate #53184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
codeWriter.WriteLine("var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;");
codeWriter.WriteLine("return new RequestDelegateResult(targetDelegate, metadata);");
codeWriter.EndBlockWithSemicolon();
codeWriter.WriteLine($"var castHandler = Cast(handler, {endpoint.EmitHandlerDelegateType()} => throw null!);");
codeWriter.WriteLine("return MapCore(");
codeWriter.Indent++;
codeWriter.WriteLine("endpoints,");
Expand All @@ -148,7 +149,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
codeWriter.WriteLine("handler,");
codeWriter.WriteLine($"{endpoint.EmitVerb()},");
codeWriter.WriteLine("populateMetadata,");
codeWriter.WriteLine("createRequestDelegate);");
codeWriter.WriteLine("createRequestDelegate,");
codeWriter.WriteLine("castHandler.Method);");
codeWriter.Indent--;
codeWriter.EndBlock();
return stringWriter.ToString();
Expand Down Expand Up @@ -271,7 +273,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return;
}
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 0);
foreach (var endpoint in endpointsCode)
{
codeWriter.WriteLine(endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,18 +513,19 @@ namespace Microsoft.AspNetCore.Http.Generated
file static class GeneratedRouteBuilderExtensionsCore
{
private static readonly JsonOptions FallbackJsonOptions = new();
{{GetVerbs(verbs)}}
{{endpoints}}
{{GetVerbs(verbs)}}
{{endpoints}}

internal static RouteHandlerBuilder MapCore(
this IEndpointRouteBuilder routes,
string pattern,
Delegate handler,
IEnumerable<string>? httpMethods,
MetadataPopulator populateMetadata,
RequestDelegateFactoryFunc createRequestDelegate)
RequestDelegateFactoryFunc createRequestDelegate,
MethodInfo methodInfo)
{
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate, methodInfo);
}

private static T Cast<T>(Delegate d, T _) where T : Delegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void EmitRequestHandler(this Endpoint endpoint, CodeWriter codeWri

if (endpoint.Parameters.Length > 0)
{
codeWriter.WriteLine(endpoint.Parameters.EmitParameterPreparation(endpoint.EmitterContext, codeWriter.Indent));
codeWriter.WriteLineNoTabs(endpoint.Parameters.EmitParameterPreparation(endpoint.EmitterContext, codeWriter.Indent));
}

codeWriter.WriteLine("if (wasParamCheckFailure)");
Expand Down Expand Up @@ -182,7 +182,7 @@ public static void EmitFilteredRequestHandler(this Endpoint endpoint, CodeWriter

if (endpoint.Parameters.Length > 0)
{
codeWriter.WriteLine(endpoint.Parameters.EmitParameterPreparation(endpoint.EmitterContext, codeWriter.Indent));
codeWriter.WriteLineNoTabs(endpoint.Parameters.EmitParameterPreparation(endpoint.EmitterContext, codeWriter.Indent));
}

codeWriter.WriteLine("if (wasParamCheckFailure)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ namespace Microsoft.AspNetCore.Http.Generated
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
return new RequestDelegateResult(targetDelegate, metadata);
};
var castHandler = Cast(handler, global::System.String () => throw null!);
return MapCore(
endpoints,
pattern,
handler,
GetVerb,
populateMetadata,
createRequestDelegate);
createRequestDelegate,
castHandler.Method);
}


Expand All @@ -155,9 +157,10 @@ namespace Microsoft.AspNetCore.Http.Generated
Delegate handler,
IEnumerable<string>? httpMethods,
MetadataPopulator populateMetadata,
RequestDelegateFactoryFunc createRequestDelegate)
RequestDelegateFactoryFunc createRequestDelegate,
MethodInfo methodInfo)
{
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate, methodInfo);
}

private static T Cast<T>(Delegate d, T _) where T : Delegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,15 @@ namespace Microsoft.AspNetCore.Http.Generated
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
return new RequestDelegateResult(targetDelegate, metadata);
};
var castHandler = Cast(handler, global::System.String (global::Microsoft.AspNetCore.Http.Generators.Tests.BindableClassWithNullReturn? arg0) => throw null!);
return MapCore(
endpoints,
pattern,
handler,
GetVerb,
populateMetadata,
createRequestDelegate);
createRequestDelegate,
castHandler.Method);
}

[InterceptsLocation(@"TestMapActions.cs", 29, 5)]
Expand Down Expand Up @@ -265,13 +267,15 @@ namespace Microsoft.AspNetCore.Http.Generated
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
return new RequestDelegateResult(targetDelegate, metadata);
};
var castHandler = Cast(handler, global::System.String (global::Microsoft.AspNetCore.Http.Generators.Tests.BindableStructWithNullReturn arg0) => throw null!);
return MapCore(
endpoints,
pattern,
handler,
GetVerb,
populateMetadata,
createRequestDelegate);
createRequestDelegate,
castHandler.Method);
}


Expand All @@ -282,9 +286,10 @@ namespace Microsoft.AspNetCore.Http.Generated
Delegate handler,
IEnumerable<string>? httpMethods,
MetadataPopulator populateMetadata,
RequestDelegateFactoryFunc createRequestDelegate)
RequestDelegateFactoryFunc createRequestDelegate,
MethodInfo methodInfo)
{
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate);
return RouteHandlerServices.Map(routes, pattern, handler, httpMethods, populateMetadata, createRequestDelegate, methodInfo);
}

private static T Cast<T>(Delegate d, T _) where T : Delegate
Expand Down
Loading