SwaggerRequestExample被忽略 [英] `SwaggerRequestExample` is being ignored
问题描述
当我向我的API添加大张旗鼓时,我想获取默认值和响应示例.我添加了NuGet软件包,并尝试遵循本教程. SwaggerResponseExample
属性可以正常工作,但SwaggerRequestExample
似乎只是被忽略了.
As I was adding swagger to my API, I wanted to get default values and response examples. I added the NuGet packages and tried to follow this tutorial. The SwaggerResponseExample
attribute works properly but the SwaggerRequestExample
seems to be simply ignored.
我的动作定义如下
[SwaggerRequestExample(typeof(int), typeof(PersonExamples.Request))]
[SwaggerResponseExample(200, typeof(PersonExamples.Response))]
/* more attribute & stuff */
public IActionResult Get(int id) { /* blabla */ }
PersonExamples
类的定义如下(已删除非启示性代码)
The PersonExamples
class being defined as follow (non-revelant code removed)
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(); }
}
}
这也是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";
});
}
}
当我运行项目并转到swagger.json页面时,我注意到响应示例已正确编写,但找不到请求示例.经过进一步调试后,我注意到在调用页面时会遇到放置在PersonExamples.Request.GetExamples
方法中的一个断点.因此,我相信SwaggerRequestExample
属性从不调用该方法,甚至可能不被调用.
When I run my project and go to the swagger.json page I notice that the response example is properly written, but the request example is nowhere to be found. After further debugging, I notice that a breakpoint placed in PersonExamples.Response.GetExamples
will be hit when the page is called, but one placed in the PersonExamples.Request.GetExamples
method won't. So i believe that the SwaggerRequestExample
attribute never calls the method and may not even be called itself.
我是否使用了不正确的标签?为什么从来不叫它?
Did I improperly used the tag ? Why is it never called ?
推荐答案
我知道这个问题已经很老了,但是Swagger示例不支持GET请求参数(查询参数).仅当参数在请求正文中(例如POST请求)时才有效
I know this question is quite old, but Swagger Examples don't support GET request parameters (query parameters). It only works when the parameters are in the request body (eg: POST requests)
这篇关于SwaggerRequestExample被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!