RequestDelegateGenerator.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Collections.Immutable;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Microsoft.AspNetCore.Analyzers.Infrastructure;
  9. using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
  10. using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
  11. using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;
  12. using Microsoft.CodeAnalysis;
  13. using Microsoft.CodeAnalysis.CSharp;
  14. using Microsoft.CodeAnalysis.Operations;
  15. namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator;
  16. [Generator]
  17. public sealed class RequestDelegateGenerator : IIncrementalGenerator
  18. {
  19. public void Initialize(IncrementalGeneratorInitializationContext context)
  20. {
  21. var endpointsWithDiagnostics = context.SyntaxProvider.CreateSyntaxProvider(
  22. predicate: static (node, _) => node.TryGetMapMethodName(out var method) && InvocationOperationExtensions.KnownMethods.Contains(method),
  23. transform: static (context, token) =>
  24. {
  25. var operation = context.SemanticModel.GetOperation(context.Node, token);
  26. var wellKnownTypes = WellKnownTypes.GetOrCreate(context.SemanticModel.Compilation);
  27. if (operation.IsValidOperation(wellKnownTypes, out var invocationOperation))
  28. {
  29. return new Endpoint(invocationOperation, wellKnownTypes, context.SemanticModel);
  30. }
  31. return null;
  32. })
  33. .Where(static endpoint => endpoint != null)
  34. .Select((endpoint, _) =>
  35. {
  36. AnalyzerDebug.Assert(endpoint != null, "Invalid endpoints should not be processed.");
  37. return endpoint;
  38. })
  39. .WithTrackingName(GeneratorSteps.EndpointModelStep);
  40. context.RegisterSourceOutput(endpointsWithDiagnostics, (context, endpoint) =>
  41. {
  42. foreach (var diagnostic in endpoint.Diagnostics)
  43. {
  44. context.ReportDiagnostic(diagnostic);
  45. }
  46. });
  47. var endpoints = endpointsWithDiagnostics
  48. .Where(endpoint => endpoint.Diagnostics.Count == 0)
  49. .WithTrackingName(GeneratorSteps.EndpointsWithoutDiagnosicsStep);
  50. var interceptorDefinitions = endpoints
  51. .GroupWith((endpoint) => endpoint.Location, EndpointDelegateComparer.Instance)
  52. .Select((endpointWithLocations, _) =>
  53. {
  54. var endpoint = endpointWithLocations.Source;
  55. using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
  56. using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
  57. foreach (var location in endpointWithLocations.Elements)
  58. {
  59. codeWriter.WriteLine($$"""[InterceptsLocation(@"{{location.File}}", {{location.LineNumber}}, {{location.CharacterNumber}})]""");
  60. }
  61. codeWriter.WriteLine($"internal static RouteHandlerBuilder {endpoint.HttpMethod}{endpointWithLocations.Index}(");
  62. codeWriter.Indent++;
  63. codeWriter.WriteLine("this IEndpointRouteBuilder endpoints,");
  64. // MapFallback overloads that only take a delegate do not need a pattern argument
  65. if (endpoint.HttpMethod != "MapFallback" || endpoint.Operation.Arguments.Length != 2)
  66. {
  67. codeWriter.WriteLine(@"[StringSyntax(""Route"")] string pattern,");
  68. }
  69. // MapMethods overloads define an additional `httpMethods` parameter
  70. if (endpoint.HttpMethod == "MapMethods")
  71. {
  72. codeWriter.WriteLine("IEnumerable<string> httpMethods,");
  73. }
  74. codeWriter.WriteLine("Delegate handler)");
  75. codeWriter.Indent--;
  76. codeWriter.StartBlock();
  77. codeWriter.WriteLine("MetadataPopulator populateMetadata = (methodInfo, options) =>");
  78. codeWriter.StartBlock();
  79. codeWriter.WriteLine(@"Debug.Assert(options != null, ""RequestDelegateFactoryOptions not found."");");
  80. codeWriter.WriteLine(@"Debug.Assert(options.EndpointBuilder != null, ""EndpointBuilder not found."");");
  81. codeWriter.WriteLine($"options.EndpointBuilder.Metadata.Add(new {RequestDelegateGeneratorSources.GeneratedCodeConstructor});");
  82. endpoint.EmitEndpointMetadataPopulation(codeWriter);
  83. codeWriter.WriteLine("return new RequestDelegateMetadataResult { EndpointMetadata = options.EndpointBuilder.Metadata.AsReadOnly() };");
  84. codeWriter.EndBlockWithSemicolon();
  85. codeWriter.WriteLine("RequestDelegateFactoryFunc createRequestDelegate = (del, options, inferredMetadataResult) =>");
  86. codeWriter.StartBlock();
  87. codeWriter.WriteLine(@"Debug.Assert(options != null, ""RequestDelegateFactoryOptions not found."");");
  88. codeWriter.WriteLine(@"Debug.Assert(options.EndpointBuilder != null, ""EndpointBuilder not found."");");
  89. codeWriter.WriteLine(@"Debug.Assert(options.EndpointBuilder.ApplicationServices != null, ""ApplicationServices not found."");");
  90. codeWriter.WriteLine(@"Debug.Assert(options.EndpointBuilder.FilterFactories != null, ""FilterFactories not found."");");
  91. codeWriter.WriteLine($"var handler = Cast(del, {endpoint.EmitHandlerDelegateType(considerOptionality: true)} => throw null!);");
  92. codeWriter.WriteLine("EndpointFilterDelegate? filteredInvocation = null;");
  93. codeWriter.WriteLine("var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices;");
  94. endpoint.EmitLoggingPreamble(codeWriter);
  95. endpoint.EmitJsonPreparation(codeWriter);
  96. endpoint.EmitRouteOrQueryResolver(codeWriter);
  97. endpoint.EmitJsonBodyOrServiceResolver(codeWriter);
  98. if (endpoint.NeedsParameterArray)
  99. {
  100. codeWriter.WriteLine("var parameters = del.Method.GetParameters();");
  101. }
  102. codeWriter.WriteLineNoTabs(string.Empty);
  103. codeWriter.WriteLine("if (options.EndpointBuilder.FilterFactories.Count > 0)");
  104. codeWriter.StartBlock();
  105. codeWriter.WriteLine(endpoint.Response?.IsAwaitable == true
  106. ? "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(async ic =>"
  107. : "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(ic =>");
  108. codeWriter.StartBlock();
  109. codeWriter.WriteLine("if (ic.HttpContext.Response.StatusCode == 400)");
  110. codeWriter.StartBlock();
  111. codeWriter.WriteLine(endpoint.Response?.IsAwaitable == true
  112. ? "return (object?)Results.Empty;"
  113. : "return ValueTask.FromResult<object?>(Results.Empty);");
  114. codeWriter.EndBlock();
  115. endpoint.EmitFilteredInvocation(codeWriter);
  116. codeWriter.EndBlockWithComma();
  117. codeWriter.WriteLine("options.EndpointBuilder,");
  118. codeWriter.WriteLine("handler.Method);");
  119. codeWriter.EndBlock();
  120. codeWriter.WriteLineNoTabs(string.Empty);
  121. endpoint.EmitRequestHandler(codeWriter);
  122. codeWriter.WriteLineNoTabs(string.Empty);
  123. endpoint.EmitFilteredRequestHandler(codeWriter);
  124. codeWriter.WriteLineNoTabs(string.Empty);
  125. codeWriter.WriteLine("RequestDelegate targetDelegate = filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;");
  126. codeWriter.WriteLine("var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;");
  127. codeWriter.WriteLine("return new RequestDelegateResult(targetDelegate, metadata);");
  128. codeWriter.EndBlockWithSemicolon();
  129. codeWriter.WriteLine("return MapCore(");
  130. codeWriter.Indent++;
  131. codeWriter.WriteLine("endpoints,");
  132. // For `MapFallback` overloads that only take a delegate, provide the assumed default
  133. // Otherwise, pass the pattern provided from the MapX invocation
  134. if (endpoint.HttpMethod != "MapFallback" && endpoint.Operation.Arguments.Length != 2)
  135. {
  136. codeWriter.WriteLine("pattern,");
  137. }
  138. else
  139. {
  140. codeWriter.WriteLine($"{SymbolDisplay.FormatLiteral("{*path:nonfile}", true)},");
  141. }
  142. codeWriter.WriteLine("handler,");
  143. codeWriter.WriteLine($"{endpoint.EmitVerb()},");
  144. codeWriter.WriteLine("populateMetadata,");
  145. codeWriter.WriteLine("createRequestDelegate);");
  146. codeWriter.Indent--;
  147. codeWriter.EndBlock();
  148. return stringWriter.ToString();
  149. });
  150. var httpVerbs = endpoints
  151. .Collect()
  152. .Select((endpoints, _) =>
  153. {
  154. return endpoints
  155. .Distinct(EndpointHttpMethodComparer.Instance)
  156. .Select(endpoint => endpoint.EmitterContext.HttpMethod!)
  157. .Where(verb => verb is not null)
  158. .ToImmutableHashSet();
  159. });
  160. var endpointHelpers = endpoints
  161. .Collect()
  162. .Select((endpoints, _) =>
  163. {
  164. var hasJsonBodyOrService = endpoints.Any(endpoint => endpoint.EmitterContext.HasJsonBodyOrService);
  165. var hasJsonBodyOrQuery = endpoints.Any(endpoint => endpoint.EmitterContext.HasJsonBodyOrQuery);
  166. var hasJsonBody = endpoints.Any(endpoint => endpoint.EmitterContext.HasJsonBody);
  167. var hasFormBody = endpoints.Any(endpoint => endpoint.EmitterContext.HasFormBody);
  168. var hasRouteOrQuery = endpoints.Any(endpoint => endpoint.EmitterContext.HasRouteOrQuery);
  169. var hasBindAsync = endpoints.Any(endpoint => endpoint.EmitterContext.HasBindAsync);
  170. var hasParsable = endpoints.Any(endpoint => endpoint.EmitterContext.HasParsable);
  171. var hasEndpointMetadataProvider = endpoints.Any(endpoint => endpoint.EmitterContext.HasEndpointMetadataProvider);
  172. var hasEndpointParameterMetadataProvider = endpoints.Any(endpoint => endpoint.EmitterContext.HasEndpointParameterMetadataProvider);
  173. var hasIResult = endpoints.Any(endpoint => endpoint.Response?.IsIResult == true);
  174. using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
  175. using var codeWriter = new CodeWriter(stringWriter, baseIndent: 0);
  176. if (hasRouteOrQuery)
  177. {
  178. codeWriter.WriteLine(RequestDelegateGeneratorSources.ResolveFromRouteOrQueryMethod);
  179. }
  180. if (hasJsonBody || hasJsonBodyOrService || hasJsonBodyOrQuery)
  181. {
  182. codeWriter.WriteLine(RequestDelegateGeneratorSources.TryResolveBodyAsyncMethod);
  183. }
  184. if (hasFormBody)
  185. {
  186. codeWriter.WriteLine(RequestDelegateGeneratorSources.TryResolveFormAsyncMethod);
  187. }
  188. if (hasBindAsync)
  189. {
  190. codeWriter.WriteLine(RequestDelegateGeneratorSources.BindAsyncMethod);
  191. }
  192. if (hasJsonBodyOrService)
  193. {
  194. codeWriter.WriteLine(RequestDelegateGeneratorSources.ResolveJsonBodyOrServiceMethod);
  195. }
  196. if (hasParsable)
  197. {
  198. codeWriter.WriteLine(RequestDelegateGeneratorSources.TryParseExplicitMethod);
  199. }
  200. if (hasIResult)
  201. {
  202. codeWriter.WriteLine(RequestDelegateGeneratorSources.ExecuteAsyncExplicitMethod);
  203. }
  204. if (hasEndpointMetadataProvider)
  205. {
  206. codeWriter.WriteLine(RequestDelegateGeneratorSources.PopulateEndpointMetadataMethod);
  207. }
  208. if (hasEndpointParameterMetadataProvider)
  209. {
  210. codeWriter.WriteLine(RequestDelegateGeneratorSources.PopulateEndpointParameterMetadataMethod);
  211. }
  212. return stringWriter.ToString();
  213. });
  214. var helperTypes = endpoints
  215. .Collect()
  216. .Select((endpoints, _) =>
  217. {
  218. var hasFormBody = endpoints.Any(endpoint => endpoint.EmitterContext.HasFormBody);
  219. var hasJsonBody = endpoints.Any(endpoint => endpoint.EmitterContext.HasJsonBody || endpoint.EmitterContext.HasJsonBodyOrService || endpoint.EmitterContext.HasJsonBodyOrQuery);
  220. var hasResponseMetadata = endpoints.Any(endpoint => endpoint.EmitterContext.HasResponseMetadata);
  221. var requiresPropertyAsParameterInfo = endpoints.Any(endpoint => endpoint.EmitterContext.RequiresPropertyAsParameterInfo);
  222. using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
  223. using var codeWriter = new CodeWriter(stringWriter, baseIndent: 0);
  224. if (hasFormBody || hasJsonBody || hasResponseMetadata)
  225. {
  226. codeWriter.WriteLine(RequestDelegateGeneratorSources.ContentTypeConstantsType);
  227. }
  228. if (requiresPropertyAsParameterInfo)
  229. {
  230. codeWriter.WriteLine(RequestDelegateGeneratorSources.PropertyAsParameterInfoClass);
  231. }
  232. return stringWriter.ToString();
  233. });
  234. var endpointsAndHelpers = interceptorDefinitions.Collect().Combine(endpointHelpers).Combine(httpVerbs).Combine(helperTypes);
  235. context.RegisterSourceOutput(endpointsAndHelpers, (context, sources) =>
  236. {
  237. var (((endpointsCode, helperMethods), httpVerbs), helperTypes) = sources;
  238. if (endpointsCode.IsDefaultOrEmpty)
  239. {
  240. return;
  241. }
  242. using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
  243. using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
  244. foreach (var endpoint in endpointsCode)
  245. {
  246. codeWriter.WriteLine(endpoint);
  247. }
  248. var code = RequestDelegateGeneratorSources.GetGeneratedRouteBuilderExtensionsSource(
  249. endpoints: stringWriter.ToString(),
  250. helperMethods: helperMethods ?? string.Empty,
  251. helperTypes: helperTypes ?? string.Empty,
  252. verbs: httpVerbs);
  253. context.AddSource("GeneratedRouteBuilderExtensions.g.cs", code);
  254. });
  255. }
  256. }