【问题标题】:How to insert a JSON code block for a Swagger-API using YAML?如何使用 YAML 为 Swagger-API 插入 JSON 代码块?
【发布时间】:2016-10-03 18:06:38
【问题描述】:

在我正在使用 YAML 编辑的招摇文档中。我打算使用 GFM 语法插入一个代码块,这是根据this document swagger 所期望的。

description: >-
  Some description of the object here.
  More Here. An example to for this is as follows:
  ```json
  {
    "Key": {
      "name": "myName",
      "id": 100
    }
  }
  ```

然而,这并没有显示为 JSON 格式,而是全部显示在一行中,如下所示:

Some description of the object here. More Here. An example to for this is as follows: ```json { "Key": { "name": "myName", "id": 100 } } ```

【问题讨论】:

  • 在第一组三重反引号后尝试将JSON(全部大写)作为您的语言标识符。 json(小写)在 Github 上不是一种有效的语言,所以如果 Swagger 严格遵循这一点,这对你来说可能是个问题。

标签: json yaml swagger


【解决方案1】:

这一切都以一行结束是因为您通过指定> 使用folded style block scalar- 用于剥离chomping 指示符)。

您要使用的是literal style block scalar,它会保留换行符和间距。您可能还想使用默认剪辑 chomping(在 JSON 代码末尾留一个换行符):

description: |
  Some description of the object here.
  More Here. An example to for this is as follows:
  ```json
  {
    "Key": {
      "name": "myName",
      "id": 100
    }
  }
  ```

(唯一的变化是第一行>-|

【讨论】:

  • 感谢您的回复安东。我之前尝试过使用文字样式的块缩放器,但仍然无法让它工作。我希望在示例 JSON 的 Swagger API 上看到的制表符/故意空格没有出现。名称 json 也会出现,代码块周围的引号也会出现
  • 选项卡不适用于大多数 YAML 解析器。我对 swagger 一无所知,所以我无法帮助您解决具体的问题。
  • Anthon 响应在 swagger 编辑器和 ui 中都能完美运行。这是swagger ui的屏幕截图imgur.com/A835iVe
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2019-06-24
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2012-01-17
相关资源
最近更新 更多