I'm using Swagger with serializing for OpenApi 3.0. In detail I try to serialize an input query parameter array with the following format
?myArray=1,2,3,4
I know how to achieve this in the swagger documentation. Just set parameter.explode=false
parameters:
- name: myArray
in: query
explode: false
...
But I don't know how to set this parameter property in ASP.Net Core 3.1 in my controller class to get my swagger doc generated correctly. I tried several things like implementing a filter based on IParameterFilter but nothing worked fine for me.
Fortunately I was able solve the issue as I found a discussion in Swashbuckle.AspNetCore on GitHub.
Implementing a custom parameter filter was the right way.
public class QueryArrayParameterFilter : IParameterFilter
{
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if (!parameter.In.HasValue || parameter.In.Value != ParameterLocation.Query)
return;
if (parameter.Schema?.Type == "array" && parameter.Name.Equals("myArray"))
{
var value = null as IOpenApiExtension;
parameter.Extensions.TryGetValue("explode", out value);
if (value == null)
parameter.Extensions.Add("explode", new OpenApiBoolean(false));
}
}
}
The filter has to be applied to the swagger gen in the ASP.Net core Startup.cs
services.AddSwaggerGen(c =>
{
c.ParameterFilter<QueryArrayParameterFilter>();
}