'Render CSV example for swashbuckle

I have an API that can return an csv or json, deppending on the Accept header mimetype.

I've decorated the method with [Produces("application/json","text/csv")]. Here it is a MRE with the ASP.NET Core default project:

public class WeatherForecast
{
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    public string? Summary { get; set; }
}
[HttpGet(Name = "GetWeatherForecast")]
[Produces("application/json","text/csv")]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = "Freezing"
    })
    .ToArray();
}

This enables the mimetype selection in the Swagger UI, and it's being correct added to the Accept header of the request. My issue is that the example response value is not being formatted as csv, but as json.

Example value for the csv mimetype

How can I configure how examples are rendered for custom mimetypes, like text/csv?

I'm using Swashbuckle.AspNetCore Version 6.3.1.



Solution 1:[1]

You should use Swashbuckle.AspNetCore.Examples package.

And you can refer below post.

Editing media type in Swagger with Swashbuckle

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason