【问题标题】:C# swagger documentation for individual members of the request body object请求正文对象的各个成员的 C# swagger 文档
【发布时间】:2021-11-24 08:53:38
【问题描述】:

我们正在使用 /// cmets 通过以下方式编写 swagger 文档:

/// <summary>
/// Create a new widget
/// </summary>
/// <param name="widget"></param>
[HttpPost("/create")]
[ProducesResponseType(typeof(IPayment), 200)]
[ProducesResponseType(typeof(ErrorResult), 400)]
[ProducesResponseType(typeof(ErrorResult), 404)]
public Task<IActionResult> CreateWidget([FromBody] Widget widget)
{

现在 Widget 是 IWidget 的一个实现,文档的用户应该详细了解 Widget / IWidget 的每个数据成员的含义,什么是强制性的,什么是可选的,以及有效值。

我们发现添加这个描述的唯一地方是在

/// <param name="widget">very big multi line description</param>

虽然这对最终用户有效,但有没有更好的方法?我们之所以这样问,是因为如果在类/接口定义中内联提供描述,则更易于维护。

【问题讨论】:

    标签: c# swagger asp.net-core-2.1 swashbuckle


    【解决方案1】:

    与使用/// cmets 记录您的操作的方式相同,您还可以记录您的模型

    这是一个例子:
    http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Location#/Location/Location_Get2

    代码如下:

    /// <summary>
    /// ## Geographic coordinates
    /// ___
    /// A **geographic coordinate system** is a coordinate system used in geography that enables every location on Earth
    /// </summary>
    public class Location
    {
        /// <summary>**Latitude**: a geographic coordinate that specifies the north–south position of a point on the Earth's surface.</summary>
        /// <example>25.85</example>
        [Range(-90, 90)]
        public float Lat { get; set; }
    
        /// <summary>**Longitude**: a geographic coordinate that specifies the east-west position of a point on the Earth's surface.</summary>
        /// <example>-80.27</example>
        [Range(-180, 180)]
        public float Lon { get; set; }
    }
    

    看起来是这样的:


    在发布或放置请求时,文档也可以在模型选项卡上看到:

    https://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Company

    和往常一样在模特下:

    【讨论】:

    • 当模型被标记为 [FromRoute][FromQuery] 时有效,但它不适用于绑定 [FromBody] 的模型,这是 OP 所要求的
    • 你确定@Ian 吗?据我所见,模型部分中有文档,标签无关紧要......我将从我发布的同一个链接向这个答案添加几张图片
    • 我很抱歉@Helder,你是对的。我很困惑,因为链接显示了路由参数,我希望在同一个地方看到身体参数。我没有检查模型(或在我的页面“架构”)视图。不过我还是觉得太隐蔽了。如果 Swashbuckle 允许我们以与路由参数相同的方式记录主体参数,那就太好了。
    • 此外,我的身体参数是数组,这使事情变得复杂。他们没有在示例正文中显示任何摘要/备注/示例文本,这导致了我的误解。甚至模式视图也只显示摘要。数组(内置类型)将忽略备注/示例。
    • @Ian 您可能不知道 Swashbuckle 和 SwaggerUI 之间有明显的区别...... Swashbuckle 只生成符合 OpenApi/swagger 的文档:swagger-net-test.azurewebsites.net/swagger/docs/V1 ... SwaggerUI “吃掉”该文档和吐出我们看到的精彩 UI,如果您想在此处进行改进,请在此处请求:github.com/swagger-api/swagger-ui/issues
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多