Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsParameters throwing error on cast when showing the Description with EnableAnnotations #2962

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the stack trace of the exception that occurs without this change? Odd that one works but "first of possibly many" doesn't.

Copy link
Contributor Author

@jgarciadelanoceda jgarciadelanoceda Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a thing that is only happening for MinimalApi when the property has a SwaggerParameterAttribute on it. I think the problem is right here:
image

It is not able to cast the array of Attributes to a Ienumerable of SwaggerParameterAttribute. Since the SwaggerParamterAttribute can only be applied once for each parameter then there is no problem in adding it

The stackTrace is this one:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate Operation for action - HTTP: POST /fruit/{id}. See inner exception
       ---> System.InvalidCastException: Unable to cast object of type 'System.Attribute[]' to type 'System.Collections.Generic.IEnumerable`1[Swashbuckle.AspNetCore.Annotations.SwaggerParameterAttribute]'.
         at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](ParameterInfo element)
         at Swashbuckle.AspNetCore.Annotations.AnnotationsParameterFilter.ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.Annotations\AnnotationsParameterFilter.cs:line 34
         at Swashbuckle.AspNetCore.Annotations.AnnotationsParameterFilter.Apply(OpenApiParameter parameter, ParameterFilterContext context) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.Annotations\AnnotationsParameterFilter.cs:line 18
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameterAsync(ApiParameterDescription apiParameter, SchemaRepository schemaRepository) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 599
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParametersAsync(ApiDescription apiDescription, SchemaRepository schemaRespository, Func`3 parameterGenerator) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 490
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParametersAsync(ApiDescription apiDescription, SchemaRepository schemaRespository) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 508
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationAsync(ApiDescription apiDescription, SchemaRepository schemaRepository, Func`3 parametersGenerator, Func`3 bodyGenerator, Func`3 applyFilters) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 322
         --- End of inner exception stack trace ---
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationAsync(ApiDescription apiDescription, SchemaRepository schemaRepository, Func`3 parametersGenerator, Func`3 bodyGenerator, Func`3 applyFilters) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 345
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationAsync(ApiDescription apiDescription, SchemaRepository schemaRepository) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 373
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationsAsync(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 268
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePathsAsync(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository, Func`3 operationsGenerator) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 207
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePathsAsync(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 229
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(String documentName, String host, String basePath) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.SwaggerGen\SwaggerGenerator\SwaggerGenerator.cs:line 56
         at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) in C:\Git\Github\Swashbuckle.AspNetCore\src\Swashbuckle.AspNetCore.Swagger\SwaggerMiddleware.cs:line 64
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Copy link
Contributor Author

@jgarciadelanoceda jgarciadelanoceda Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange indeed!, because for Controller parameters this does not happen.


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