RspecOpenapi

Public
Generate OpenAPI schema from RSpec request specs.
Icons/chart bar
Used 3 times
Created by
E Emanuele Bonanno

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/zJoswq"
Template Source

Review the code before running this template on your machine.

inject_into_file "Gemfile", after: "group :development, :test do" do
<<-RUBY
  gem 'rspec-openapi', require: false
RUBY
end

after_bundle do
  file "spec/support/openapi.rb", <<~CODE
  if ENV["OPENAPI"].present?
    require "rspec/openapi"
  
    RSpec::OpenAPI.info = {
      title: "<Your Title>",
      description: "<Your Description>"
    }
  
    RSpec::OpenAPI.application_version = ""
  
    RSpec::OpenAPI.request_headers = %w[Content-Type]
    RSpec::OpenAPI.servers = [
      { url: "http://localhost:3000" }
    ]
  
    RSpec::OpenAPI.security_schemes = {
      "BearerAuth" => {
        description: "Bearer Authentication",
        type: "http",
        scheme: "bearer",
        bearerFormat: "JWT"
      }
    }
    seed = ENV.fetch("OPENAPI_SEED", 0xFFFF)
    srand(seed)
    Faker::Config.random = Random.new(seed) if defined? Faker
  
    RSpec.configure do |config|
      config.order = :defined
      config.before do
        allow(Devise).to receive(:friendly_token) { ("A".."Z").to_a.sample(20).join } if defined?(Devise)
      end
  
      ActiveRecord::Base.connection.tables.each do |t|
        ActiveRecord::Base.connection.execute("ALTER TABLE #{t} AUTO_INCREMENT = 1")
      end
  
      config.include ActiveSupport::Testing::TimeHelpers
    end
  end
  CODE
end
Comments

Sign up or Login to leave a comment.