Docs
Generating OpenAPI from Code
Rails Sinatra Rack

OpenAPI and Ruby on Rails, Sinatra, or Rack-based frameworks with Optic

To begin using Optic with your API you first need an OpenAPI file describing your API. This YAML or JSON file can be written by hand or generated from your code. This document describes the recommended process for generating a new OpenAPI file for a Ruby on Rails, Sinatra, or Rack-based project.

We recommend using the exoego/rspec-openapi (opens in a new tab) gem. It generates a new (or updates an existing) OpenAPI file by analyzing request traffic from RSpec and Minitest tests. All that is necessary are tests that make HTTP requests.

For example, if you have an RSpec test,

describe 'GET /' do
  it 'returns a successful response' do
    get '/'
    expect(last_response.status).to eq(200)
  end
end

Then running OPENAPI=1 bundle exec rspec would produce or update an OpenAPI file at doc/openapi.yaml that contains that endpoint.

We've put together a simple Sinatra example here (opens in a new tab).

What's next

Automate your OpenAPI generation and test your API specifications by setting up Optic in CI.