在Swagger编辑器启动时自动执行“将JSON转换为YAML”

时间:2019-08-27 11:42:38

标签: json api swagger openapi swagger-editor

我正在spec.js内每个SwaggerEditorBundle调用一个swagger json对象,以读取本地swagger api文档。

在Swagger编辑器中,swagger json代码格式错误,因此我需要单击 Editor->将JSON转换为YAML ,以强制采用YAML样式。

在阅读spec.js之后如何自动执行将JSON转换为YAML 的javascript函数,以确保始终采用漂亮的YAML格式?

index.html

  <script src="./dist/swagger-editor-bundle.js"> </script>
  <script src="./dist/swagger-editor-standalone-preset.js"> </script>
  <script src="./dist/spec.js"> </script>
  <script>
  window.onload = function() {
    const editor = SwaggerEditorBundle({
      spec: spec,
      dom_id: '#swagger-editor',
      layout: 'StandaloneLayout',
      presets: [
        SwaggerEditorStandalonePreset
      ]
    })

    window.editor = editor
  }
  </script>

spec.js

var spec = {
  "consumes": [
    "application/json"
  ],
  "info": {
    "title": "Simple API overview",
    "version": "v2"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "200 300 response",
            "examples": {
              "application/json": "{\n    \"versions\": [\n        {\n            \"status\": \"CURRENT\",\n            \"updated\": \"2011-01-21T11:33:21Z\",\n            \"id\": \"v2.0\",\n            \"links\": [\n                {\n                    \"href\": \"http://127.0.0.1:8774/v2/\",\n                    \"rel\": \"self\"\n                }\n            ]\n        },\n        {\n            \"status\": \"EXPERIMENTAL\",\n            \"updated\": \"2013-07-23T11:33:21Z\",\n            \"id\": \"v3.0\",\n            \"links\": [\n                {\n                    \"href\": \"http://127.0.0.1:8774/v3/\",\n                    \"rel\": \"self\"\n                }\n            ]\n        }\n    ]\n}"
            }
          }
        },
        "summary": "List API versions"
      }
    },
  },
  "swagger": "2.0"
}

0 个答案:

没有答案