mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16e40bfcf3 | ||
|
|
8c6aed4a9e | ||
|
|
8e59d2b2f2 | ||
|
|
a813b63bcc | ||
|
|
076f71388c | ||
|
|
9cfb5bc557 | ||
|
|
6cafc0c0e5 | ||
|
|
86c512f639 | ||
|
|
32638062d7 | ||
|
|
dd6530b718 | ||
|
|
095906da58 | ||
|
|
a40a06646f | ||
|
|
42c8e123a8 | ||
|
|
c85fa3b37a | ||
|
|
985f444834 | ||
|
|
1d2a25e231 | ||
|
|
a0680506e2 | ||
|
|
4a32108f78 | ||
|
|
05e22c3bd7 | ||
|
|
989aab656f | ||
|
|
f7036b7e5a | ||
|
|
84ab7a9e4c | ||
|
|
0169fbab66 | ||
|
|
4c42ad5f97 | ||
|
|
b91b6e5f1e | ||
|
|
a34c931bb6 | ||
|
|
4b7ab9d381 | ||
|
|
3d3d93f3ab | ||
|
|
0aca50c66c | ||
|
|
9644a16bce | ||
|
|
52939874d6 | ||
|
|
f8dbd98bbc | ||
|
|
9916d3f0b0 | ||
|
|
56eec5948e | ||
|
|
7b01ae1aa1 |
3
.github/workflows/ruby.yml
vendored
3
.github/workflows/ruby.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
ruby: [2.6, 2.7]
|
ruby: [2.6, 2.7, truffleruby-head]
|
||||||
rails: [5.2.4.4, 6.0.3.4]
|
rails: [5.2.4.4, 6.0.3.4]
|
||||||
env:
|
env:
|
||||||
RAILS_VERSION: ${{ matrix.rails }}
|
RAILS_VERSION: ${{ matrix.rails }}
|
||||||
@ -31,6 +31,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
|
gem update --system
|
||||||
bundle install
|
bundle install
|
||||||
cd rswag-ui && npm install
|
cd rswag-ui && npm install
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Update swagger-ui to 3.52.5
|
||||||
|
|
||||||
|
## [2.4.0] - 2021-02-09
|
||||||
|
### Added
|
||||||
|
- Added `SWAGGER_DRY_RUN` env variable [#274](https://github.com/rswag/rswag/pull/274)
|
||||||
|
|
||||||
## [2.3.3] - 2021-02-07
|
## [2.3.3] - 2021-02-07
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|||||||
8
Gemfile
8
Gemfile
@ -25,23 +25,25 @@ end
|
|||||||
gem 'rswag-api', path: './rswag-api'
|
gem 'rswag-api', path: './rswag-api'
|
||||||
gem 'rswag-ui', path: './rswag-ui'
|
gem 'rswag-ui', path: './rswag-ui'
|
||||||
|
|
||||||
|
group :development, :test do
|
||||||
|
gem 'rswag-specs', path: './rswag-specs'
|
||||||
|
end
|
||||||
|
|
||||||
group :test do
|
group :test do
|
||||||
gem 'capybara'
|
gem 'capybara'
|
||||||
gem 'geckodriver-helper'
|
gem 'geckodriver-helper'
|
||||||
gem 'generator_spec'
|
gem 'generator_spec'
|
||||||
gem 'rspec-rails'
|
gem 'rspec-rails'
|
||||||
gem 'selenium-webdriver'
|
gem 'selenium-webdriver'
|
||||||
gem 'rswag-specs', path: './rswag-specs'
|
|
||||||
gem 'test-unit'
|
gem 'test-unit'
|
||||||
end
|
end
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
gem 'rswag-specs', path: './rswag-specs'
|
|
||||||
gem 'rubocop'
|
gem 'rubocop'
|
||||||
end
|
end
|
||||||
|
|
||||||
group :assets do
|
group :assets do
|
||||||
gem 'therubyracer'
|
gem 'mini_racer'
|
||||||
gem 'uglifier'
|
gem 'uglifier'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
17
README.md
17
README.md
@ -5,6 +5,8 @@ rswag
|
|||||||
|
|
||||||
OpenApi 3.0 and Swagger 2.0 compatible!
|
OpenApi 3.0 and Swagger 2.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 automaticaly runs the tests. Once you have green tests, run a rake task to auto-generate corresponding Swagger files and expose them as YAML or JSON endpoints. Rswag also provides an embedded version of the awesome [swagger-ui](https://github.com/swagger-api/swagger-ui) that's powered by the exposed file. This toolchain makes it seamless to go from integration specs, which youre probably doing in some form already, to living documentation for your API consumers.
|
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 automaticaly runs the tests. Once you have green tests, run a rake task to auto-generate corresponding Swagger files and expose them as YAML or JSON endpoints. Rswag also provides an embedded version of the awesome [swagger-ui](https://github.com/swagger-api/swagger-ui) that's powered by the exposed file. This toolchain makes it seamless to go from integration specs, which youre probably doing in some form already, to living documentation for your API consumers.
|
||||||
|
|
||||||
Api Rswag creates [Swagger](http://swagger.io) tooling for Rails API's. Generate beautiful API documentation, including a UI to explore and test operations, directly from your rspec integration tests.
|
Api Rswag creates [Swagger](http://swagger.io) tooling for Rails API's. Generate beautiful API documentation, including a UI to explore and test operations, directly from your rspec integration tests.
|
||||||
@ -18,7 +20,7 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
|||||||
|
|
||||||
|Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
|
|Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
|
||||||
|----------|----------|----------|
|
|----------|----------|----------|
|
||||||
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.42.0|
|
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.52.5|
|
||||||
|[2.3.0](https://github.com/rswag/rswag/tree/2.3.0)|3.0.3|3.23.11|
|
|[2.3.0](https://github.com/rswag/rswag/tree/2.3.0)|3.0.3|3.23.11|
|
||||||
|[2.2.0](https://github.com/rswag/rswag/tree/2.2.0)|2.0|3.18.2|
|
|[2.2.0](https://github.com/rswag/rswag/tree/2.2.0)|2.0|3.18.2|
|
||||||
|[1.6.0](https://github.com/rswag/rswag/tree/1.6.0)|2.0|2.2.5|
|
|[1.6.0](https://github.com/rswag/rswag/tree/1.6.0)|2.0|2.2.5|
|
||||||
@ -136,7 +138,7 @@ There is also a generator which can help get you started `rails generate rspec:s
|
|||||||
path '/blogs/{id}' do
|
path '/blogs/{id}' do
|
||||||
|
|
||||||
get 'Retrieves a blog' do
|
get 'Retrieves a blog' do
|
||||||
tags 'Blogs'
|
tags 'Blogs', 'Another Tag'
|
||||||
produces 'application/json', 'application/xml'
|
produces 'application/json', 'application/xml'
|
||||||
parameter name: :id, in: :path, type: :string
|
parameter name: :id, in: :path, type: :string
|
||||||
|
|
||||||
@ -616,15 +618,12 @@ after do |example|
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
You need to disable --dry-run option for Rspec > 3
|
#### Dry Run Option ####
|
||||||
|
|
||||||
<!-- This is now enabled by default in rswag.
|
The `--dry-run` option is enabled by default for Rspec 3, but if you need to
|
||||||
You need to set the ``` config.swagger_dry_run = false``` value in the spec/spec_helper.rb file.
|
disable it you can use the environment varible `SWAGGER_DRY_RUN=0` during the
|
||||||
This is one of the more powerful features of rswag. When rswag runs your integration test suite via ```bundle exec rspec```, it will capture the request and response bodies and output those values in the examples section.
|
generation command or add the following to your `config/environments/test.rb`:
|
||||||
These integration tests are usually written with ```let``` variables for post body parameters, and since its an integration test the service is returning actual values.
|
|
||||||
We might as well re-use these values and embed them into the generated swagger to provide a more real world example for request/response examples. -->
|
|
||||||
|
|
||||||
Add to config/environments/test.rb:
|
|
||||||
```ruby
|
```ruby
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.swagger_dry_run = false
|
config.swagger_dry_run = false
|
||||||
|
|||||||
@ -15,5 +15,5 @@ Gem::Specification.new do |s|
|
|||||||
|
|
||||||
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
|
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
|
||||||
|
|
||||||
s.add_dependency 'railties', '>= 3.1', '< 7.0'
|
s.add_dependency 'railties', '>= 3.1', '< 7.1'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -28,9 +28,11 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
def swagger_dry_run
|
def swagger_dry_run
|
||||||
@swagger_dry_run ||= begin
|
return @swagger_dry_run if defined? @swagger_dry_run
|
||||||
@rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
|
if ENV.key?('SWAGGER_DRY_RUN')
|
||||||
|
@rspec_config.swagger_dry_run = ENV['SWAGGER_DRY_RUN'] == '1'
|
||||||
end
|
end
|
||||||
|
@swagger_dry_run = @rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
|
||||||
end
|
end
|
||||||
|
|
||||||
def swagger_format
|
def swagger_format
|
||||||
|
|||||||
@ -194,12 +194,36 @@ module Rswag
|
|||||||
|
|
||||||
def build_json_payload(parameters, example)
|
def build_json_payload(parameters, example)
|
||||||
body_param = parameters.select { |p| p[:in] == :body }.first
|
body_param = parameters.select { |p| p[:in] == :body }.first
|
||||||
body_param ? example.send(body_param[:name]).to_json : nil
|
|
||||||
|
return nil unless body_param
|
||||||
|
|
||||||
|
raise(MissingParameterError, body_param[:name]) unless example.respond_to?(body_param[:name])
|
||||||
|
|
||||||
|
example.send(body_param[:name]).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def doc_version(doc)
|
def doc_version(doc)
|
||||||
doc[:openapi] || doc[:swagger] || '3'
|
doc[:openapi] || doc[:swagger] || '3'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class MissingParameterError < StandardError
|
||||||
|
attr_reader :body_param
|
||||||
|
|
||||||
|
def initialize(body_param)
|
||||||
|
@body_param = body_param
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
<<~MSG
|
||||||
|
Missing parameter '#{body_param}'
|
||||||
|
|
||||||
|
Please check your spec. It looks like you defined a body parameter,
|
||||||
|
but did not declare usage via let. Try adding:
|
||||||
|
|
||||||
|
let(:#{body_param}) {}
|
||||||
|
MSG
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -50,7 +50,11 @@ module Rswag
|
|||||||
.merge(schemas)
|
.merge(schemas)
|
||||||
|
|
||||||
errors = JSON::Validator.fully_validate(validation_schema, body)
|
errors = JSON::Validator.fully_validate(validation_schema, body)
|
||||||
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
|
return unless errors.any?
|
||||||
|
|
||||||
|
raise UnexpectedResponse,
|
||||||
|
"Expected response body to match schema: #{errors[0]}\n" \
|
||||||
|
"Response body: #{JSON.pretty_generate(JSON.parse(body))}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def definitions_or_component_schemas(swagger_doc, version)
|
def definitions_or_component_schemas(swagger_doc, version)
|
||||||
@ -62,7 +66,7 @@ module Rswag
|
|||||||
swagger_doc.slice(:definitions)
|
swagger_doc.slice(:definitions)
|
||||||
else
|
else
|
||||||
components = swagger_doc[:components] || {}
|
components = swagger_doc[:components] || {}
|
||||||
{ components: { schemas: components[:schemas] } }
|
{ components: components }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -15,7 +15,7 @@ Gem::Specification.new do |s|
|
|||||||
|
|
||||||
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
|
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
|
||||||
|
|
||||||
s.add_dependency 'activesupport', '>= 3.1', '< 7.0'
|
s.add_dependency 'activesupport', '>= 3.1', '< 7.1'
|
||||||
s.add_dependency 'railties', '>= 3.1', '< 7.0'
|
s.add_dependency 'railties', '>= 3.1', '< 7.1'
|
||||||
s.add_dependency 'json-schema', '~> 2.2'
|
s.add_dependency 'json-schema', '~> 2.2'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -160,6 +160,21 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'missing body parameter' do
|
||||||
|
before do
|
||||||
|
metadata[:operation][:parameters] = [{ name: 'comment', in: :body, schema: { type: 'object' } }]
|
||||||
|
allow(example).to receive(:comment).and_raise(NoMethodError, "undefined method 'comment'")
|
||||||
|
allow(example).to receive(:respond_to?).with(:'Content-Type')
|
||||||
|
allow(example).to receive(:respond_to?).with('comment').and_return(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses the referenced metadata to build the request' do
|
||||||
|
expect do
|
||||||
|
request[:payload]
|
||||||
|
end.to raise_error(Rswag::Specs::MissingParameterError, /Missing parameter 'comment'/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'form payload' do
|
context 'form payload' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:consumes] = ['multipart/form-data']
|
metadata[:operation][:consumes] = ['multipart/form-data']
|
||||||
|
|||||||
6
rswag-ui/package-lock.json
generated
6
rswag-ui/package-lock.json
generated
@ -5,9 +5,9 @@
|
|||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"swagger-ui-dist": {
|
"swagger-ui-dist": {
|
||||||
"version": "3.42.0",
|
"version": "3.52.5",
|
||||||
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.42.0.tgz",
|
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.52.5.tgz",
|
||||||
"integrity": "sha512-hTNX6cX7KWtBZgk6ZQSOzsBJhqdCmD5NOIjb6dBPKSnYZidSkIXOcaPMR3+kwxLrj8bDC881bSDlNbLsHikacg=="
|
"integrity": "sha512-8z18eX8G/jbTXYzyNIaobrnD7PSN7yU/YkSasMmajrXtw0FGS64XjrKn5v37d36qmU3o1xLeuYnktshRr7uIFw=="
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"swagger-ui-dist": "3.42.0"
|
"swagger-ui-dist": "3.52.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,6 @@ Gem::Specification.new do |s|
|
|||||||
|
|
||||||
s.files = Dir.glob('{lib,node_modules}/**/*') + ['MIT-LICENSE', 'Rakefile' ]
|
s.files = Dir.glob('{lib,node_modules}/**/*') + ['MIT-LICENSE', 'Rakefile' ]
|
||||||
|
|
||||||
s.add_dependency 'actionpack', '>=3.1', '< 7.0'
|
s.add_dependency 'actionpack', '>=3.1', '< 7.1'
|
||||||
s.add_dependency 'railties', '>= 3.1', '< 7.0'
|
s.add_dependency 'railties', '>= 3.1', '< 7.1'
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user