diff --git a/Net.Sdk.Web.Extensions.SourceGenerators.Tests/Routes/SimpleRoute2.cs b/Net.Sdk.Web.Extensions.SourceGenerators.Tests/Routes/SimpleRoute2.cs index 40a8888..43b13b8 100644 --- a/Net.Sdk.Web.Extensions.SourceGenerators.Tests/Routes/SimpleRoute2.cs +++ b/Net.Sdk.Web.Extensions.SourceGenerators.Tests/Routes/SimpleRoute2.cs @@ -4,7 +4,7 @@ public class SimpleRoute2 { [GeneratePost("somethingSimple")] - public async Task GetSomething() + public IResult GetSomething() { return Results.Ok(); } diff --git a/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj b/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj index 2b0177c..12251fe 100644 --- a/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj +++ b/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.8.16 + 0.8.17 latest true Alexandru Macocian diff --git a/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs b/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs index 9e1b680..e94620e 100644 --- a/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs +++ b/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs @@ -254,13 +254,27 @@ public class UseRoutesGenerator : IIncrementalGenerator var parameters = GetMethodParameters(methodDeclarationSyntax); var variables = string.Join(", ", methodDeclarationSyntax.ParameterList.Parameters .Select(param => param.Identifier.Text)); - - return @$" + + /* + * Generate an async lambda for Task or a synchronous lambda for IResult returns. If neither are matched, do not generate any method + */ + var returnTypeSymbol = semanticModel.GetSymbolInfo(methodDeclarationSyntax.ReturnType).Symbol as ITypeSymbol; + return returnTypeSymbol?.ToDisplayString() switch + { + "System.Threading.Tasks.Task" => @$" builder.Map{type}(""{pattern}"", async (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) => {{ var cancellationToken = httpContext.RequestAborted; return await route.{methodDeclarationSyntax.Identifier}({variables}); - }}){routeFilterSb};"; + }}){routeFilterSb};", + "Microsoft.AspNetCore.Http.IResult" => @$" + builder.Map{type}(""{pattern}"", (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) => + {{ + var cancellationToken = httpContext.RequestAborted; + return route.{methodDeclarationSyntax.Identifier}({variables}); + }}){routeFilterSb};", + _ => default + }; } private static string GetMethodParameters(MethodDeclarationSyntax methodDeclaration)