The project is in a healthy, maintained state
A gem to convert Rails routes to OpenAPI v3.1 YAML
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

Converting Rails Routes Output into OpenAPI v3.1

In an effort to learn a little bit about Ruby and Rails, I decided a fun project would be to attempt generating a YAML file that aligns with the OpenAPI v3.1.0 specification. This file can then be imported into Postman so you can test Rails routes without manually using the Rails console or the consuming frontend application.

Quick Start

  1. Add the gem to your Gemfile:
gem 'rails_routes_to_openapi'
  1. Install dependencies:
bundle install
  1. Run the conversion task:
bundle exec rake routes_to_openapi:convert

This task will internally run rails routes on your Rails application and generate an OpenAPI YAML file named similar to openapi_v3.1_YYYYMMDDHHMMSS.yml in the project root.

Warning

Tip: Ensure you run the task within a Rails project context so that it can properly pull the routes.

Workflow Overview

  1. Routes Processing
    The conversion workflow starts by reading the output of rails routes, splitting it into lines, and filtering out header information. A helper module parses each line into a structured RouteInfo object that captures the HTTP verb, URI pattern, and the associated controller action.

  2. OpenAPI Generation
    The parsed routes are then converted into an OpenAPI compliant hash. The conversion process does the following:

    • It adapts URL parameters (e.g., turning /a/b/:param/d into /a/b/{param}).
    • For non-GET verbs, it attaches a default request body schema.
    • Finally, it merges the above details into an OpenAPI compliant hash which is then dumped into YAML format.
  3. Output
    The generated YAML file contains:

    • OpenAPI version information.
    • Descriptive metadata (title, description, version, etc.).
    • A list of servers.
    • A paths list that maps each route to its HTTP verb and summary (derived from the controller action).

Example OpenAPI YAML Output

Below is an abbreviated snippet of what the output might look like:

openapi: 3.1.0
info:
  title: Rails Routes to OpenAPI
  description: Generate an OpenAPI v3.1 YAML file
  version: "1.1.0"
servers:
  - url: "https://api.example.com"
    description: "Base URL for the API"
paths:
  "/":
    get:
      summary: "home#show"
  "/path/to/authorize/native":
    get:
      summary: "doorkeeper/authorizations#show"
  "/path/to/authorize":
    get:
      summary: "doorkeeper/authorizations#new"
    delete:
      summary: "doorkeeper/authorizations#destroy"
      requestBody:
        required: true
        content:
          "application/json":
            schema:
              type: object
              properties:
                id:
                  type: string
                  format: string