百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

grape-swagger

> 编程语言
开源

为您的 grape API 添加符合 OAPI/swagger v2.0 规范的文档

1.1K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

为您的 grape API 添加符合 OAPI/swagger v2.0 规范的文档

Table of Contents

  • What is grape-swagger?
  • Related Projects
  • Compatibility
  • Swagger-Spec
  • Installation
  • Upgrade
  • Usage
  • Model Parsers
    • Custom Model Parsers
      • insert_before
      • insert_after
    • CORS
  • Configure
    • host:
    • base_path:
    • mount_path:
    • add_base_path:
    • add_root:
    • add_version:
    • doc_version:
    • endpoint_auth_wrapper:
    • swagger_endpoint_guard:
    • token_owner:
    • security_definitions:
    • security:
    • models:
    • tags:
    • hide_documentation_path: (default: true)
    • info:
    • array_use_braces:
    • api_documentation
    • specific_api_documentation
    • consumes
    • produces
  • Routes Configuration
    • Swagger Header Parameters
    • Hiding an Endpoint
    • Overriding Auto-Generated Nicknames
    • Specify endpoint details
    • Overriding the route summary
    • Overriding the tags
    • Deprecating routes
    • Overriding the name of the body parameter
    • Defining an endpoint as an array
    • Using an options hash
    • Overriding parameter type
    • Overriding data type of the parameter
    • Multiple types
    • Array of data type
    • Collection format of arrays
    • Hiding parameters
    • Setting a Swagger default value
    • Setting additionalProperties for object-type parameters
      • Allow any additional properties
      • Allow any additional properties of a particular type
      • Allow any additional properties matching a defined schema
      • Example parameter value
      • Expose nested namespace as standalone route
        • With a custom name
      • Response documentation
      • Changing default status codes
      • Multiple status codes for response
      • File response
      • Default response
      • Extensions
      • Response examples documentation
      • Response headers documentation
      • Adding root element to responses
      • Multiple present Response
  • Using Grape Entities
    • Documented class/definition
    • Custom Model Description and Example
    • Relationships
      • 1xN
      • 1x1
      • Inheritance with allOf and discriminator
  • Securing the Swagger UI
  • Example
    • Grouping the API list using Namespace
    • Example Code
  • Rake Tasks
    • OpenApi/Swagger Documentation
    • OpenApi/Swagger Validation
  • Contributing to grape-swagger
  • Copyright and License

What is grape-swagger?

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.

Related Projects

  • Grape
  • Grape Swagger Entity
    • Grape Entity
  • Grape Swagger Representable
  • Swagger UI

Compatibility

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

Swagger-Spec

Grape-swagger generates documentation per Swagger / OpenAPI Spec 2.0.

Installation

Add to your Gemfile:

gem 'grape-swagger'

Upgrade

Please see UPGRADING when upgrading from a previous version.

Usage

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'.

Custom Model Parsers

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.

insert_before

GrapeSwagger.model_parsers.insert_before(GrapeSwagger::Representable::Parser, GrapeSwagger::Roar::Parser, Roar::Decorator)

insert_after

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.

CORS

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

Configure

  • host
  • base_path
  • mount_path
  • add_base_path
  • add_root
  • add_version
  • doc_version
  • endpoint_auth_wrapper
  • swagger_endpoint_guard
  • token_owner
  • security_definitions
  • security
  • models
  • tags
  • hide_documentation_path
  • info
  • array_use_braces
  • api_documentation
  • specific_api_documentation
  • consumes
  • produces

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' }

host:

Sets explicit the host, default would be taken from request.

add_swagger_documentation \
   host: 'www.example.com'

base_path:

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

mount_path:

The path where the API documentation is loaded, default is: /swagger_doc.

add_swagger_documentation \
   mount_path: '/swagger_doc'

add_base_path:

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:

Add root element to all the responses, default is: false.

add_swagger_documentation \
   add_root: true

add_version:

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

doc_version:

Specify the version of the documentation at info section, default is: '0.0.1'

add_swagger_documentation \
   doc_version: '0.0.1'

endpoint_auth_wrapper:

Specify the middleware to use for securing endpoints.

add_swagger_documentation \
   endpoint_auth_wrapper: WineBouncer::OAuth2

swagger_

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyapidocumentationgrapeoapi

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言