Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/narazaka/swagger-dsl
Swagger (OpenAPI 3) DSL for Ruby
https://github.com/narazaka/swagger-dsl
json-schema openapi rails ruby swagger
Last synced: about 1 month ago
JSON representation
Swagger (OpenAPI 3) DSL for Ruby
- Host: GitHub
- URL: https://github.com/narazaka/swagger-dsl
- Owner: Narazaka
- License: zlib
- Created: 2019-11-30T14:59:26.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-08T10:30:55.000Z (almost 3 years ago)
- Last Synced: 2024-04-26T02:41:29.470Z (9 months ago)
- Topics: json-schema, openapi, rails, ruby, swagger
- Language: Ruby
- Size: 81.1 KB
- Stars: 7
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Swagger::DSL
Swagger (OpenAPI 3) DSL
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'swagger-dsl'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install swagger-dsl
## Usage
see also [swagger-serializer](https://github.com/Narazaka/swagger-serializer)
```ruby
class BaseSerializer
extend Swagger::DSL::Serializer
endclass UserSerializer < BaseSerializer
swagger do
id :integer
name :string
age :integer, minimum: 18
end
endclass ApplicationController
extend Swagger::DSL::RailsController
endclass UsersController < ApplicationController
swagger :update do
params do
path :id, schema: :integer
query do
safe schema: :boolean, required: false
redirect do
schema do
string!
format! "url"
end
end
end
endbody do
# name :string
# age :integer, minimum: 18
cref! UserSerializer # "#/components/User"
endrender 200, dsl: :jimmy do # another json schema dsl by "jimmy" gem
object do
string :status, enum: %w[ok], default: :ok
cref :user, UserSerializer
require all
end
end
enddef update
# some
end
endSwagger::DSL.current["info"] = {
"title" => "my app",
"version" => "0.1.0",
}JSON.dump(Swagger::DSL.current)
```### required default
Body and response parameters are "required" default in default DSL.
So `params` are also default "required".
If you do not want it, `Swagger::DSL.current.config.default_required = false`.
### If Rails eager_load = true
Rails controllers will be loaded before loading the routes when eager_load is enabled.
So set `Swagger::DSL.current.config.lazy_define_paths = true`, `reload_routes!` and `Swagger::DSL.current.define_paths!`
```ruby
if Rails.application.config.eager_load
Swagger::DSL.current.config.lazy_define_paths = true
Rails.application.config.after_initialize do
Rails.application.reload_routes!
Swagger::DSL.current.define_paths!
JSON.dump(Swagger::DSL.current)
end
end
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/swagger-dsl.