Skip to content

Commit 773d94d

Browse files
captainsafiaatossell91
authored andcommitted
Implement RouteHandlerServices.Map and use in RequestDelegateGenerator (dotnet#46180)
* Implement RouteHandlerServices.Map and use in RequestDelegateGenerator * Tweak generated code * Remove warnings from generated code * Fix docstring for RouteHandlerServices.Map * Add baseline tests and fix global:: issue * Clean up generated code * Add HTTP verb caching and fix usings * Use FQN and remove GeneratedCode attribute
1 parent 55634bb commit 773d94d

10 files changed

+476
-367
lines changed

src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public sealed class RequestDelegateGenerator : IIncrementalGenerator
2020
"MapPut",
2121
"MapDelete",
2222
"MapPatch",
23-
"Map",
2423
};
2524

2625
public void Initialize(IncrementalGeneratorInitializationContext context)
@@ -46,48 +45,54 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4645
.WithTrackingName("EndpointModel");
4746

4847
var thunks = endpoints.Select((endpoint, _) => $$"""
49-
[{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}}] = (
50-
(del, builder) =>
48+
[{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}}] = (
49+
(methodInfo, options) =>
5150
{
52-
builder.Metadata.Add(new SourceKey{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}});
51+
if (options == null)
52+
{
53+
return new RequestDelegateMetadataResult { EndpointMetadata = ReadOnlyCollection<object>.Empty };
54+
}
55+
options.EndpointBuilder.Metadata.Add(new SourceKey{{StaticRouteHandlerModelEmitter.EmitSourceKey(endpoint)}});
56+
return new RequestDelegateMetadataResult { EndpointMetadata = options.EndpointBuilder.Metadata.AsReadOnly() };
5357
},
54-
(del, builder) =>
58+
(del, options, inferredMetadataResult) =>
5559
{
5660
var handler = ({{StaticRouteHandlerModelEmitter.EmitHandlerDelegateType(endpoint)}})del;
5761
EndpointFilterDelegate? filteredInvocation = null;
5862
59-
if (builder.FilterFactories.Count > 0)
63+
if (options.EndpointBuilder.FilterFactories.Count > 0)
6064
{
6165
filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(ic =>
6266
{
6367
if (ic.HttpContext.Response.StatusCode == 400)
6468
{
65-
return System.Threading.Tasks.ValueTask.FromResult<object?>(Results.Empty);
69+
return ValueTask.FromResult<object?>(Results.Empty);
6670
}
6771
{{StaticRouteHandlerModelEmitter.EmitFilteredInvocation()}}
6872
},
69-
builder,
73+
options.EndpointBuilder,
7074
handler.Method);
7175
}
7276
7377
{{StaticRouteHandlerModelEmitter.EmitRequestHandler()}}
7478
{{StaticRouteHandlerModelEmitter.EmitFilteredRequestHandler()}}
7579
76-
return filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;
80+
RequestDelegate targetDelegate = filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;
81+
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
82+
return new RequestDelegateResult(targetDelegate, metadata);
7783
}),
7884
""");
7985

8086
var stronglyTypedEndpointDefinitions = endpoints.Select((endpoint, _) => $$"""
81-
{{RequestDelegateGeneratorSources.GeneratedCodeAttribute}}
82-
internal static Microsoft.AspNetCore.Builder.RouteHandlerBuilder {{endpoint.HttpMethod}}(
83-
this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
84-
[System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern,
85-
{{StaticRouteHandlerModelEmitter.EmitHandlerDelegateType(endpoint)}} handler,
86-
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
87-
[System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)
88-
{
89-
return GeneratedRouteBuilderExtensionsCore.MapCore(endpoints, pattern, handler, GetVerb, filePath, lineNumber);
90-
}
87+
internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder {{endpoint.HttpMethod}}(
88+
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
89+
[global::System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern,
90+
global::{{StaticRouteHandlerModelEmitter.EmitHandlerDelegateType(endpoint)}} handler,
91+
[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
92+
[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)
93+
{
94+
return global::Microsoft.AspNetCore.Http.Generated.GeneratedRouteBuilderExtensionsCore.MapCore(endpoints, pattern, handler, {{StaticRouteHandlerModelEmitter.EmitVerb(endpoint)}}, filePath, lineNumber);
95+
}
9196
""");
9297

9398
var thunksAndEndpoints = thunks.Collect().Combine(stronglyTypedEndpointDefinitions.Collect());

0 commit comments

Comments
 (0)