All Projects → k0kubun → Rspec Openapi

k0kubun / Rspec Openapi

Licence: mit
Generate OpenAPI schema from RSpec request specs

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Rspec Openapi

Limestone
Boilerplate Rails 6 SaaS application with Webpack, Stimulus and Docker integration.
Stars: ✭ 191 (+48.06%)
Mutual labels:  rails, rspec
Shoulda Matchers
Simple one-liner tests for common Rails functionality
Stars: ✭ 3,166 (+2354.26%)
Mutual labels:  rails, rspec
Action Cable Testing
Action Cable testing utils
Stars: ✭ 192 (+48.84%)
Mutual labels:  rails, rspec
Mumuki Laboratory
🔬 Where students practice and receive automated and human feedback
Stars: ✭ 131 (+1.55%)
Mutual labels:  rails, rspec
Action mailer matchers
ActionMailerMatchers provides rspec matchers to test Rails' common ActionMailer functionality.
Stars: ✭ 50 (-61.24%)
Mutual labels:  rails, rspec
Expertiza
Expertiza is a web application through which students can submit and peer-review learning objects (articles, code, web sites, etc). The Expertiza project is supported by the National Science Foundation.
Stars: ✭ 160 (+24.03%)
Mutual labels:  rails, rspec
Crystalball
Regression Test Selection library for your RSpec test suite
Stars: ✭ 259 (+100.78%)
Mutual labels:  rails, rspec
Rails new
A thoughtfully designed template for building modern Rails apps. Get started in minutes instead of hours 🔥🚀
Stars: ✭ 151 (+17.05%)
Mutual labels:  rails, rspec
Heavens door
Capybara test scenario recorder for Rails
Stars: ✭ 857 (+564.34%)
Mutual labels:  rails, rspec
Rails Api And Angularjs
Integration between rails and angularjs which includes rspec tests.
Stars: ✭ 22 (-82.95%)
Mutual labels:  rails, rspec
Apitome
Apitome: /iˈpitəmē/ An API documentation presentation layer for RSpec API Documentation output.
Stars: ✭ 244 (+89.15%)
Mutual labels:  rails, rspec
Test Prof
Ruby Tests Profiling Toolbox
Stars: ✭ 1,193 (+824.81%)
Mutual labels:  rails, rspec
Capybara error intel
🐛 Ruby gem for heuristic error messages in Capybara based Page Objects
Stars: ✭ 16 (-87.6%)
Mutual labels:  rails, rspec
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+789.15%)
Mutual labels:  rails, rspec
Lurker
📖 The ultimate tool for documenting and testing APIs in Rails
Stars: ✭ 120 (-6.98%)
Mutual labels:  rails, rspec
Zero Rails openapi
Concise DSL for generating OpenAPI Specification 3 (OAS3) JSON documentation for Ruby application.
Stars: ✭ 125 (-3.1%)
Mutual labels:  rails
Open Api Renderer
🎩 A React renderer for OpenAPI v3.
Stars: ✭ 127 (-1.55%)
Mutual labels:  openapi
Http Router
🎉 Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
Stars: ✭ 124 (-3.88%)
Mutual labels:  openapi
Boring generators
Boring generators aims to make your development faster by delegating boring setups to us.
Stars: ✭ 125 (-3.1%)
Mutual labels:  rails
Base App
An app to help jumpstart a new Rails 4 app. Features Ruby 2.0, PostgreSQL, jQuery, RSpec, Cucumber, user and admin system built with Devise, Facebook login.
Stars: ✭ 127 (-1.55%)
Mutual labels:  rspec

rspec-openapi test

Generate OpenAPI schema from RSpec request specs.

What's this?

There are some gems which generate OpenAPI specs from RSpec request specs. However, they require a special DSL specific to these gems, and we can't reuse existing request specs as they are.

Unlike such existing gems, rspec-openapi can generate OpenAPI specs from request specs without requiring any special DSL. Furthermore, rspec-openapi keeps manual modifications when it merges automated changes to OpenAPI specs in case we can't generate everything from request specs.

Installation

Add this line to your application's Gemfile:

gem 'rspec-openapi', group: :test

Usage

Run rspec with OPENAPI=1 to generate doc/openapi.yaml for your request specs.

$ OPENAPI=1 rspec

Example

Let's say you have a request spec like this:

RSpec.describe 'Tables', type: :request do
  describe '#index' do
    it 'returns a list of tables' do
      get '/tables', params: { page: '1', per: '10' }, headers: { authorization: 'k0kubun' }
      expect(response.status).to eq(200)
    end

    it 'does not return tables if unauthorized' do
      get '/tables'
      expect(response.status).to eq(401)
    end
  end

  # ...
end

If you run the spec with OPENAPI=1,

OPENAPI=1 rspec spec/requests/tables_spec.rb

It will generate doc/openapi.yaml file like:

openapi: 3.0.3
info:
  title: rspec-openapi
paths:
  "/tables":
    get:
      summary: index
      tags:
      - Table
      parameters:
      - name: page
        in: query
        schema:
          type: integer
        example: 1
      - name: per
        in: query
        schema:
          type: integer
        example: 10
      responses:
        '200':
          description: returns a list of tables
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: integer
                    name:
                      type: string
                    # ...

and the schema file can be used as an input of Swagger UI or Redoc.

Redoc example

Configuration

The following configurations are optional.

# Change the path to generate schema from `doc/openapi.yaml`
RSpec::OpenAPI.path = 'doc/schema.yaml'

# Disable generating `example`
RSpec::OpenAPI.enable_example = false

# Change `info.version`
RSpec::OpenAPI.application_version = '1.0.0'

# Generate a comment on top of a schema file
RSpec::OpenAPI.comment = <<~EOS
  This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi

  When you write a spec in spec/requests, running the spec with `OPENAPI=1 rspec` will
  update this file automatically. You can also manually edit this file.
EOS

# Generate a custom description, given an RSpec example
RSpec::OpenAPI.description_builder = -> (example) { example.description }

How can I add information which can't be generated from RSpec?

rspec-openapi tries to keep manual modifications as much as possible when generating specs. You can directly edit doc/openapi.yaml as you like without spoiling the automatic generation capability.

Can I exclude specific specs from OpenAPI generation?

Yes, you can specify openapi: false to disable the automatic generation.

RSpec.describe '/resources', type: :request, openapi: false do
  # ...
end

# or

RSpec.describe '/resources', type: :request do
  it 'returns a resource', openapi: false do
    # ...
  end
end

Project status

Beta

Basic features are there, and some people are already using this.

Current limitation

  • Generating a JSON file is not supported yet

Other missing features with notes

  • Delete obsoleted endpoints
    • Give up, or at least make the feature optional?
    • Running all to detect obsoleted endpoints is sometimes not realistic anyway.
  • Intelligent merges
    • To maintain both automated changes and manual edits, the schema merge needs to be intelligent.
    • We'll just deep-reverse-merge schema for now, but if there's a $ref for example, modifications there should be rerouted to the referenced object.
    • A type could be an array of all possible types when merged.

Links

Existing RSpec plugins which have OpenAPI integration:

Acknowledgements

This gem was heavily inspired by the following gem:

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].