【问题标题】:Document possible values for a string using Swagger/C#?使用 Swagger/C# 记录字符串的可能值?
【发布时间】:2019-01-28 11:27:22
【问题描述】:

有没有一种方法可以在方法摘要 XML 上列出字符串参数的可能值并将其记录在 Swagger 上?

就像在下面的方法中,myParam 的可能值是 YN,我想把它放到我的 Swagger 文档中。

/// <summary>
/// My method.
/// </summary>
/// <param name="myParam">String parameter with predefined values.</param>
/// <returns>Something.</returns>
[HttpGet]
[ProducesResponseType(typeof(string), 200)]
public JsonResult MyMethod(string myParam)
{
   ...
}

【问题讨论】:

  • 如果您只有一组特定的值,那么您应该使用不同的数据类型。例如,在这种情况下为 bool,或者如果您有更多值,则为 enum
  • 这里是 link 在 Swashbuckle Github 存储库中的评论。
  • 我明白这一点,但我无法改变这一点,我只是在维护代码(记录它)。
  • 这是 Swashbuckle 还是 Swagger-Net 还是其他什么?
  • 看看this helps.

标签: c# xml swagger documentation summary


【解决方案1】:

有没有一种方法可以在方法摘要 XML 中列出 字符串参数的可能值并将其记录在 Swagger 上?

不,你不能这样做

然而,swagger 定义了一个支持rich formattingdescription 字段,您可以使用它来描述您的参数以及可能传入的允许值。任何使用API​​ 定义的人都可以看到。

类似:

paths:
  /MyMethod/myParam:
    get:
      summary: Gets a user by ID.
      parameters:
        - in: path
          name: myParam
          type: string
          required: true
          description: Must be in the form of "X" or "Y"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多