Skip to content

Commit

Permalink
Merge pull request #2224 from martincostello/Use-IParameterInfoParame…
Browse files Browse the repository at this point in the history
…terDescriptor

Use IParameterInfoParameterDescriptor for parameter info
  • Loading branch information
domaindrivendev committed Oct 5, 2021
2 parents a02a198 + 3c5791f commit 73610b9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ public static bool IsRequiredParameter(this ApiParameterDescription apiParameter

public static ParameterInfo ParameterInfo(this ApiParameterDescription apiParameter)
{
var controllerParameterDescriptor = apiParameter.ParameterDescriptor as ControllerParameterDescriptor;
return controllerParameterDescriptor?.ParameterInfo;
var parameterDescriptor = apiParameter.ParameterDescriptor as
#if NETCOREAPP2_2_OR_GREATER
Microsoft.AspNetCore.Mvc.Infrastructure.IParameterInfoParameterDescriptor;
#else
ControllerParameterDescriptor;
#endif

return parameterDescriptor?.ParameterInfo;
}

public static PropertyInfo PropertyInfo(this ApiParameterDescription apiParameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Xunit;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.TestSupport;
using Xunit;

namespace Swashbuckle.AspNetCore.Annotations.Test
{
public class AnnotationsRequestBodyFilterTests
{
[Fact]
public void Apply_EnrichesRequestBodyMetadata_IfParameterDecoratedWithSwaggerRequestBodyAttribute()
public void Apply_EnrichesRequestBodyMetadata_IfControllerParameterDecoratedWithSwaggerRequestBodyAttribute()
{
var requestBody = new OpenApiRequestBody();
var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
Expand All @@ -28,6 +31,25 @@ public void Apply_EnrichesRequestBodyMetadata_IfParameterDecoratedWithSwaggerReq
Assert.True(requestBody.Required);
}

[Fact]
public void Apply_EnrichesRequestBodyMetadata_IfEndpointParameterDecoratedWithSwaggerRequestBodyAttribute()
{
var requestBody = new OpenApiRequestBody();
var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerRequestBodyAttribute))
.GetParameters()[0];
var bodyParameterDescription = new ApiParameterDescription
{
ParameterDescriptor = new CustomParameterDescriptor { ParameterInfo = parameterInfo }
};
var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

Subject().Apply(requestBody, context);

Assert.Equal("Description for param", requestBody.Description);
Assert.True(requestBody.Required);
}

[Fact]
public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerRequestBodyAttribute()
{
Expand Down Expand Up @@ -67,5 +89,10 @@ private AnnotationsRequestBodyFilter Subject()
{
return new AnnotationsRequestBodyFilter();
}

private sealed class CustomParameterDescriptor : ParameterDescriptor, IParameterInfoParameterDescriptor
{
public ParameterInfo ParameterInfo { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ public static ApiDescription Create(
foreach (var parameter in parameterDescriptions)
{
// If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata
var controllerParameterDescriptor = actionDescriptor.Parameters
.OfType<ControllerParameterDescriptor>()
.FirstOrDefault(parameterDescriptor => parameterDescriptor.Name == parameter.Name);

if (controllerParameterDescriptor != null)
parameter.ParameterDescriptor = actionDescriptor.Parameters
.OfType<ParameterDescriptor>()
.Where(parameterDescriptor => parameterDescriptor.Name == parameter.Name)
.FirstOrDefault();

var parameterDescriptorWithParameterInfo = parameter.ParameterDescriptor as
#if NETCOREAPP2_2_OR_GREATER
Microsoft.AspNetCore.Mvc.Infrastructure.IParameterInfoParameterDescriptor;
#else
ControllerParameterDescriptor;
#endif

if (parameterDescriptorWithParameterInfo != null)
{
parameter.ParameterDescriptor = controllerParameterDescriptor;
parameter.ModelMetadata = ModelMetadataFactory.CreateForParameter(controllerParameterDescriptor.ParameterInfo);
parameter.ModelMetadata = ModelMetadataFactory.CreateForParameter(parameterDescriptorWithParameterInfo.ParameterInfo);
}

apiDescription.ParameterDescriptions.Add(parameter);
Expand Down

0 comments on commit 73610b9

Please sign in to comment.