当前位置: 首页 > 知识库问答 >
问题:

在核心3.1 api swagger用户界面中,SwashBuckle使具有相同动词但多个路由asp.netendpoint的所有路由参数都是强制性的

路扬
2023-03-14

我正在开发<code>asp。netcore2.2项目并升级到asp。net core 3.1,还升级了Swashbelt。AspNetCore转换为5.0。升级后,我可以看到swagger生成的endpoint发生了变化。

我有一个[HttpDelete]的endpoint,有两个不同的路由,如下所示:

[HttpDelete("{id}")]
[HttpDelete("{id}/some/{anotherId}")]
public IActionResult Delete(int id, int anotherId) 
{
    return NoContent();
}

[HttpDelete(“{id}”)]

这里只需要< code>id参数。但是< code>id和< code>anotherId参数在这里也被标记为required。这是不对的。

[HttpDelete(“{id}/some/{anotherId}”)]

这里应该需要id另一个Id参数。这是正确的。

这是我的Startup.cs

配置服务:

services.AddVersionedApiExplorer(options =>
{
    options.GroupNameFormat = "'v'VV";
});

services.AddApiVersioning(options =>
{
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.DefaultApiVersion = new ApiVersion(1, 0);
    options.ReportApiVersions = true;
    options.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
});

var apiVersionDescriptionProvider =
    services.BuildServiceProvider().GetService<IApiVersionDescriptionProvider>();

services
    .AddSwaggerGen(options =>
{
    foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
    {
        options.SwaggerDoc(
            $"TestDocumentOpenAPISpecification{description.GroupName}",
            new Microsoft.OpenApi.Models.OpenApiInfo
            {
                Title = "Test Document API",
                Version = description.ApiVersion.ToString(),
                Description = "Test",
                Contact = new Microsoft.OpenApi.Models.OpenApiContact
                {
                    Email = "Test@test.com",
                    Name = "Test Team",
                    Url = new Uri("https://www.test.com")
                }
            });
    }

    options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Description = "Input your JWT Authorization header to access this API. Example: \"Authorization: Bearer {token}\"",
        Name = "Authorization",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.ApiKey,
        Scheme = "Bearer"
    });
    options.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[] { }
        }
    });

    options.DocInclusionPredicate((documentName, apiDescription) =>
    {
        var actionApiVersionModel = apiDescription.ActionDescriptor
        .GetApiVersionModel(ApiVersionMapping.Explicit | ApiVersionMapping.Implicit);

        if (actionApiVersionModel == null)
        {
            return true;
        }

        if (actionApiVersionModel.DeclaredApiVersions.Any())
        {
            return actionApiVersionModel.DeclaredApiVersions.Any(v =>
            $"TestDocumentOpenAPISpecificationv{v.ToString()}" == documentName);
        }

        return actionApiVersionModel.ImplementedApiVersions.Any(v =>
            $"TestDocumentOpenAPISpecificationv{v.ToString()}" == documentName);
    });

    //var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    //var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFile);

    //options.IncludeXmlComments(xmlCommentsFullPath);
});

配置:

app.UseSwagger();

app.UseSwaggerUI(options =>
{
    foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
    {
        options.SwaggerEndpoint(
            $"/swagger/TestDocumentOpenAPISpecification{description.GroupName}/swagger.json",
            $"Test Document API - {description.GroupName.ToUpperInvariant()}");
    }
    options.RoutePrefix = string.Empty;

    options.DefaultModelExpandDepth(2);
    options.DefaultModelRendering(Swashbuckle.AspNetCore.SwaggerUI.ModelRendering.Model);
    options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
    options.DisplayRequestDuration();
    options.EnableValidator();
    options.EnableFilter();
    options.EnableDeepLinking();
    options.DisplayOperationId();
});

生成的swagger使另一个id在两条路由中都是强制性的。以前不是这样的。我尝试向两条路由添加Name,但仍然失败。请协助我的错误。

共有1个答案

姚和顺
2023-03-14

经过一些分析,我使用IOperationFilter使其工作。

public class DeleteOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.HttpMethod == "DELETE" && context.MethodInfo.Name == "Delete")
        {
            foreach (var parameter in context.ApiDescription.ParameterDescriptions)
            {
                if (parameter.RouteInfo == null)
                {
                    operation.Parameters.Single(x => x.Name.Equals(parameter.Name)).Required = false;
                }
            }
            return;
        }
    }
}

并将其添加到ConfigureServices中,

services.AddSwaggerGen(options =>
{
    ...
    options.OperationFilter<DeleteOperationFilter>();
};

我不确定这是最好的方法,但这是可行的。如果我错了,请纠正我。

 类似资料:
  • 我有寻呼路线/电影吗?categoryId=21213在这个页面上,我有一部分演员,点击它应该重定向到/movies?categoryId=21213/演员?actorId=23434234 我应该如何正确地描述用一个actor来渲染我最新的组件?我试过了 但这不起作用

  • 我在web.php添加了以下路线,但它不起作用。 我的控制器如下所示,我正在使用Ajax发送数据,但收到的错误是Method not allowed exception。 Ajax代码----------------

  • 将路由链接到参数 显示特定产品详细信息的组件的路由需要该产品ID的路由参数。我们可以使用以下实现: 注意:product-details路由的路径中的 ,它将参数放在路径中。例如,要查看ID为5的产品的产品详细信息页面,必须使用以下URL:localhost:3000/product-details/5 注意,指令传递一个数组,该数组指定路径和路由参数。或者,我们可以使用JS跳转: Product

  • 我遇到了一个问题,当使用相同的根查询在两个相邻的react-router-中继路由组件中请求数据时,第二个路由组件中的graph ql服务器查询使先前从第一个路由组件中检索到的数据无效。 例如: 我正在使用一种解决方法将字段附加到“查看器”根节点,以便从根字段查询数据数组: 我的schema.js包括查看器(用户)类型和根的以下定义: 和根: 现在,在main.jsx应用切入点中,我定义了以下路线

  • 简介 Apache ShardingSphere 使用 ThreadLocal 管理分片键值进行强制路由。 可以通过编程的方式向 HintManager 中添加分片值,该分片值仅在当前线程内生效。 Hint 的主要使用场景: 分片字段不存在 SQL 和数据库表结构中,而存在于外部业务逻辑。 强制在主库进行某些数据操作。 使用方法 使用 Hint 分片 规则配置 Hint 分片算法需要用户实现 or

  • 在一条骆驼路线中,我有两个url调用,调用两个不同的应用程序。 两者都能够抛出。因此,如果URL1抛出我必须处理的异常并将交换体设置为“数据源1不可用”,并且如果URL2抛出相同的异常,我想显示不同的消息。 如何使用onException处理此问题

  • 问题内容: 我有两个路线组,“动漫”和“漫画”。这些URL是/ anime/或/manga/,但它们都共享完全相同的控制器和模板 (唯一不同的是用于每个模板的配色方案,但这些颜色是在检查是否通过过滤器的过滤器中确定的)正在查看的特定项目是动漫还是漫画) : 动漫陈述定义: 漫画状态定义: 如您所见,其中已经有很多重复,我一点都不喜欢。随着我不断添加新路线,重复次数只会增加 (您已经可以看到mang

  • 我想简化一段我的代码,但不确定如何做。我有 所以我想让这四个应用一起做同样的事情。我开始考虑为结果做一个函数,如下所示: 但我认为如果我把所有的应用程序合并在一起做同样的事情会更好。知道吗? 谢谢,