OpenAPI Specification (OAS) documentation generator for Grape APIs. Supports OpenAPI 2.0 (Swagger), 3.0, and 3.1 specifications.
- Why Grape::OAS?
- Features
- Compatibility
- Installation
- Quick Start
- Documentation
- Basic Usage
- Extensibility
- Related Projects
- Development
- Contributing
- License
Grape::OAS is built around a DTO (Data Transfer Object) architecture that separates collecting API metadata from generating schemas. This clean separation makes the codebase easier to reason about and enables support for multiple output formats (OAS 2.0, 3.0, 3.1) from the same API definition.
- Multi-version support: Generate OAS 2.0, 3.0, or 3.1 from the same API
- Entity integration: Works with grape-entity and dry-struct
- Automatic type inference: Derives OpenAPI types from Grape parameter definitions
- Flexible output: Mount as an endpoint or generate programmatically
| grape-oas | grape | grape-entity | dry-struct | Ruby |
|---|---|---|---|---|
| 0.1.x | >= 3.0 | >= 0.7 | >= 1.0 | >= 3.2 |
gem 'grape-oas'For entity support:
gem 'grape-entity' # For grape-entity support
gem 'dry-struct' # For dry-struct contract supportclass API < Grape::API
format :json
add_oas_documentation(
info: {
title: 'My API',
version: '1.0.0'
}
)
resource :users do
desc 'List users', entity: Entity::User
get { User.all }
end
endDocumentation available at:
/swagger_doc- OpenAPI 3.0 (default)/swagger_doc?oas=2- OpenAPI 2.0/swagger_doc?oas=3.1- OpenAPI 3.1
# Generate OpenAPI 3.0 spec
spec = GrapeOAS.generate(app: API, schema_type: :oas3)
puts JSON.pretty_generate(spec)# In Rakefile
require 'grape_oas/tasks'rake grape_oas:generate[MyAPI,oas31,spec/openapi.json]| Document | Description |
|---|---|
| Configuration | All configuration options |
| Usage Guide | Detailed usage examples |
| Architecture | System architecture overview |
| Introspectors | Custom introspector development |
| Exporters | Custom exporter development |
| API Model | Internal API model reference |
desc 'Get a user by ID',
detail: 'Returns detailed user information',
tags: ['users']
params do
requires :id, type: Integer, desc: 'User ID'
end
get ':id' do
User.find(params[:id])
enddesc 'Get user' do
success Entity::User
failure [[404, 'Not Found'], [500, 'Server Error']]
endclass Entity::User < Grape::Entity
expose :id, documentation: { type: Integer }
expose :name, documentation: { type: String }
expose :posts, using: Entity::Post, documentation: { is_array: true }
endclass MyModelIntrospector
extend GrapeOAS::Introspectors::Base
def self.handles?(subject)
subject.is_a?(Class) && subject < MyBaseModel
end
def self.build_schema(subject, stack: [], registry: {})
GrapeOAS::ApiModel::Schema.new(type: "object", canonical_name: subject.name)
end
end
GrapeOAS.introspectors.register(MyModelIntrospector)GrapeOAS.exporters.register(MyCustomExporter, as: :custom)
schema = GrapeOAS.generate(app: API, schema_type: :custom)| Project | Description |
|---|---|
| grape | REST-like API framework for Ruby |
| grape-entity | Entity exposure for Grape APIs |
| grape-swagger | OpenAPI documentation for Grape APIs |
| grape-swagger-entity | grape-swagger adapter for grape-entity |
| oas_grape | Another OpenAPI 3.1 generator for Grape |
git clone https://github.com/numbata/grape-oas.git
cd grape-oas
bin/setup
bundle exec rake testBug reports and pull requests are welcome on GitHub at https://github.com/numbata/grape-oas. See CONTRIBUTING.md for guidelines.
MIT License. Copyright (c) Andrei Subbota.