Swagger UI - 试试吧! Returns 整个 Swagger 文档

Swagger UI - Try it Out! Returns whole Swagger Doc

当我 运行 以下命令时:

rake swagger:docs

它 returns 我为每个控制器准备了 JSON 文件。

当我访问 swagger-ui 时,它会给我正确的页面:

但是当我点击 Try it Out! 时,它 returns 我整个 JSON 如下所示:

我是 Swagger 的新手。谁能帮帮我,我哪里错了?

更新: 我的 public 目录结构:

更新 trucks.json :

{
  "apiVersion": "v1",
  "swaggerVersion": "1.2",
  "basePath": "http://localhost:3000",
  "resourcePath": "trucks",
  "apis": [
    {
      "path": "/api/v1/trucks.json",
      "operations": [
        {
          "summary": "Fetches all Trucks",
          "parameters": [
            {
              "paramType": "header",
              "name": "X-Auth-Token",
              "type": "string",
              "description": "Authentication Token",
              "required": true
            }
          ],
          "responseMessages": [
            {
              "code": 401,
              "responseModel": null,
              "message": "Unauthorized"
            },
            {
              "code": 406,
              "responseModel": null,
              "message": "The request you made is not acceptable"
            }
          ],
          "nickname": "API::V1::Trucks#index",
          "method": "get"
        }
      ]
    }
  ],
  "authorizations": null
}

找出解决方案。发布以便如果有人偶然发现可以尝试一下。

我的 swagger 文档配置如下所示:

Swagger::Docs::Config.register_apis({
   'v1' =>{
      :api_extension_type => :json,
      :api_file_path => "public",
      :base_path => 'http://localhost:3000',
      :parent_controller => ApplicationController,
      :base_api_controller => ActionController::Base,
      :camelize_model_properties => false,
      :clean_directory => true,
      :attributes => {
            :info => {
              "title" => "Swagger Yattya",
              "description" => "Yattya api documentation",
              "termsOfServiceUrl" => "http://helloreverb.com/terms/",
              "contact" => "apiteam@wordnik.com",
              "license" => "Apache 2.0",
              "licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
        }
        }
    }
  })

不需要第一行:

:api_extension_type => :json

从配置中删除了这一行,效果非常好!