From 9b7c0dd089784af1dd6c3f5104e5d208be15c72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Garc=C3=ADa=20de=20la=20Noceda=20Arg=C3=BCelles?= Date: Sun, 23 Jun 2024 14:00:23 +0200 Subject: [PATCH] Fix parameter filter for Minimal APIs (#2962) Fix `InvalidCastException` for Minimal APIs when retrieving `[SwaggerParameter]`. --- .../AnnotationsParameterFilter.cs | 5 ++--- test/WebSites/WebApi/Program.cs | 15 ++++++++++++++- test/WebSites/WebApi/WebApi.csproj | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs b/src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs index 1c0304042a..2990329382 100644 --- a/src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs +++ b/src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs @@ -31,8 +31,7 @@ private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo p private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo) { - var swaggerParameterAttribute = parameterInfo.GetCustomAttributes() - .FirstOrDefault(); + var swaggerParameterAttribute = parameterInfo.GetCustomAttribute(); if (swaggerParameterAttribute != null) ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute); @@ -47,4 +46,4 @@ private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerP parameter.Required = swaggerParameterAttribute.RequiredFlag.Value; } } -} \ No newline at end of file +} diff --git a/test/WebSites/WebApi/Program.cs b/test/WebSites/WebApi/Program.cs index dc5b28abe7..834c8a8c49 100644 --- a/test/WebSites/WebApi/Program.cs +++ b/test/WebSites/WebApi/Program.cs @@ -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" }); }); @@ -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)), @@ -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 { diff --git a/test/WebSites/WebApi/WebApi.csproj b/test/WebSites/WebApi/WebApi.csproj index 727da5ac8b..5f0339397b 100644 --- a/test/WebSites/WebApi/WebApi.csproj +++ b/test/WebSites/WebApi/WebApi.csproj @@ -8,6 +8,7 @@ +