为您的 grape API 添加符合 OAPI/swagger v2.0 规范的文档
The grape-swagger gem provides an autogenerated documentation for your Grape API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in Swagger UI. You should be able to point the petstore demo to your API.
This screenshot is based on the Hussars sample app.
The following versions of grape, grape-entity and grape-swagger can currently be used together.
| grape-swagger | swagger spec | grape | grape-entity | representable |
|---|---|---|---|---|
| 0.10.5 | 1.2 | >= 0.10.0 ... = 0.16.2 | = 0.14.0 ... = 2.4.1 | |
| 0.26.0 | 2.0 | >= 0.16.2 ... = 2.4.1 | ||
| 0.27.0 | 2.0 | >= 0.16.2 ... = 0.5.0 | >= 2.4.1 | |
| 0.32.0 | 2.0 | >= 0.16.2 | >= 0.5.0 | >= 2.4.1 |
| 0.34.0 | 2.0 | >= 0.16.2 ... = 0.5.0 | >= 2.4.1 | |
| >= 1.0.0 | 2.0 | >= 1.3.0 | >= 0.5.0 | >= 2.4.1 |
| >= 2.0.0 ... = 1.8.0 ... = 0.5.0 | >= 2.4.1 | |||
| >= 2.1.3 ... = 1.8.0 ... = 0.5.0 | >= 2.4.1 | |||
| >= 2.2.0 | 2.0 | >= 2.1 ... = 0.5.0 | >= 2.4.1 |
Grape-swagger generates documentation per Swagger / OpenAPI Spec 2.0.
Add to your Gemfile:
gem 'grape-swagger'
Please see UPGRADING when upgrading from a previous version.
Mount all your different APIs (with Grape::API superclass) on a root node. In the root class definition, include add_swagger_documentation, this sets up the system and registers the documentation on '/swagger_doc'. See example/config.ru for a simple demo.
require 'grape-swagger'
module API
class Root 0.3'
# For representable ( https://github.com/apotonick/representable )
gem 'grape-swagger-representable', '~> 0.2'
If you are not using Rails, make sure to load the parser inside your application initialization logic, e.g., via require 'grape-swagger/entity' or require 'grape-swagger/representable'.
You can create your own model parser, for example for roar.
module GrapeSwagger
module Roar
class Parser
attr_reader :model
attr_reader :endpoint
def initialize(model, endpoint)
@model = model
@endpoint = endpoint
end
def call
# Parse your model and return hash with model schema for swagger
end
end
end
end
Then you should register your custom parser.
GrapeSwagger.model_parsers.register(GrapeSwagger::Roar::Parser, Roar::Decorator)
To control model parsers sequence, you can insert your parser before or after another parser.
GrapeSwagger.model_parsers.insert_before(GrapeSwagger::Representable::Parser, GrapeSwagger::Roar::Parser, Roar::Decorator)
GrapeSwagger.model_parsers.insert_after(GrapeSwagger::Roar::Parser, GrapeSwagger::Representable::Parser, Representable::Decorator)
As we know, Roar::Decorator uses Representable::Decorator as a superclass, this allows to avoid a problem when Roar objects are processed by GrapeSwagger::Representable::Parser instead of GrapeSwagger::Roar::Parser.
If you use the online demo, make sure your API supports foreign requests by enabling CORS in Grape, otherwise you'll see the API description, but requests on the API won't return. Use rack-cors to enable CORS.
require 'rack/cors'
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
end
end
Alternatively you can set CORS headers in a Grape before block.
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
end
You can pass a hash with optional configuration settings to add_swagger_documentation.
The examples show the default value.
The host and base_path options also accept a proc or a lambda to evaluate, which is passed a request object:
add_swagger_documentation \
base_path: proc { |request| request.host =~ /^example/ ? '/api-example' : '/api' }
Sets explicit the host, default would be taken from request.
add_swagger_documentation \
host: 'www.example.com'
Base path of the API that's being exposed, default would be taken from request.
add_swagger_documentation \
base_path: nil
host and base_path are also accepting a proc or lambda
The path where the API documentation is loaded, default is: /swagger_doc.
add_swagger_documentation \
mount_path: '/swagger_doc'
Add basePath key to the documented path keys, default is: false.
add_swagger_documentation \
add_base_path: true # only if base_path given
Add root element to all the responses, default is: false.
add_swagger_documentation \
add_root: true
Add version key to the documented path keys, default is: true,
here the version is the API version, specified by grape in path
add_swagger_documentation \
add_version: true
Specify the version of the documentation at info section, default is: '0.0.1'
add_swagger_documentation \
doc_version: '0.0.1'
Specify the middleware to use for securing endpoints.
add_swagger_documentation \
endpoint_auth_wrapper: WineBouncer::OAuth2
暂无开放 Issues,或尚未同步最近议题。