Tomáš Zajda
Tomáš Zajda

Reputation: 147

Adding comments to Example Value in Swagger UI

I am trying to get summary comments from my code into the example value field in Swagger UI. However, the comments are only visible in the Schema field. Is there a way to include the comments in the example value as well (e.g. as an extra parameter or an extra line)? I am using just a basic Weather Forecast template code with Swashbuckle.

This is my GET endpoint:

        [HttpGet("weather")]
        [Produces("application/vnd.ms-excel")]
        public IEnumerable<WeatherForecast> Get()
        {
          ...
        }

This is my model:

    /// <summary>
    ///     Object containing the weather forecast data.
    /// </summary>
    public class WeatherForecast
    {
        /// <summary>
        ///     Date of the forecast.
        /// </summary>
        /// <example>01/01/2022 12:00:00</example>
        public DateTime Date { get; set; }

        /// <summary>
        ///     Temperature in Celsius.
        /// </summary>
         /// <example>20</example>>
        public int TemperatureC { get; set; }

        /// <summary>
        ///     Temperature in Fahrenheit.
        /// </summary>
        /// <example>68</example>
        public int TemperatureF => 32 + (int) (TemperatureC / 0.5556);

        /// <summary>
        ///     Summary of the forecast.
        /// </summary>
        /// <example>Chilly</example>
        public string? Summary { get; set; }
    }

And this is my Swagger config:

builder.Services.AddSwaggerGen(c =>
{
    c.IncludeXmlComments(string.Format(@"{0}MyAPIProject.xml",AppDomain.CurrentDomain.BaseDirectory));
    c.ExampleFilters();
});

Upvotes: 0

Views: 6980

Answers (1)

user123456
user123456

Reputation: 2659

try adding this according to the documentation https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-6.0&tabs=visual-studio

 <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1591</NoWarn>
    </PropertyGroup>

Upvotes: 1

Related Questions