Skip to content

Commit 1118b62

Browse files
authored
Merge branch 'master' into task/add-requests-examples-support
2 parents 894dcfa + e23bc8a commit 1118b62

34 files changed

+1027
-157
lines changed

.github/workflows/ruby.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
ruby: [2.6, 2.7, truffleruby-head]
15+
rails: [5.2.4.4, 6.0.3.4]
16+
env:
17+
RAILS_VERSION: ${{ matrix.rails }}
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
- uses: ruby/setup-ruby@v1
22+
with:
23+
ruby-version: ${{ matrix.ruby }}
24+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
25+
26+
- uses: actions/cache@v2
27+
id: cache
28+
with:
29+
path: |
30+
rswag-ui/node_modules
31+
vendor/bundle
32+
key: ${{ runner.os }}-ruby_${{ matrix.ruby }}-rails_${{ matrix.rails }}-${{ hashFiles('Gemfile', '**/package-lock.json') }}
33+
34+
- name: Install dependencies
35+
run: |
36+
bundle install
37+
cd rswag-ui && npm install
38+
39+
- name: rswag-api
40+
run: |
41+
cd rswag-api
42+
bundle exec rspec
43+
44+
- name: rswag-specs
45+
if: success() || failure()
46+
run: |
47+
cd rswag-specs
48+
bundle exec rspec
49+
50+
- name: rswag-ui
51+
if: success() || failure()
52+
run: |
53+
cd rswag-ui
54+
bundle exec rspec
55+
56+
- name: test-app
57+
if: success() || failure()
58+
run: |
59+
cd test-app
60+
bundle exec rake db:migrate db:test:prepare
61+
bundle exec rspec

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.3
1+
2.7.2

CHANGELOG.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
# rswag
22
All notable changes to this project will be documented in this file.
33

4-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4+
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).
65

