Skip to content

Commit

Permalink
Fix parameter filter for Minimal APIs (#2962)
Browse files Browse the repository at this point in the history
Fix `InvalidCastException` for Minimal APIs when retrieving `[SwaggerParameter]`.
  • Loading branch information
jgarciadelanoceda authored Jun 23, 2024
1 parent f6c1e31 commit 9b7c0dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo p
private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
{

var swaggerParameterAttribute = parameterInfo.GetCustomAttributes<SwaggerParameterAttribute>()
.FirstOrDefault();
var swaggerParameterAttribute = parameterInfo.GetCustomAttribute<SwaggerParameterAttribute>();

if (swaggerParameterAttribute != null)
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
Expand All @@ -47,4 +46,4 @@ private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerP
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;
}
}
}
}
15 changes: 14 additions & 1 deletion test/WebSites/WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.EnableAnnotations();
c.SwaggerDoc("v1", new() { Title = "WebApi", Version = "v1" });
});

Expand All @@ -20,7 +24,7 @@

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Expand All @@ -33,12 +37,21 @@
.WithName("GetWeatherForecast")
.WithOpenApi();

app.MapPost("/fruit/{id}", ([AsParameters] CreateFruitModel model) =>
{
return model.Fruit;
}).WithName("CreateFruit");

app.Run();

record struct CreateFruitModel
([FromRoute, SwaggerParameter(Description = "The id of the fruit that will be created", Required = true)] string Id,
[FromBody] Fruit Fruit);
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
record Fruit(string Name);

namespace WebApi
{
Expand Down
1 change: 1 addition & 0 deletions test/WebSites/WebApi/WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.Annotations\Swashbuckle.AspNetCore.Annotations.csproj" />
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.SwaggerGen\Swashbuckle.AspNetCore.SwaggerGen.csproj" />
<ProjectReference Include="..\..\..\src\Swashbuckle.AspNetCore.SwaggerUI\Swashbuckle.AspNetCore.SwaggerUI.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 9b7c0dd

Please sign in to comment.