簡體   English   中英

使用grape-swagger的API文檔問題

[英]API documentation issue using grape-swagger

我正在嘗試使用grape-swagger gem 為我的Grape API 創建自動生成的文檔。

使用'grape'寶石,我創建了一些 API。

例子:

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

這個 API 工作正常。

按照“ grape-swagger”的文檔,我無法正確生成 API 文檔。

我的步驟:

  1. 我在gemfile下面添加了

    寶石'葡萄招搖'

  2. 我還使用 rack-cors 通過在 config.ru 中添加以下內容來啟用 CORS

    要求 'rack/cors' 使用 Rack::Cors 確實允許做起源 ' ' 資源 ' ',標題::any,方法:[:get,:post,:put,:delete,:options] end end

還在 gemfile 中添加了下面的內容。

gem 'rack-cors', :require => 'rack/cors'
  1. 我還在我的 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

我需要設置任何其他配置嗎?

在 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 文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM