Merge branch 'master' into openapi/merge

This commit is contained in:
Greg Myers 2020-04-04 20:05:00 +01:00 committed by GitHub
commit 74ea37e048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 91 additions and 16 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
### Changed ### Changed
- Update swagger-ui version to 3.23.11 [#239](https://github.com/rswag/rswag/pull/239)
### Deprecated ### Deprecated
### Removed ### Removed
### Fixed ### Fixed

View File

@ -55,6 +55,19 @@ Push to your fork and [submit a Pull Request][pr].
[pr]: https://github.com/rswag/rswag/compare/ [pr]: https://github.com/rswag/rswag/compare/
## Updating Swagger UI
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
```
Navigate to the rswag-ui folder and run npm install to update the package-lock.json
## Release ## Release
(for maintainers) (for maintainers)

View File

@ -27,9 +27,10 @@ gem 'rswag-ui', path: './rswag-ui'
group :test do group :test do
gem 'capybara' gem 'capybara'
gem 'capybara-webkit' gem 'geckodriver-helper'
gem 'generator_spec' gem 'generator_spec'
gem 'rspec-rails' gem 'rspec-rails'
gem 'selenium-webdriver'
gem 'rswag-specs', path: './rswag-specs' gem 'rswag-specs', path: './rswag-specs'
gem 'test-unit' gem 'test-unit'
end end

View File

@ -768,6 +768,21 @@ end
Note how the filter is passed the rack env for the current request. This provides a lot of flexibilty. For example, you can assign the "host" property (as shown) or you could inspect session information or an Authorization header and remove operations based on user permissions. Note how the filter is passed the rack env for the current request. This provides a lot of flexibilty. For example, you can assign the "host" property (as shown) or you could inspect session information or an Authorization header and remove operations based on user permissions.
### Custom Headers for Swagger Files ###
You can specify custom headers for serving your generated Swagger JSON. For example you may want to force a specific charset for the 'Content-Type' header. You can configure a hash of headers to be sent with the request:
```ruby
Rswag::Api.configure do |c|
...
c.swagger_headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
end
```
Take care when overriding Content-Type if you serve both YAML and JSON files as it will no longer switch the Content-Type header correctly.
### Enable Swagger Endpoints for swagger-ui ### ### Enable Swagger Endpoints for swagger-ui ###
You can update the _rswag-ui.rb_ initializer, installed with rswag-ui, to specify which Swagger endpoints should be available to power the documentation UI. If you're using rswag-api, these should correspond to the Swagger endpoints it exposes. When the UI is rendered, you'll see these listed in a drop-down to the top right of the page: You can update the _rswag-ui.rb_ initializer, installed with rswag-ui, to specify which Swagger endpoints should be available to power the documentation UI. If you're using rswag-api, these should correspond to the Swagger endpoints it exposes. When the UI is rendered, you'll see these listed in a drop-down to the top right of the page:

View File

@ -1,7 +1,7 @@
module Rswag module Rswag
module Api module Api
class Configuration class Configuration
attr_accessor :swagger_root, :swagger_filter attr_accessor :swagger_root, :swagger_filter, :swagger_headers
def resolve_swagger_root(env) def resolve_swagger_root(env)
path_params = env['action_dispatch.request.path_parameters'] || {} path_params = env['action_dispatch.request.path_parameters'] || {}

View File

@ -19,11 +19,12 @@ module Rswag
swagger = parse_file(filename) swagger = parse_file(filename)
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil? @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain') mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
headers = { 'Content-Type' => mime }.merge(@config.swagger_headers || {})
body = unload_swagger(filename, swagger) body = unload_swagger(filename, swagger)
return [ return [
'200', '200',
{ 'Content-Type' => mime }, headers,
[ body ] [ body ]
] ]
end end

View File

@ -37,6 +37,42 @@ module Rswag
end end
end end
context 'when swagger_headers is configured' do
let(:env) { env_defaults.merge('PATH_INFO' => 'v1/swagger.json') }
context 'replacing the default content type header' do
before do
config.swagger_headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
end
it 'returns a 200 status' do
expect(response.length).to eql(3)
expect(response.first).to eql('200')
end
it 'applies the headers to the response' do
expect(response[1]).to include( 'Content-Type' => 'application/json; charset=UTF-8')
end
end
context 'adding an additional header' do
before do
config.swagger_headers = { 'Access-Control-Allow-Origin' => '*' }
end
it 'returns a 200 status' do
expect(response.length).to eql(3)
expect(response.first).to eql('200')
end
it 'applies the headers to the response' do
expect(response[1]).to include( 'Access-Control-Allow-Origin' => '*')
end
it 'keeps the default header' do
expect(response[1]).to include( 'Content-Type' => 'application/json')
end
end
end
context "given a path that doesn't map to any swagger file" do context "given a path that doesn't map to any swagger file" do
let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') } let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') }
before do before do

View File

@ -5,9 +5,9 @@
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"swagger-ui-dist": { "swagger-ui-dist": {
"version": "3.18.2", "version": "3.23.11",
"resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.18.2.tgz", "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.23.11.tgz",
"integrity": "sha512-pWAEiKkgWUJvjmLW9AojudnutJ+NTn5g6OdNLj1iIJWwCkoy40K3Upwa24DqFbmIE4vLX4XplND61hp2L+s5vg==" "integrity": "sha512-ipENHHH/sqpngTpHXUwg55eAOZ7b2UVayUwwuWPA6nQSPhjBVXX4zOPpNKUwQIFOl3oIwVvZF7mqoxH7pMgVzA=="
} }
} }
} }

View File

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

View File

@ -2,11 +2,11 @@
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
# #
# Note that this schema.rb definition is the authoritative source for your # This file is the source Rails uses to define your schema when running `rails
# database schema. If you need to create the application database on another # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# system, you should be using db:schema:load, not running all the migrations # be faster and is potentially less error prone than running all of your
# from scratch. The latter is a flawed and unsustainable approach (the more migrations # migrations from scratch. Old migrations may fail to apply correctly if those
# you'll amass, the slower it'll run and the greater likelihood for issues). # migrations use external dependencies or application code.
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.

View File

@ -5,8 +5,8 @@ RSpec.feature 'swagger-ui', js: true do
scenario 'browsing api-docs' do scenario 'browsing api-docs' do
visit '/api-docs' visit '/api-docs'
expect(page).to have_content('GET /blogs Searches blogs') expect(page).to have_content('GET /blogs Searches blogs', normalize_ws: true)
expect(page).to have_content('POST /blogs Creates a blog') expect(page).to have_content('POST /blogs Creates a blog', normalize_ws: true)
expect(page).to have_content('GET /blogs/{id} Retrieves a blog') expect(page).to have_content('GET /blogs/{id} Retrieves a blog', normalize_ws: true)
end end
end end

View File

@ -53,7 +53,15 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via: # arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name") # config.filter_gems_from_backtrace("gem name")
Capybara.javascript_driver = :webkit Capybara.register_driver :firefox_headless do |app|
options = ::Selenium::WebDriver::Firefox::Options.new
options.args << '--headless'
Capybara::Selenium::Driver.new(app, browser: :firefox, options: options)
end
Capybara.javascript_driver = :firefox_headless
end end
Capybara::Webkit.configure(&:block_unknown_urls) Capybara::Webkit.configure(&:block_unknown_urls)