grape-swagger

grape-swagger

Grape API的自动Swagger文档生成工具

grape-swagger是一个专为Grape API设计的Ruby gem,能够自动生成符合Swagger规范的API文档。它可以轻松整合到现有Grape项目中,输出可在Swagger UI中浏览的文档。该工具具有丰富的配置选项,包括自定义主机、基础路径和安全定义等。grape-swagger还支持与grape-entity和representable等流行的模型解析器集成,为开发者提供了一个高效的API文档管理解决方案,有效提升了API的可发现性和可用性。

grape-swaggerAPI文档SwaggerRubyGrapeGithub开源项目

Gem Version Build Status Coverage Status Code Climate

Table of Contents

What is grape-swagger? <a name="what"></a>

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.

Demo Screenshot

This screenshot is based on the Hussars sample app.

Related Projects <a name="related"></a>

Compatibility <a name="version"></a>

The following versions of grape, grape-entity and grape-swagger can currently be used together.

grape-swaggerswagger specgrapegrape-entityrepresentable
0.10.51.2>= 0.10.0 ... <= 0.14.0< 0.5.0n/a
0.11.01.2>= 0.16.2< 0.5.0n/a
0.25.22.0>= 0.14.0 ... <= 0.18.0<= 0.6.0>= 2.4.1
0.26.02.0>= 0.16.2 ... <= 1.1.0<= 0.6.1>= 2.4.1
0.27.02.0>= 0.16.2 ... <= 1.1.0>= 0.5.0>= 2.4.1
0.32.02.0>= 0.16.2>= 0.5.0>= 2.4.1
0.34.02.0>= 0.16.2 ... < 1.3.0>= 0.5.0>= 2.4.1
>= 1.0.02.0>= 1.3.0>= 0.5.0>= 2.4.1
>= 2.0.02.0>= 1.7.0>= 0.5.0>= 2.4.1

Swagger-Spec <a name="swagger-spec"></a>

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

<!-- validating it with: http://bigstickcarpet.com/swagger-parser/www/index.html -->

Installation <a name="install"></a>

Add to your Gemfile:

gem 'grape-swagger'

Upgrade

Please see UPGRADING when upgrading from a previous version.

Usage <a name="usage"></a>

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 < Grape::API format :json mount API::Cats mount API::Dogs mount API::Pirates add_swagger_documentation end end

To explore your API, either download Swagger UI and set it up yourself or go to the online swagger demo and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc).

Model Parsers <a name="model_parsers"></a>

Since 0.21.0, Grape::Entity is not a part of grape-swagger, you need to add grape-swagger-entity manually to your Gemfile. Also added support for representable via grape-swagger-representable.

# For Grape::Entity ( https://github.com/ruby-grape/grape-entity ) gem 'grape-swagger-entity', '~> 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 <a name="configure"></a>

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: <a name="host"></a>

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

add_swagger_documentation \ host: 'www.example.com'

base_path: <a name="base_path"></a>

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: <a name="mount_path"></a>

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

add_swagger_documentation \ mount_path: '/swagger_doc'

add_base_path: <a name="add_base_path"></a>

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: <a name="add_root"></a>

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

add_swagger_documentation \ add_root: true

add_version: <a name="add_version"></a>

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: <a name="doc_version"></a>

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: <a name="endpoint_auth_wrapper"></a>

Specify the middleware to use for securing endpoints.

add_swagger_documentation \ endpoint_auth_wrapper: WineBouncer::OAuth2

swagger_endpoint_guard: <a name="swagger_endpoint_guard"></a>

Specify the method and auth scopes, used by the middleware for securing endpoints.

add_swagger_documentation \ swagger_endpoint_guard: 'oauth2 false'

token_owner: <a name="token_owner"></a>

Specify the token_owner method, provided by the middleware, which is typically named 'resource_owner'.

add_swagger_documentation \ token_owner: 'resource_owner'

security_definitions: <a name="security_definitions"></a>

Specify the Security Definitions Object

NOTE: Swagger-UI is supporting only implicit flow yet

add_swagger_documentation \ security_definitions: { api_key: { type: "apiKey", name: "api_key", in: "header" } }

security: <a name="security"></a>

Specify the Security Object

add_swagger_documentation \ security: [ { api_key: [] } ]

models: <a name="models"></a>

A list of entities to document. Combine with the grape-entity gem.

These would be added to the definitions section of the swagger file.

add_swagger_documentation \ models: [ TheApi::Entities::UseResponse, TheApi::Entities::ApiError ]

tags: <a name="tags"></a>

A list of tags to document. By default tags are automatically generated for endpoints based on route names.

add_swagger_documentation \ tags: [ { name: 'widgets', description: 'A description of widgets' } ]

hide_documentation_path: (default: true) <a name="hide_documentation_path"></a>

add_swagger_documentation \ hide_documentation_path: true

Don't show the /swagger_doc path in the generated swagger documentation.

info: <a name="info"></a>

add_swagger_documentation \ info: { title: "The API title to be displayed on the API homepage.", description: "A description of the API.", contact_name: "Contact name", contact_email: "Contact@email.com", contact_url: "Contact URL", license: "The name of the license.", license_url: "www.The-URL-of-the-license.org", terms_of_service_url: "www.The-URL-of-the-terms-and-service.com", }

A hash merged into the info key of the JSON documentation.

array_use_braces: <a name="array_use_braces"></a>

add_swagger_documentation \ array_use_braces: true

This setting must be true in order for params defined as an Array type to submit each element properly.

params do optional :metadata, type: Array[String] end

with array_use_braces: true:

metadata[]: { "name": "Asset ID", "value": "12345" }
metadata[]: { "name": "Asset Tag", "value": "654321"}

with array_use_braces: false:

metadata: {"name": "Asset ID", "value": "123456"}
metadata: {"name": "Asset Tag", "value": "654321"}

api_documentation

Customize the Swagger API documentation route, typically contains a desc field. The default description is "Swagger compatible API description".

add_swagger_documentation \ api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }

specific_api_documentation

Customize the Swagger API specific documentation route, typically contains a desc field. The default description is "Swagger compatible API description for specific API".

add_swagger_documentation \ specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }

consumes

Customize the Swagger API default global consumes field value.

add_swagger_documentation \ consumes: ['application/json', 'application/x-www-form-urlencoded']

produces

Customize the Swagger API default global produces field value.

add_swagger_documentation \ produces: ['text/plain']

Routes Configuration <a name="routes"></a>

编辑推荐精选

蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多