Swashbuckle + ASP.Net Core WebApi: Swagger Document 不包含 Request-Header 或 QueryParameter 用于版本选择?

Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection?

我使用 ASP.Net Core WebApi、Swashbuckle 和 Microsoft.AspNetCore.Mvc.Versioning 来记录和控制我的 API.

到目前为止,版本控制也有效。

我的问题:

生成的 Swagger UI 文档不包含用于确定端点版本的参数(Request-Header 或查询参数)。因此,当我在 Swagger-Document 中按“执行”时,为端点选择了错误的版本(默认版本)。

准确地说:

Swagger 执行这个请求:https://localhost:5001/values

但是,它应该执行这个请求:https://localhost:5001/values?api-version=2.0

代码:

控制器:

[ApiController]
[Route("[controller]")]
[SwaggerTag("Gets some values. Have fun with it")]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
public class ValuesController : ControllerBase
{
    public ValuesController()
    {
    }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 10</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("1.0")]
  public async Task<IActionResult> Get()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 20</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerOperation(Tags = new[] { "Values", "Changed Endpoints" })]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("2.0")]
  public async Task<IActionResult> Getv2()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 });
  }

启用版本控制:

        services.AddApiVersioning(config =>
        {
            config.DefaultApiVersion = new ApiVersion(1, 0);
            config.AssumeDefaultVersionWhenUnspecified = true;
            config.ReportApiVersions = true;

            config.ApiVersionReader = ApiVersionReader.Combine(new QueryStringApiVersionReader(),
             new HeaderApiVersionReader()
             {
                 HeaderNames = { "x-api-version" }
             });
        });

启用 SwaggerGen:

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("1.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.SwaggerDoc("2.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.EnableAnnotations();
            c.IncludeXmlComments(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "OpenApi.xml"));
            c.DocInclusionPredicate((docName, apiDesc) =>
            {
                if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;

                var versions = methodInfo.GetCustomAttributes(true)
                    .OfType<Microsoft.AspNetCore.Mvc.MapToApiVersionAttribute>()
                    .SelectMany(attr => attr.Versions).ToList();

                return versions.Any(v => v.ToString() == docName);
            });
        });

有人可以帮助我吗?

通过添加解决此问题:

     services.AddVersionedApiExplorer(options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.DefaultApiVersion = new ApiVersion(1, 0);
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.DefaultApiVersionParameterDescription = "Do NOT modify api-version!";
        });

现已添加参数。但不幸的是,没有自动填充默认值。有人有想法吗?