domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.27k stars 1.32k forks source link

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

Open Sour-Codes opened 1 year ago

Sour-Codes commented 1 year ago

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.
songzheng45 commented 1 year ago

I switched to NSwag in the end.

dotjpg3141 commented 1 year ago

There are some workarounds in https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/1000 for ASP.NET Core

Havunen commented 9 months ago

FYI this is supported in DotSwashbuckle

fprdkhti commented 8 months ago

I still can't use inheritdoc functionality when it inherits xml document from another assembly. thanks for any help.

Chaosflo commented 3 months ago

Any news when this will be pushed?