Skip to content

Commit

Permalink
Merge pull request #2356 from alexjephsonsw/minimalapi-enhancements
Browse files Browse the repository at this point in the history
Add MethodInfo discovery for Minimal API
  • Loading branch information
domaindrivendev committed Mar 2, 2022
2 parents 43b1b34 + b36b178 commit bb118db
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodInfo>()
.FirstOrDefault();

return methodInfo != null;
}
#endif

methodInfo = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FakeController>(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<object> { 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit bb118db

Please sign in to comment.