76
## [Unreleased]
7+
8+
### Changed
9+
- Update swagger-ui to 3.52.5
10+
11+
## [2.4.0] - 2021-02-09
812
### Added
13+
- Added `SWAGGER_DRY_RUN` env variable [#274](https://github.com/rswag/rswag/pull/274)
14+
15+
## [2.3.3] - 2021-02-07
16+
17+
### Fixed
18+
- Include response examples [#394](https://github.com/rswag/rswag/pull/394)
19+
920
### Changed
10-
### Deprecated
11-
### Removed
21+
- Update swagger-ui to 3.42.0
22+
23+
## [2.3.2] - 2021-01-27
24+
### Added
25+
- RequestBody now supports the `required` flag [#342](https://github.com/rswag/rswag/pull/342)
1226
### Fixed
13-
### Security
27+
- Fix response example rendering [#330](https://github.com/rswag/rswag/pull/330)
28+
- Fix empty content block [#347](https://github.com/rswag/rswag/pull/347)
1429

1530
## [2.3.1] - 2020-04-08
1631
### Fixed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ Push to your fork and [submit a Pull Request][pr].
5757

5858
## Updating Swagger UI
5959

60-
Find the latest versions of swagger-ui here:
60+
Find the latest versions of swagger-ui here:
6161
https://github.com/swagger-api/swagger-ui/releases
6262

6363
Update the swagger-ui-dist version in the rswag-ui dependencies
64-
```
64+
```
6565
./rswag-ui/package.json
6666
```
6767

Gemfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ end
2525
gem 'rswag-api', path: './rswag-api'
2626
gem 'rswag-ui', path: './rswag-ui'
2727

28+
group :development, :test do
29+
gem 'rswag-specs', path: './rswag-specs'
30+
end
31+
2832
group :test do
2933
gem 'capybara'
3034
gem 'geckodriver-helper'
3135
gem 'generator_spec'
3236
gem 'rspec-rails'
3337
gem 'selenium-webdriver'
34-
gem 'rswag-specs', path: './rswag-specs'
3538
gem 'test-unit'
3639
end
3740

3841
group :development do
39-
gem 'rswag-specs', path: './rswag-specs'
4042
gem 'rubocop'
4143
end
4244

4345
group :assets do
44-
gem 'therubyracer'
46+
gem 'mini_racer'
4547
gem 'uglifier'
4648
end
4749

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ rswag
55

66
OpenApi 3.0 and Swagger 2.0 compatible!
77

8+
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.
9+
810
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.
911

1012
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
1820

1921
|Rswag Version|Swagger (OpenAPI) Spec.|swagger-ui|
2022
|----------|----------|----------|
21-
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.28.0|
23+
|[master](https://github.com/rswag/rswag/tree/master)|3.0.3|3.52.5|
2224
|[2.3.0](https://github.com/rswag/rswag/tree/2.3.0)|3.0.3|3.23.11|
2325
|[2.2.0](https://github.com/rswag/rswag/tree/2.2.0)|2.0|3.18.2|
2426
|[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
136138
path '/blogs/{id}' do
137139
138140
get 'Retrieves a blog' do
139-
tags 'Blogs'
141+
tags 'Blogs', 'Another Tag'
140142
produces 'application/json', 'application/xml'
141143
parameter name: :id, in: :path, type: :string
142144
@@ -363,8 +365,8 @@ you should use the folowing syntax, making sure there are no whitespaces at the
363365

364366
### Specifying/Testing API Security ###
365367

366-
Swagger allows for the specification of different security schemes and their applicability to operations in an API.
367-
To leverage this in rswag, you define the schemes globally in _swagger_helper.rb_ and then use the "security" attribute at the operation level to specify which schemes, if any, are applicable to that operation.
368+
Swagger allows for the specification of different security schemes and their applicability to operations in an API.
369+
To leverage this in rswag, you define the schemes globally in _swagger_helper.rb_ and then use the "security" attribute at the operation level to specify which schemes, if any, are applicable to that operation.
368370
Swagger supports :basic, :bearer, :apiKey and :oauth2 and :openIdConnect scheme types. See [the spec](https://swagger.io/docs/specification/authentication/) for more info, as this underwent major changes between Swagger 2.0 and Open API 3.0
369371

370372
```ruby
@@ -416,7 +418,7 @@ describe 'Blogs API' do
416418
end
417419

418420
# example of documenting an endpoint that handles basic auth and api key based security
419-
describe 'Auth examples API' do
421+
describe 'Auth examples API' do
420422
path '/auth-tests/basic-and-api-key' do
421423
post 'Authenticates with basic auth and api key' do
422424
tags 'Auth Tests'
@@ -437,11 +439,11 @@ describe 'Auth examples API' do
437439
end
438440
end
439441
end
440-
442+
441443

442444
```
443445

444-
__NOTE:__ Depending on the scheme types, you'll be required to assign a corresponding parameter value with each example.
446+
__NOTE:__ Depending on the scheme types, you'll be required to assign a corresponding parameter value with each example.
445447
For example, :basic auth is required above and so the :Authorization (header) parameter must be set accordingly
446448

447449
## Configuration & Customization ##
@@ -479,9 +481,9 @@ rake rswag:specs:swaggerize PATTERN="spec/swagger/**/*_spec.rb"
479481

480482
### Referenced Parameters and Schema Definitions ###
481483

482-
Swagger allows you to describe JSON structures inline with your operation descriptions OR as referenced globals.
484+
Swagger allows you to describe JSON structures inline with your operation descriptions OR as referenced globals.
483485
For example, you might have a standard response structure for all failed operations.
484-
Again, this is a structure that changed since swagger 2.0. Notice the new "schemas" section for these.
486+
Again, this is a structure that changed since swagger 2.0. Notice the new "schemas" section for these.
485487
Rather than repeating the schema in every operation spec, you can define it globally and provide a reference to it in each spec:
486488

487489
```ruby
@@ -560,7 +562,7 @@ end
560562

561563
### Response headers ###
562564

563-
In Rswag, you could use `header` method inside the response block to specify header objects for this response.
565+
In Rswag, you could use `header` method inside the response block to specify header objects for this response.
564566
Rswag will validate your response headers with those header objects and inject them into the generated swagger file:
565567

566568
```ruby
@@ -608,19 +610,20 @@ To enable examples generation from responses add callback above run_test! like:
608610

609611
```
610612
after do |example|
611-
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
613+
example.metadata[:response][:content] = {
614+
'application/json' => {
615+
example: JSON.parse(response.body, symbolize_names: true)
616+
}
617+
}
612618
end
613619
```
614620

615-
You need to disable --dry-run option for Rspec > 3
621+
#### Dry Run Option ####
616622

617-
<!-- This is now enabled by default in rswag.
618-
You need to set the ``` config.swagger_dry_run = false``` value in the spec/spec_helper.rb file.
619-
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.
620-
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.
621-
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. -->
623+
The `--dry-run` option is enabled by default for Rspec 3, but if you need to
624+
disable it you can use the environment varible `SWAGGER_DRY_RUN=0` during the
625+
generation command or add the following to your `config/environments/test.rb`:
622626

623-
Add to config/environments/test.rb:
624627
```ruby
625628
RSpec.configure do |config|
626629
config.swagger_dry_run = false
@@ -656,8 +659,8 @@ describe 'Blogs API', document: false do
656659
```
657660

658661
##### rswag helper methods #####
659-
<!--
660-
There are some helper methods to help with documenting request bodies.
662+
<!--
663+
There are some helper methods to help with documenting request bodies.
661664
```ruby
662665
describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
663666
let(:api_key) { 'fake_key' }
@@ -693,7 +696,7 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
693696
end
694697
end
695698
end
696-
end
699+
end
697700
```
698701
699702
In the above example, we see methods ```request_body_json``` ```request_body_plain``` ```request_body_xml```.
@@ -703,7 +706,7 @@ and the examples: :blog which will create a named example "blog" under the "requ
703706
Again, documenting request response examples changed in Open API 3.0. The example above would generate a swagger.json snippet that looks like this:
704707
705708
```json
706-
...
709+
...
707710
{"requestBody": {
708711
"required": true,
709712
"content": {
@@ -737,9 +740,9 @@ Again, documenting request response examples changed in Open API 3.0. The exampl
737740
}
738741
```
739742
740-
*NOTE:* for this example request body to work in the tests properly, you need to ``let`` a variable named *blog*.
743+
*NOTE:* for this example request body to work in the tests properly, you need to ``let`` a variable named *blog*.
741744
The variable with the matching name (blog in this case) is eval-ed and captured to be placed in the examples section.
742-
This ```let``` value is used in the integration test to run the test AND captured and injected into the requestBody section.
745+
This ```let``` value is used in the integration test to run the test AND captured and injected into the requestBody section.
743746
744747
##### rswag response examples #####
745748
@@ -837,7 +840,7 @@ You can specify custom headers for serving your generated Swagger JSON. For exam
837840
```ruby
838841
Rswag::Api.configure do |c|
839842
...
840-
843+
841844
c.swagger_headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
842845
end
843846
```
@@ -909,5 +912,5 @@ docker pull swaggerapi/swagger-editor
909912
```
910913
docker run -d -p 80:8080 swaggerapi/swagger-editor
911914
```
912-
This will run the swagger editor in the docker daemon and can be accessed
915+
This will run the swagger editor in the docker daemon and can be accessed
913916
at ```http://localhost```. From here, you can use the UI to load the generated swagger.json to validate the output.

rswag-api/rswag-api.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Gem::Specification.new do |s|
99
s.authors = ['Richie Morris', 'Greg Myers', 'Jay Danielian']
1010
s.email = ['domaindrivendev@gmail.com']
1111
s.homepage = 'https://github.com/rswag/rswag'
12-
s.summary = 'A Rails Engine that exposes Swagger files as JSON endpoints'
13-
s.description = 'Open up your API to the phenomenal Swagger ecosystem by exposing Swagger files, that describe your service, as JSON endpoints'
12+
s.summary = 'A Rails Engine that exposes OpenAPI (formerly called Swagger) files as JSON endpoints'
13+
s.description = 'Open up your API to the phenomenal OpenAPI ecosystem by exposing OpenAPI files, that describe your service, as JSON endpoints. More about the OpenAPI initiative here: http://spec.openapis.org/'
1414
s.license = 'MIT'
1515

1616
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
1717

18-
s.add_dependency 'railties', '>= 3.1', '< 7.0'
18+
s.add_dependency 'railties', '>= 3.1', '< 7.1'
1919
end

rswag-specs/lib/generators/rspec/templates/spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
<% end -%>
2020
2121
after do |example|
22-
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
22+
example.metadata[:response][:content] = {
23+
'application/json' => {
24+
example: JSON.parse(response.body, symbolize_names: true)
25+
}
26+
}
2327
end
2428
run_test!
2529
end

rswag-specs/lib/rswag/specs/configuration.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def swagger_docs
2828
end
2929

3030
def swagger_dry_run
31-
@swagger_dry_run ||= begin
32-
@rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
31+
return @swagger_dry_run if defined? @swagger_dry_run
32+
if ENV.key?('SWAGGER_DRY_RUN')
33+
@rspec_config.swagger_dry_run = ENV['SWAGGER_DRY_RUN'] == '1'
3334
end
35+
@swagger_dry_run = @rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run
3436
end
3537

3638
def swagger_format

rswag-specs/lib/rswag/specs/example_group_helpers.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ def header(name, attributes)
8484
def examples(example = nil)
8585
return super() if example.nil?
8686

87-
metadata[:response][:examples] = example
87+
metadata[:response][:content] =
88+
example.each_with_object({}) do |(mime, example_object), memo|
89+
memo[mime] = { example: example_object }
90+
end
8891
end
8992

9093
def run_test!(&block)

0 commit comments

Comments
 (0)