【问题标题】:API documentation issue using grape-swagger使用grape-swagger的API文档问题
【发布时间】:2015-05-27 05:45:41
【问题描述】:

我正在尝试使用grape-swagger gem 为我的Grape API 创建自动生成的文档。

使用'grape' gem 我创建了一些 API。

示例:

http://localhost:9292/api/v1/charges

此 API 运行良好。

根据 'grape-sagger' 的文档,我无法正确生成 API 文档。

我的步骤:

1) 我在 gemfile 下面添加了

gem 'grape-swagger'

2) 我还使用 rack-cors 通过在 config.ru 中添加以下内容来启用 CORS

require 'rack/cors'
use Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
  end
end

也在 gemfile 的下方添加。

gem 'rack-cors', :require => 'rack/cors'

3) 我还在 API 类的末尾添加了以下内容

add_swagger_documentation

但是当我运行http://localhost:9292/api/v1/swagger_doc 时,我没有得到正确的路径。 我需要像http://localhost:9292/api/v1/charges 这样的API 路径,但它返回为http://localhost:9292/api/v1/swagger_doc/charges

我需要设置任何其他配置吗?

【问题讨论】:

    标签: ruby-on-rails ruby swagger grape


    【解决方案1】:

    在 api 类的末尾添加以下参数以隐藏文档路径:

    add_swagger_documentation hide_documentation_path: true, api_version: 'v1'
    

    如果您想使用此 gem grape-swagger-rails,请按照文档使用此配置:

    然后创建名为 config/initializers/swagger.rb 的文件并添加此行进行自定义:

    GrapeSwaggerRails.options.url      = "/api/v1/swagger_doc"
    GrapeSwaggerRails.options.app_url  = "#{ENV['APPLICATION_URL']}"
    

    您可以在环境变量中设置 APPLICATION_URL,例如:localhost:3000

    而你的 config/routes.rb 文件将路由设置为

    mount GrapeSwaggerRails::Engine => '/swagger'
    

    然后您可以从这里访问所有文档:localhost:3000/swagger

    通过这种方式,您可以使用葡萄 api 自定义您的 swagger 文档。

    【讨论】:

    • 谢谢萨钦!如果我添加 hide_documentation_path: true,则删除 swagger_doc 的 API 路径。但我很担心,因为我没有得到 localhost:9292/ap1/v1/charges,因为它以 localhost:9292/api/v1/swagger/charges 的形式返回。有什么出路还是我做错了什么?
    • 我按照上述所有说明进行操作,但出现以下错误:解析 JSON/YAML 响应失败
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    相关资源
    最近更新 更多