`SwaggerRequestExample` 被忽略

Mat*_*LES 5 c# swagger swashbuckle asp.net-core-2.0

当我向 API 添加 swagger 时,我想获得默认值和响应示例。我添加了 NuGet 包并尝试遵循本教程。该SwaggerResponseExample属性工作正常,但SwaggerRequestExample似乎被简单地忽略了。

我的行动定义如下

[SwaggerRequestExample(typeof(int), typeof(PersonExamples.Request))]
[SwaggerResponseExample(200, typeof(PersonExamples.Response))]
/* more attribute & stuff */
public IActionResult Get(int id) { /* blabla */ }
Run Code Online (Sandbox Code Playgroud)

PersonExamples定义如下的类(删除了非相关代码)

public class PersonExamples
{
    public class Request : IExamplesProvider
    {
        public object GetExamples() { return _persons.List().First().Id; }
    }

    public class Response : IExamplesProvider
    {
        public object GetExamples() { return _persons.List().First(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里也是相关的部分 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(conf =>
    {
        conf.SwaggerDoc(_documentationPrefix, new Info
        {
            Title = "Global",
            Version = "v0",
        });

        conf.OperationFilter<ExamplesOperationFilter>();

        var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Global.xml");
        conf.IncludeXmlComments(filePath);
    });

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSwagger(opt =>
    {
        opt.RouteTemplate = "{documentName}/swagger.json";
    });

    if (env.IsDevelopment())
    {
        app.UseSwaggerUI(conf =>
        {
            conf.SwaggerEndpoint($"/{_documentationPrefix}/swagger.json", "DataService API");
            conf.RoutePrefix = "doc/swagger";
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行我的项目并转到 swagger.json 页面时,我注意到响应示例编写正确,但找不到请求示例。进一步调试后,我注意到在PersonExamples.Response.GetExamples调用页面时会命中放置在其中的断点,但放置在PersonExamples.Request.GetExamples方法中的断点不会。所以我相信该SwaggerRequestExample属性永远不会调用该方法,甚至可能不会被调用本身。

我是否错误地使用了标签?为什么它永远不会被调用?

小智 2

我知道这个问题已经很老了,但是 Swagger Examples 不支持 GET 请求参数(查询参数)。仅当参数位于请求正文中时才有效(例如:POST 请求)