Generate api document (OpenAPI) side only from Rails routing.
Provides a rake command to help generate, view, and edit OpenAPI documents.
bundle exec rake routes:oas:init # r2-oas initialize
bundle exec rake routes:oas:docs # generate oas_docs
bundle exec rake routes:oas:ui # view at swagger ui
bundle exec rake routes:oas:editor # edit at swagger editor
bundle exec rake routes:oas:monitor # monitor oas_docs and analyze
bundle exec rake routes:oas:build # build oas_docs from src
bundle exec rake routes:oas:clean # clean unused components
bundle exec rake routes:oas:analyze # analyze oas_docs and generae src
bundle exec rake routes:oas:deploy # deploy oas_docs to deploy_docsAdd this line to your application's Gemfile:
group :development do
gem 'r2-oas'
endAnd then execute:
$ bundle
Or install it yourself as:
$ gem install r2-oas
If you want to view with Swagger UI or edit with Swagger Editor, This gem needs the following:
swaggerapi/swagger-ui:latestdocker imageswaggerapi/swagger-editor:latestdocker imagechromedriver
If you do not have it download as below.
$ docker pull swaggerapi/swagger-editor:latest
$ docker pull swaggerapi/swagger-ui:latest
$ brew install chromedriver
After requiring a gem and Configure Rakefile in your rails project
R2OAS.load_tasks$ bundle exec rake routes:oas:init
create oas_docs
create oas_docs/.paths
create oas_docs/plugins/helpers
create oas_docs/tasks/helpers
create oas_docs/plugins/.gitkeep
create oas_docs/plugins/helpers/.gitkeep
create oas_docs/tasks/.gitkeep
create oas_docs/tasks/helpers/.gitkeep
$ bundle exec rake routes:oas:docs
$ bundle exec rake routes:oas:editoroas_docs.mp4
oas_editor.mp4
You can execute the following command in the root directory of rails.
The following are examples of typical command usage.
Full docs are available at https://yukihirop.github.io/r2-oas
Initialize r2-oas.
$ bundle exec rake routes:oas:init
create oas_docs
create oas_docs/.paths
create oas_docs/plugins/helpers
create oas_docs/tasks/helpers
create oas_docs/plugins/.gitkeep
create oas_docs/plugins/helpers/.gitkeep
create oas_docs/tasks/.gitkeep
create oas_docs/tasks/helpers/.gitkeepGenerate docs.
$ bundle exec rake routes:oas:docs # Generate docs
$ PATHS_FILE="oas_docs/schema/paths/api/v1/task.yml" bundle exec rake routes:oas:docs # Generate docs by specify unit pathsStart Swagger editor.
$ bundle exec rake routes:oas:editor # Start swagger editor
$ PATHS_FILE="oas_docs/schema/paths/api/v1/task.yml" bundle exec rake routes:oas:editor # Start swagger editor by specify unit pathsStart swagger ui.
$ bundle exec rake routes:oas:ui # Start swagger ui
$ PATHS_FILE="oas_docs/schema/paths/api/v1/task.yml" bundle exec rake routes:oas:ui # Start swagger ui by specify unit pathsBuild docs.
Plugin is applied
$ bundle exec rake routes:oas:buildAnalyze docs.
Reads OpenAPI format document and divides it into several parts to generate a source file
$ OAS_FILE="~/Desktop/swagger.yml" bundle exec rake routes:oas:analyzeFull docs are available at https://yukihirop.github.io/r2-oas
- Rails 7.2.2.2
- Rails (>= 8.x)
- Ruby 3.2.6
- Ruby 3.3.6
- Ruby (>= 3.4.x)
- Rails Engine Routing
- Rails Normal Routing
Full docs are available at https://yukihirop.github.io/r2-oas/#/schema/3.0.0
-
tag namerepresentscontroller nameand determinespaths file name.- For example, If
controller nameisApi::V1::UsersController,tag_nameisapi/v1/user, thenpaths file nameisapi/v1/user.yml
- For example, If
-
_ofcomponents/{schemas,requestBodies, ...} nameconvert/when save file.- For example, If
components/schemas nameisApi_V1_User,components/schemas file nameisapi/v1/user.yml. _is supposed to be used to expressnamespace.- format is
Namespace1_Namespace2_Model.
- For example, If
-
.ofcomponents/{schemas,requestBodies, ...} nameconvert/when saving the file.- For example, If
components/schemas nameisapi.v1.User,components/schemas file nameisapi/v1/user.yml. .is supposed to be used to expressnamespace.- format is
namespace1.namespace2.Model.
- For example, If
All settings are optional
Full docs are available at https://yukihirop.github.io/r2-oas/#/setting/configure
# Errors here can prevent type information from showing when hovering in the editor
bundle exec steep stats
# You can check the error details with this command
bundle exec steep checkbundle exec rake steep:ignore:dig | pbcopy
# =>
#
# ignore 'RBS::DuplicatedMethodDefinition'
# ignore 'Ruby::ArgumentTypeMismatch'bundle exec rake steep:ignore:file | pbcopy
# =>
#
# ignore 'lib/r2-oas.rb'
# ignore 'lib/r2-oas/app_configuration.rb'
# ignore 'lib/r2-oas/app_configuration/deprecation.rb'bundle exec rake steep:ignore:preview
# =>
# 📄 lib/r2-oas/schema/monitor.rb
# Line 50: if current_time - last_check_time >= interval_to_save_edited_tmp_schema
# → if current_time - last_check_time >= interval_to_save_edited_tmp_schema # steep:ignore Ruby::NoMethod
# Line 62: analyzer.analyze_docs
# → analyzer.analyze_docs # steep:ignore Ruby::NoMethod
# Line 66: YAML.load_file(doc_save_file_path) || @after_schema_data
# → YAML.load_file(doc_save_file_path) || @after_schema_data # steep:ignore Ruby::UnknownConstantbundle exec rake steep:ignore:auto
# =>
# Analyzing steep check errors...
# ✅ Modified: lib/r2-oas/app_configuration/deprecation.rb (2 lines)
# ✅ Modified: lib/r2-oas/plugin/base.rb (3 lines)
# ✅ Modified: lib/r2-oas/schema/squeezer.rb (2 lines)/bin/bash devscript/all_support_ruby.sh bundle
.
.
.
===== Bundle install for All Support Ruby Result =====
ruby-3.2.6: 0
ruby-3.3.6: 0
ruby-3.4.7: 0
======================================================If specify ruby version 3.2.6 and 3.3.6
/bin/bash devscript/all_support_ruby.sh bundle 3.2.6 3.3.6
.
.
.
===== Bundle install for All Support Ruby Result =====
ruby-3.2.6: 0
ruby-3.3.6: 0
======================================================/bin/bash devscript/all_support_ruby.sh rspec
.
.
.
===== Rspec for All Support Ruby Result =====
ruby-3.2.6: 0
ruby-3.3.6: 0
ruby-3.4.7: 0
=============================================If specify ruby version 3.2.6 and 3.3.6
/bin/bash devscript/all_support_ruby.sh rspec 3.2.6 3.3.6
.
.
.
===== Rspec for All Support Ruby Result =====
ruby-3.2.6: 0
ruby-3.3.6: 0
=============================================Use rack-cors to enable CORS.
require 'rack/cors'
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [ :get, :post, :put, :delete, :options ]
end
endAlternatively, you can set CORS headers in a before block.
before do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
endThe gem is available as open source under the terms of the MIT License.
- Fork it ( http://github.com/yukihirop/r2-oas/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
