24 Commits

Author SHA1 Message Date
Blake Erickson
989aab656f Revert "Add a macro for complexes multiparts" 2021-03-06 14:52:49 -07:00
Blake Erickson
f7036b7e5a Merge pull request #300 from sobrinho/master
Add a macro for complexes multiparts
2021-03-06 10:22:54 -07:00
Blake Erickson
84ab7a9e4c Merge pull request #405 from gogainda/master
Add Truffleruby head to CI
2021-03-06 10:01:05 -07:00
Blake Erickson
0169fbab66 Merge pull request #404 from Wolfer/patch-1
Allow use #/components/parameters and other in inherited $refs
2021-03-06 09:58:10 -07:00
Blake Erickson
4c42ad5f97 Merge pull request #406 from mynnx/specs-print-failed-body
Show the response body for comparison when schema checks fail
2021-03-06 09:30:48 -07:00
Blake Erickson
b91b6e5f1e Fix bundler warning
Move rswag-specs under development and test to resolve this warning:

```
Your Gemfile lists the gem rswag-specs (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
```
2021-03-05 21:34:00 -07:00
mynnx
a34c931bb6 Show the response body for comparison when schema checks fail 2021-03-03 16:32:16 -08:00
Igor Victor
4b7ab9d381 Add Truffleruby head to CI 2021-02-26 17:58:30 +01:00
Serg F
3d3d93f3ab Allow use #/components/parameters and other in inherited $refs 2021-02-19 15:15:44 +03:00
Blake Erickson
0aca50c66c Update changelog with 2.4.0 info 2021-02-09 11:51:34 -07:00
Blake Erickson
9644a16bce Merge branch 'splitpayments-dry-run-env' 2021-02-09 08:07:35 -07:00
Blake Erickson
52939874d6 Clean up README merge conflict 2021-02-09 08:05:58 -07:00
Blake Erickson
7ef900ec1d version bump 2021-02-07 07:55:52 -07:00
Blake Erickson
c0142093d4 Update swagger-ui to 3.42.0 2021-02-07 07:47:52 -07:00
Blake Erickson
1f4ecb3c10 update changelog 2021-02-07 06:31:47 -07:00
Blake Erickson
aa4e6f2070 Include example definition in test app
From changes in this commit: eadaf34ef6

we need to include the new output in the test-app swagger.json file
2021-02-06 17:42:23 -07:00
Blake Erickson
eadaf34ef6 Merge pull request #394 from donny741/master
Fix response examples
2021-02-06 07:30:53 -07:00
Donatas Povilaitis
cbaf6cd3e4 Fix response examples 2021-01-29 20:47:46 +02:00
Blake Erickson
670c94cc44 Update version to 2.3.2 in changelog 2021-01-27 14:53:15 -07:00
Blake Erickson
b86d3063a8 Merge pull request #371 from rswag/jamie
PR Grooming
2021-01-27 07:59:59 -07:00
Gabriel Sobrinho
f8dbd98bbc Add a macro for complexes multiparts
This will allow to describe multipart in a short way, like JSON payload:

Before:

    put 'Creates a blog with thumbnail' do
      consumes 'multipart/form-data'
      parameter name: :title, in: :formData, type: :string, required: true
      parameter name: :content, in: :formData, type: :string, required: true
      parameter name: :file, in: :formData, type: :file, required: true

      let(:blog) { FactoryBot.build(:blog) }
      let(:title) { blog.title }
      let(:content) { blog.content }
      let(:file) { blog.file }

      ...
    end

After:

    put 'Creates a blog with thumbnail' do
      consumes 'multipart/form-data'
      parameter name: :blog, in: :formData, schema: { '$ref' => '#/definitions/blog' }

      let(:blog) { FactoryBot.attributes_for(:blog) }

      ...
    end

Your mileage may vary but you can always choose the best option.
2020-04-16 22:38:35 -03:00
Igor Kapkov
9916d3f0b0 Merge branch 'master' into dry-run-env 2020-03-25 16:59:30 +11:00
Igor Kapkov
56eec5948e Update readme 2020-03-25 16:57:58 +11:00
Igor Kapkov
7b01ae1aa1 Add ENV var to configure --dry-run 2020-01-22 15:54:24 +11:00
11 changed files with 88 additions and 26 deletions

View File

@@ -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 }}

View File

@@ -4,15 +4,25 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
## [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 ### Added
- RequestBody now supports the `required` flag [#342](https://github.com/rswag/rswag/pull/342) - RequestBody now supports the `required` flag [#342](https://github.com/rswag/rswag/pull/342)
### Changed
### Deprecated
### Removed
### Fixed ### Fixed
- Fix response example rendering [#330](https://github.com/rswag/rswag/pull/330) - Fix response example rendering [#330](https://github.com/rswag/rswag/pull/330)
- Fix empty content block [#347](https://github.com/rswag/rswag/pull/347) - Fix empty content block [#347](https://github.com/rswag/rswag/pull/347)
### Security
## [2.3.1] - 2020-04-08 ## [2.3.1] - 2020-04-08
### Fixed ### Fixed

View File

@@ -25,18 +25,20 @@ 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

View File

@@ -18,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| |Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
|----------|----------|----------| |----------|----------|----------|
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.28.0| |[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.42.0|
|[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|
@@ -616,15 +616,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

View File

@@ -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

View File

@@ -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

View File

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

View File

@@ -28,10 +28,11 @@ module Rswag
{ {
path_item: { template: '/blogs', parameters: [{ type: :string }] }, path_item: { template: '/blogs', parameters: [{ type: :string }] },
operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] }, operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] },
response: { code: '201', description: 'blog created', headers: { type: :string }, schema: { '$ref' => '#/definitions/blog' } }, response: response_metadata,
document: document document: document
} }
end 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 context 'with the document tag set to false' do
let(:swagger_doc) { { swagger: '2.0' } } let(:swagger_doc) { { swagger: '2.0' } }
@@ -129,6 +130,47 @@ module Rswag
) )
end 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 context 'with empty content' do
let(:swagger_doc) do let(:swagger_doc) do
{ {

View File

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

View File

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

View File

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