繁体   English   中英

Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 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 文档不包含用于确定 Endpoint 版本的参数(Request-Header 或 Query Parameter)。 因此,当我在 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!";
        });

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM