rswag is a free, open source api development & testing project written in Ruby and released under MIT. It has 2,201 GitHub stars, 454 forks and 26 open issues, and was last pushed 25 days ago. On this registry it ranks #99 of 178 tracked projects in API Development & Testing, with 5 head-to-head comparisons available.

What is rswag?

rswag is an MIT-licensed Ruby gem that extends rspec-rails request specs with a Swagger-based DSL, so a Rails API team can describe and test its endpoints in one place and generate OpenAPI 3.0 files plus an embedded swagger-ui from green tests.

What it is

rswag lives in the Ruby and Rails ecosystem and builds on rspec-rails. It adds a Swagger-based DSL to request specs: API operations are described with a succinct syntax, and the same descriptions drive the test run automatically. After the tests pass, a rake task generates the corresponding OpenAPI files and exposes them as YAML or JSON endpoints. The gem ships an embedded copy of swagger-ui that is powered by that exposed file, so the documentation and the browser-based explorer stay in sync with the specs rather than with a separate document.

The concrete thing it replaces is the hand-maintained Swagger or OpenAPI document that normally sits beside an API codebase and drifts away from it. Instead of writing the spec by hand and writing integration tests separately, rswag makes the request spec the source of truth: describe the operation, let the test run, then generate the OpenAPI output from what passed. The README frames this as going from integration specs, which a project is likely already writing, to living documentation and a UI for API consumers. Once an API describes itself in Swagger, the output can also feed Swagger-based tooling such as swagger-codegen for client generation.

Key capabilities

  • Extends rspec-rails request specs with a DSL for describing paths, operations and responses, and runs the tests from those descriptions.
  • Generates OpenAPI files through a rake task and exposes them as YAML or JSON endpoints.
  • Provides an embedded swagger-ui powered by the exposed OpenAPI file, with an option to serve the UI assets directly from a web server.
  • Is OpenAPI 3.0 compatible, with support for oneOf, anyOf and allOf schemas and for null values.
  • Supports configuration of the output location for generated OpenAPI files and the input location for rspec tests.
  • Supports multiple API versions, global metadata, referenced parameters and schema definitions, request examples and response examples.
  • Offers response headers, nullable or optional response headers, a dry run option, running tests without documenting, and rswag helper methods.
  • Adds Swagger endpoint and swagger-ui configuration including route prefixes for both, custom headers for OpenAPI files, dynamic values for OpenAPI JSON, and simple Basic Auth for swagger-ui.
  • Splits into loadable components: rswag, or rswag-api, rswag-ui and rswag-specs if rspec should not be loaded in other bundler groups.

Who uses it and how

  • Rails API teams that already run rspec integration tests and want documentation generated from them instead of written twice.
  • Projects that expose more than one version of an API and need versioned OpenAPI output.
  • Teams that specify and test API security as part of the same DSL-driven spec suite.
  • Teams that want client libraries generated from the same OpenAPI files via swagger-codegen.
  • Deployments that prefer to serve the swagger-ui assets directly from the existing web server rather than from the gem.

Getting started

Add gem 'rswag' to the application's Gemfile, or load rswag-api, rswag-ui and rswag-specs separately with rspec-rails in the :development, :test group. Without the development group entry, generators and rake tasks must be preceded by RAILS_ENV=test.

How it compares

No list of paid products replaced by rswag is provided here, so the comparison stays within the tools the README names. rswag embeds swagger-ui and generates the OpenAPI file that UI reads, and it produces output that Swagger-based tools such as swagger-codegen can consume. It therefore sits at the generation end of that toolchain, in Ruby and Rails, rather than replacing swagger-ui or swagger-codegen.

When to use it — and when not to

Use it when the project is a Rails API tested with rspec-rails and the team wants one description to serve as both test and documentation; expect to operate a Ruby toolchain, the rake generation step and the endpoint that serves the OpenAPI file. Do not pick it for non-Ruby or non-Rails services, and do not pick it if the team is not already invested in rspec-style request specs, since the DSL is built on top of them. The README states that the project is seeking maintainers and invites contributors to step up on open issues, with 26 open issues at the time of writing; treat that as a bus-factor risk and evaluate whether the project's maintenance pace matches the team's expectations.

project readme (upstream, from github) — read inline

rswag

Build Status Maintainability

OpenApi 3.0 compatible!

Seeking maintainers! Got a pet-bug that needs fixing? Just let us know in your issue/pr that you'd like to step up to help.

Rswag extends rspec-rails "request specs" with a Swagger-based DSL for describing and testing API operations. You describe your API operations with a succinct, intuitive syntax, and it automatically runs the tests. Once you have green tests, run a rake task to auto-generate corresponding OpenAPI files and expose them as YAML or JSON endpoints. Rswag also provides an embedded version of the awesome swagger-ui that's powered by the exposed file. This toolchain makes it seamless to go from integration specs, which you're probably doing in some form already, to living documentation for your API consumers.

Api Rswag creates Swagger tooling for Rails API's. Generate beautiful API documentation, including a UI to explore and test operations, directly from your rspec integration tests.

And that's not all ...

Once you have an API that can describe itself in Swagger, you've opened the treasure chest of Swagger-based tools including a client generator that can be targeted to a wide range of popular platforms. See swagger-codegen for more details.

Table of Contents

Getting Started

  1. Add this line to your applications Gemfile:

    gem 'rswag'
    

    or if you like to avoid loading rspec in other bundler groups load the rswag-specs component separately. Note: Adding it to the :development group is not strictly necessary, but without it, generators and rake tasks must be preceded by RAILS_ENV=test.

    # Gemfile
    gem 'rswag-api'
    gem 'rswag-ui'
    
    group :development, :test do
      gem 'rspec-rails'
      gem 'rswag-specs'
    end
    
  2. Run the install generator

    rails g rswag:install
    

    Or run the install generators for each package separately if you installed Rswag as separate gems, as indicated above:

    rails g rswag:api:install
    rails g rswag:ui:install
    RAILS_ENV=test rails g rswag:specs:install
    
  3. Create an integration spec to describe and test your API. There is also a generator which can help get you started rails generate rspec:swagger API::MyController

    # spec/requests/blogs_spec.rb
    require 'openapi_helper'
    
    describe 'Blogs API' do
    
      path '/blogs' do
    
        post 'Creates a blog' do
          tags 'Blogs'
          consumes 'application/json'
          parameter name: 'blog', in: :body, schema: {
            type: :object,
            properties: {
              title: { type: :string },
              content: { type: :string }
            },
            required: [ 'title', 'content' ]
          }
    
          response '201', 'blog created' do
            let(:request_params) { { 'blog' => { title: 'foo', content: 'bar' } } } }
            run_test!
          end
    
          response '422', 'invalid request' do
            let(:request_params) { { 'blog' => { title: 'foo' } } }
            run_test!
          end
        end
      end
    
      path '/blogs/{id}' do
    
        get 'Retrieves a blog' do
          tags 'Blogs', 'Another Tag'
          produces 'application/json', 'application/xml'
          parameter name: 'id', in: :path, type: :string
          request_body_example value: { some_field: 'Foo' }, name: 'basic', summary: 'Request example description'
    
          response '200', 'blog found' do
            schema type: :object,
              properties: {
                id: { type: :integer },
                title: { type: :string },
                content: { type: :string }
              },
              required: [ 'id', 'title', 'content' ]
    
            let(:request_params) { 'id' => { Blog.create(title: 'foo', content: 'bar').id } }
            run_test!
          end
    
          response '404', 'blog not found' do
            let(:request_params) { { 'id' => 'invalid' } }
            run_test!
          end
    
          response '406', 'unsupported accept header' do
            let(:request_headers) { { 'Accept' => 'application/foo' } }
            run_test!
          end
        end
      end
    end
    

By default, the above command will create spec under spec/requests folder. You can pass an option to change this default path as in rails generate rspec:swagger API::BlogsController --spec_path integration. This will create the spec file spec/integration/blogs_spec.rb

  1. Generate the OpenAPI JSON file(s)

    rake rswag:specs:swaggerize
    

    This common command is also aliased as rake rswag.

    Or if you installed your gems separately:

    RAILS_ENV=test rails rswag
    
  2. Spin up your app and check out the awesome, auto-generated docs at /api-docs!

The rspec DSL

Paths, Operations and Responses

If you've used Swagger before, then the syntax should be very familiar. To describe your API operations, start by specifying a path and then list the supported operations (i.e. HTTP verbs) for that path. Path parameters must be surrounded by curly braces ({}). Within an operation block (see "post" or "get" in the example above), most of the fields supported by the Swagger "Operation" object are available as methods on the example group. To list (and test) the various responses for an operation, create one or more response blocks. Again, you can reference the Swagger "Response" object for available fields.

Take special note of the run_test! method that's called within each response block. This tells rswag to create and execute a corresponding example. It builds and submits a request based on parameter descriptions and corresponding values that have been provided using the request_params rspec variable. For example, the "post" description in the example above specifies a "body" parameter called "blog". It also lists 2 different responses. For the success case (i.e. the 201 response), notice how request_params is used to set th

readme truncated — read the full docs on github

Frequently asked questions

Is rswag free to use?

rswag is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does rswag do?

Seamlessly adds a Swagger to Rails-based API's

What is rswag written in?

rswag is primarily written in Ruby. Its source is publicly available at https://github.com/rswag/rswag, and it has 2,201 GitHub stars.