【问题标题】:Swagger array of strings without name不带名称的 Swagger 字符串数组
【发布时间】:2023-03-26 16:30:01
【问题描述】:

目前我正在尝试为我的软件创建一个 swagger 文件。 现在我想为 timeRange 创建一个定义。 我的问题是这个数组看起来像这样:

timeRange: {
  "2016-01-15T09:00:00.000Z", // this is the start date
  "2017-01-15T09:00:00.000Z" // this is the end date
}

如何创建开箱即用的示例值? 它是一个至少有两个的“字符串数组”。

"timeRange": {
  "type": "array",
  "items": {
    "type": "string",
    "example": "2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
  }
}

这会生成一个这样的示例:

"timeRange": [
  "2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
]

这个例子不起作用,因为它是一个数组而不是一个对象。 全部一起: 如何实现一个存在于两个不同字符串(没有名称)中的示例值。

希望你能帮助我! 干杯!

【问题讨论】:

    标签: arrays api documentation swagger swagger-ui


    【解决方案1】:
    timeRange: {
      "2016-01-15T09:00:00.000Z", // this is the start date
      "2017-01-15T09:00:00.000Z" // this is the end date
    }
    

    不是有效的 JSON——"timeRange" 需要用引号括起来,并且对象/数组的语法应该不同。

    如果使用对象语法{},值需要命名为属性:

    "timeRange": {
      "start_date": "2016-01-15T09:00:00.000Z",
      "end_date": "2017-01-15T09:00:00.000Z"
    }
    

    否则timeRange 需要是[] 数组:

    "timeRange": [
      "2016-01-15T09:00:00.000Z",
      "2017-01-15T09:00:00.000Z"
    ]
    


    在第一个示例({} 对象)中,您的 Swagger 将如下所示,每个命名属性都有一个单独的 example:

    "timeRange": {
      "type": "object",
      "properties": {
        "start_date": {
          "type": "string",
          "format": "date-time",
          "example": "2016-01-15T09:00:00.000Z"
        },
        "end_date": {
          "type": "string",
          "format": "date-time",
          "example": "2017-01-15T09:00:00.000Z"
        }
      },
      "required": ["start_date", "end_date"]
    }
    

    如果是[] 数组,您可以指定数组级别的example,它是一个多项目数组:

    "timeRange": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "date-time"
      },
      "example": [
        "2016-01-15T09:00:00.000Z",
        "2017-01-15T09:00:00.000Z"
      ]
    }
    

    【讨论】:

    • 非常感谢。这正是我一直在寻找的。我一直在浏览 swagger 文档,但找不到这样的东西!
    猜你喜欢
    • 2018-06-04
    • 2020-09-08
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多