Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

SwaggerGenOptions.IncludeXmlComments does not support /// <inheritdoc />

Open Sour-Codes opened this issue 2 years ago • 5 comments

To replicate:

  1. Create a new ASP.NET Core Web API project.

dotnet new webapi

  1. Set <GenerateDocumentationFile> to true in .csproj.

  2. In Program.cs, replace builder.Services.AddSwaggerGen(); with:


builder.Services.AddSwaggerGen(
    swaggerGenConfiguration =>
    {
        var assembly = System
            .Reflection
            .Assembly
            .GetExecutingAssembly()
            ;
        var assemblyName = assembly
            .GetName()
            .Name
            ;
		var xmlFile = $"{assemblyName}.xml";
		var xmlPath = Path.Combine(
			AppContext.BaseDirectory,
			xmlFile
		);

        swaggerGenConfiguration.IncludeXmlComments(
            xmlPath
        );
    }
);

  1. Insert the following before [HttpGet(Name = "GetWeatherForecast")]:

/// <summary>
///     This is my XML documentation comment!
/// </summary>

  1. Modify [HttpGet(Name = "GetWeatherForecast")] with the following:

[HttpGet("GetWeatherForecast")]

  1. Create a new type that extends WeatherForecastController, such as:

/// <summary>
///     Extends <see cref="WeatherForecastController" />.
/// </summary>
public class WeatherForecastController2
    : WeatherForecastController
{
    /// <summary>
    ///     Create a new instance.
    /// </summary>
    /// <param name="logger">
    ///     The logger.
    /// </param>
	public WeatherForecastController2(
		ILogger<WeatherForecastController> logger
	)
		: base(
			logger
		)
	{

	}

	/// <inheritdoc />
	public override IEnumerable<WeatherForecast> Get()
		=> base.Get();
}

  1. Run the project

dotnet run

  1. Navigate to Swagger UI.
  2. Observe how the deriving type does not show the XML documentation comment with the action.
  3. Replace the XML documentation of the deriving type with the following:

/// <summary>
///     This is my XML documentation comment without inheriting from the base type!
/// </summary>

  1. Run the project

dotnet run

  1. Observe how the deriving type does show the replaced XML documentation comment with the action.

Sour-Codes avatar Jan 26 '23 23:01 Sour-Codes