From b36b1780bf68035f2b292507e61910e1cd3d665a Mon Sep 17 00:00:00 2001 From: Alex Jephson Date: Fri, 25 Feb 2022 13:22:21 +0000 Subject: [PATCH] Add MethodInfo discovery for Minimal API --- .../ApiDescriptionExtensions.cs | 11 +++++ .../ApiDescriptionExtensionsTests.cs | 47 +++++++++++++++++++ .../Fixtures/TestMinimalApiMethod.cs | 16 +++++++ 3 files changed, 74 insertions(+) create mode 100644 test/Swashbuckle.AspNetCore.SwaggerGen.Test/ApiDescriptionExtensions/ApiDescriptionExtensionsTests.cs create mode 100644 test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/TestMinimalApiMethod.cs diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiDescriptionExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiDescriptionExtensions.cs index d4145e94bd..c5c3673f4f 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiDescriptionExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiDescriptionExtensions.cs @@ -18,6 +18,17 @@ public static bool TryGetMethodInfo(this ApiDescription apiDescription, out Meth return true; } +#if NET6_0_OR_GREATER + if (apiDescription.ActionDescriptor?.EndpointMetadata != null) + { + methodInfo = apiDescription.ActionDescriptor.EndpointMetadata + .OfType() + .FirstOrDefault(); + + return methodInfo != null; + } +#endif + methodInfo = null; return false; } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/ApiDescriptionExtensions/ApiDescriptionExtensionsTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/ApiDescriptionExtensions/ApiDescriptionExtensionsTests.cs new file mode 100644 index 0000000000..9472b18f4e --- /dev/null +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/ApiDescriptionExtensions/ApiDescriptionExtensionsTests.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Mvc.Abstractions; +using Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures; +using Swashbuckle.AspNetCore.TestSupport; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Swashbuckle.AspNetCore.SwaggerGen.Test.ApiDescriptionExtensions +{ + public class ApiDescriptionExtensionsTests + { + [Fact] + public void TryGetMethodInfo_GetsMethodInfo_IfControllerActionDescriptor() + { + var apiDescription = ApiDescriptionFactory.Create(c => nameof(c.ActionWithNoParameters), groupName: "v1", httpMethod: "POST", relativePath: "/"); + + var result = apiDescription.TryGetMethodInfo(out var methodInfo); + + Assert.True(result); + Assert.NotNull(methodInfo); + } + + [Fact] + public void TryGetMethodInfo_GetsMethodInfo_IfEndpointActionDescriptor() + { + var testMethodInfo = typeof(TestMinimalApiMethod).GetMethod("RequestDelegate"); + + var actionDescriptor = new ActionDescriptor(); + actionDescriptor.EndpointMetadata = new List { testMethodInfo }; + actionDescriptor.Parameters = testMethodInfo + .GetParameters() + .Select(p => new ParameterDescriptor + { + Name = p.Name, + ParameterType = p.ParameterType + }) + .ToList(); + + var apiDescription = ApiDescriptionFactory.Create(actionDescriptor, testMethodInfo, groupName: "v1", httpMethod: "POST", relativePath: "/"); + + var result = apiDescription.TryGetMethodInfo(out var methodInfo); + + Assert.True(result); + Assert.NotNull(methodInfo); + } + } +} \ No newline at end of file diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/TestMinimalApiMethod.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/TestMinimalApiMethod.cs new file mode 100644 index 0000000000..6186cfc09a --- /dev/null +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/TestMinimalApiMethod.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures +{ + public class TestMinimalApiMethod + { + public static Task RequestDelegate(long id) + { + return Task.FromResult(id); + } + } +}