【发布时间】:2022-01-12 00:00:08
【问题描述】:
我正在开发 API,我的控制器只返回 JsonDocument 而不是 DTO。
我需要为 API 文档提供一些响应示例。但是示例应该专门针对特定的端点。
我创建了示例提供程序IExampleProvider<JsonDocument> - 不幸的是,该示例为所有 JsonDocument 填充,而不是唯一一个由属性修饰的示例:
[SwaggerResponseExample(200, typeof(MySpecificResponseExample))]
即使没有指定上述属性,也包括示例文档。
Startup.cs:
c.ExampleFilters();
c.OperationFilter<AddResponseHeadersFilter>();
services.AddSwaggerExamplesFromAssemblyOf<Startup>();
示例提供者:
public class (MySpecificResponseExample))] : IExamplesProvider<JsonDocument>
{
public JsonDocument GetExamples()
{
return JsonDocument.Parse(@"Example JSON is here.");
}
}
控制器:
[HttpGet]
[ProducesResponseType(typeof(JsonDocument), 200)]
[SwaggerResponseExample(200, typeof(MySpecificResponseExample))]
public async Task<IActionResult> GetSomething() {}
如何仅将示例包含到具有特定属性的端点?我有另一个指定 [ProducesResponseType(typeof(JsonDocument), 200)] 的端点,但我想省略 MySpecificResponseExample。
【问题讨论】:
标签: c# asp.net-web-api swagger-ui .net-5