Compare commits

..

No commits in common. "master" and "2.3.2" have entirely different histories.

17 changed files with 32 additions and 141 deletions

View File

@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: [2.6, 2.7, truffleruby-head]
ruby: [2.6, 2.7]
rails: [5.2.4.4, 6.0.3.4]
env:
RAILS_VERSION: ${{ matrix.rails }}
@ -31,7 +31,6 @@ jobs:
- name: Install dependencies
run: |
gem update --system
bundle install
cd rswag-ui && npm install

View File

@ -5,21 +5,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [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
### Fixed
- Include response examples [#394](https://github.com/rswag/rswag/pull/394)
### Changed
- Update swagger-ui to 3.42.0
## [2.3.2] - 2021-01-27
### Added
- RequestBody now supports the `required` flag [#342](https://github.com/rswag/rswag/pull/342)

View File

@ -57,11 +57,11 @@ Push to your fork and [submit a Pull Request][pr].
## Updating Swagger UI
Find the latest versions of swagger-ui here:
Find the latest versions of swagger-ui here:
https://github.com/swagger-api/swagger-ui/releases
Update the swagger-ui-dist version in the rswag-ui dependencies
```
```
./rswag-ui/package.json
```

View File

@ -25,25 +25,23 @@ end
gem 'rswag-api', path: './rswag-api'
gem 'rswag-ui', path: './rswag-ui'
group :development, :test do
gem 'rswag-specs', path: './rswag-specs'
end
group :test do
gem 'capybara'
gem 'geckodriver-helper'
gem 'generator_spec'
gem 'rspec-rails'
gem 'selenium-webdriver'
gem 'rswag-specs', path: './rswag-specs'
gem 'test-unit'
end
group :development do
gem 'rswag-specs', path: './rswag-specs'
gem 'rubocop'
end
group :assets do
gem 'mini_racer'
gem 'therubyracer'
gem 'uglifier'
end

View File

@ -5,8 +5,6 @@ rswag
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.
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.
@ -20,7 +18,7 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
|----------|----------|----------|
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.52.5|
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.28.0|
|[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|
|[1.6.0](https://github.com/rswag/rswag/tree/1.6.0)|2.0|2.2.5|
@ -138,7 +136,7 @@ There is also a generator which can help get you started `rails generate rspec:s
path '/blogs/{id}' do
get 'Retrieves a blog' do
tags 'Blogs', 'Another Tag'
tags 'Blogs'
produces 'application/json', 'application/xml'
parameter name: :id, in: :path, type: :string
@ -618,12 +616,15 @@ after do |example|
end
```
#### Dry Run Option ####
You need to disable --dry-run option for Rspec > 3
The `--dry-run` option is enabled by default for Rspec 3, but if you need to
disable it you can use the environment varible `SWAGGER_DRY_RUN=0` during the
generation command or add the following to your `config/environments/test.rb`:
<!-- This is now enabled by default in rswag.
You need to set the ``` config.swagger_dry_run = false``` value in the spec/spec_helper.rb file.
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.
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
RSpec.configure do |config|
config.swagger_dry_run = false

View File

@ -15,5 +15,5 @@ Gem::Specification.new do |s|
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
s.add_dependency 'railties', '>= 3.1', '< 7.1'
s.add_dependency 'railties', '>= 3.1', '< 7.0'
end

View File

@ -28,11 +28,9 @@ module Rswag
end
def swagger_dry_run
return @swagger_dry_run if defined? @swagger_dry_run
if ENV.key?('SWAGGER_DRY_RUN')
@rspec_config.swagger_dry_run = ENV['SWAGGER_DRY_RUN'] == '1'
@swagger_dry_run ||= begin
@rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
end
@swagger_dry_run = @rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
end
def swagger_format

View File

@ -194,36 +194,12 @@ module Rswag
def build_json_payload(parameters, example)
body_param = parameters.select { |p| p[:in] == :body }.first
return nil unless body_param
raise(MissingParameterError, body_param[:name]) unless example.respond_to?(body_param[:name])
example.send(body_param[:name]).to_json
body_param ? example.send(body_param[:name]).to_json : nil
end
def doc_version(doc)
doc[:openapi] || doc[:swagger] || '3'
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

View File

@ -50,11 +50,7 @@ module Rswag
.merge(schemas)
errors = JSON::Validator.fully_validate(validation_schema, body)
return unless errors.any?
raise UnexpectedResponse,
"Expected response body to match schema: #{errors[0]}\n" \
"Response body: #{JSON.pretty_generate(JSON.parse(body))}"
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
end
def definitions_or_component_schemas(swagger_doc, version)
@ -66,7 +62,7 @@ module Rswag
swagger_doc.slice(:definitions)
else
components = swagger_doc[:components] || {}
{ components: components }
{ components: { schemas: components[:schemas] } }
end
end
end

View File

@ -132,8 +132,9 @@ module Rswag
def upgrade_content!(mime_list, target_node)
schema = target_node[:schema]
return if mime_list.empty? || schema.nil?
target_node[:content] ||= {}
target_node.merge!(content: {})
mime_list.each do |mime_type|
# TODO upgrade to have content-type specific schema
(target_node[:content][mime_type] ||= {}).merge!(schema: schema)

View File

@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
s.add_dependency 'activesupport', '>= 3.1', '< 7.1'
s.add_dependency 'railties', '>= 3.1', '< 7.1'
s.add_dependency 'activesupport', '>= 3.1', '< 7.0'
s.add_dependency 'railties', '>= 3.1', '< 7.0'
s.add_dependency 'json-schema', '~> 2.2'
end

View File

@ -160,21 +160,6 @@ module Rswag
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
before do
metadata[:operation][:consumes] = ['multipart/form-data']

View File

@ -28,11 +28,10 @@ module Rswag
{
path_item: { template: '/blogs', parameters: [{ type: :string }] },
operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] },
response: response_metadata,
response: { code: '201', description: 'blog created', headers: { type: :string }, schema: { '$ref' => '#/definitions/blog' } },
document: document
}
end
let(:response_metadata) { { code: '201', description: 'blog created', headers: { type: :string }, schema: { '$ref' => '#/definitions/blog' } } }
context 'with the document tag set to false' do
let(:swagger_doc) { { swagger: '2.0' } }
@ -130,47 +129,6 @@ module Rswag
)
end
context 'with response example' do
let(:response_metadata) do
{
code: '201',
description: 'blog created',
headers: { type: :string },
content: { 'application/json' => { example: { foo: :bar } } },
schema: { '$ref' => '#/definitions/blog' }
}
end
it 'adds example to definition' do
expect(swagger_doc.slice(:paths)).to match(
paths: {
'/blogs' => {
parameters: [{ schema: { type: :string } }],
post: {
parameters: [{ schema: { type: :string } }],
summary: 'Creates a blog',
responses: {
'201' => {
content: {
'application/vnd.my_mime' => {
schema: { '$ref' => '#/definitions/blog' }
},
'application/json' => {
schema: { '$ref' => '#/definitions/blog' },
example: { foo: :bar }
}
},
description: 'blog created',
headers: { schema: { type: :string } }
}
}
}
}
}
)
end
end
context 'with empty content' do
let(:swagger_doc) do
{

View File

@ -5,9 +5,9 @@
"requires": true,
"dependencies": {
"swagger-ui-dist": {
"version": "3.52.5",
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.52.5.tgz",
"integrity": "sha512-8z18eX8G/jbTXYzyNIaobrnD7PSN7yU/YkSasMmajrXtw0FGS64XjrKn5v37d36qmU3o1xLeuYnktshRr7uIFw=="
"version": "3.28.0",
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.28.0.tgz",
"integrity": "sha512-aPkfTzPv9djSiZI1NUkWr5HynCUsH+jaJ0WSx+/t19wq7MMGg9clHm9nGoIpAtqml1G51ofI+I75Ym72pukzFg=="
}
}
}

View File

@ -3,6 +3,6 @@
"version": "1.0.0",
"private": true,
"dependencies": {
"swagger-ui-dist": "3.52.5"
"swagger-ui-dist": "3.28.0"
}
}

View File

@ -15,6 +15,6 @@ Gem::Specification.new do |s|
s.files = Dir.glob('{lib,node_modules}/**/*') + ['MIT-LICENSE', 'Rakefile' ]
s.add_dependency 'actionpack', '>=3.1', '< 7.1'
s.add_dependency 'railties', '>= 3.1', '< 7.1'
s.add_dependency 'actionpack', '>=3.1', '< 7.0'
s.add_dependency 'railties', '>= 3.1', '< 7.0'
end

View File

@ -221,12 +221,6 @@
},
"content": {
"application/json": {
"example": {
"id": 1,
"title": "Hello world!",
"content": "Hello world and hello universe. Thank you all very much!!!",
"thumbnail": "thumbnail.png"
},
"schema": {
"$ref": "#/definitions/blog"
}