From d00bb06e199f88583d17d1b2bcf1c5f12c38ea08 Mon Sep 17 00:00:00 2001 From: PJ Davis Date: Wed, 30 Jan 2019 13:46:13 -0500 Subject: [PATCH 01/10] allow headers to be set in the configuration of rswag-api --- README.md | 17 ++++++++-- rswag-api/lib/rswag/api/configuration.rb | 2 +- rswag-api/lib/rswag/api/middleware.rb | 7 ++-- rswag-api/spec/rswag/api/middleware_spec.rb | 36 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 08f0157..be84c11 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ Once you have an API that can describe itself in Swagger, you've opened the trea ```ruby rails g rswag:install ``` - + Or run the install generators for each package separately if you installed Rswag as separate gems, as indicated above: - + ```ruby rails g rswag:api:install rswag:ui:install RAILS_ENV=test rails g rswag:specs:install @@ -466,6 +466,19 @@ 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 Authoriation 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 +``` + + ### 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: diff --git a/rswag-api/lib/rswag/api/configuration.rb b/rswag-api/lib/rswag/api/configuration.rb index ff56180..bf64229 100644 --- a/rswag-api/lib/rswag/api/configuration.rb +++ b/rswag-api/lib/rswag/api/configuration.rb @@ -1,7 +1,7 @@ module Rswag module Api class Configuration - attr_accessor :swagger_root, :swagger_filter + attr_accessor :swagger_root, :swagger_filter, :swagger_headers def resolve_swagger_root(env) path_params = env['action_dispatch.request.path_parameters'] || {} diff --git a/rswag-api/lib/rswag/api/middleware.rb b/rswag-api/lib/rswag/api/middleware.rb index 118c987..658988d 100644 --- a/rswag-api/lib/rswag/api/middleware.rb +++ b/rswag-api/lib/rswag/api/middleware.rb @@ -2,7 +2,7 @@ require 'json' module Rswag module Api - class Middleware + class Middleware def initialize(app, config) @app = app @@ -16,14 +16,15 @@ module Rswag if env['REQUEST_METHOD'] == 'GET' && File.file?(filename) swagger = load_json(filename) @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil? + headers = { 'Content-Type' => 'application/json' }.merge(@config.swagger_headers || {}) return [ '200', - { 'Content-Type' => 'application/json' }, + headers, [ JSON.dump(swagger) ] ] end - + return @app.call(env) end diff --git a/rswag-api/spec/rswag/api/middleware_spec.rb b/rswag-api/spec/rswag/api/middleware_spec.rb index aaa148b..6a24e2c 100644 --- a/rswag-api/spec/rswag/api/middleware_spec.rb +++ b/rswag-api/spec/rswag/api/middleware_spec.rb @@ -37,6 +37,42 @@ module Rswag 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 let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') } before do From bff44ee06e7ddd94fb6ab1510af7fbeb0d1f3b24 Mon Sep 17 00:00:00 2001 From: Hayley Date: Mon, 7 Oct 2019 16:55:20 +0100 Subject: [PATCH 02/10] update swagger-ui to 3.23.11 --- README.md | 2 +- package-lock.json | 3 +++ rswag-ui/package.json | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 package-lock.json diff --git a/README.md b/README.md index 267b563..d3cfe2f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,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)|2.0|3.17.3| +|[master](https://github.com/rswag/rswag/tree/master)|2.0|3.23.11| |[2.0.6](https://github.com/rswag/rswag/tree/2.0.6)|2.0|3.17.3| |[1.6.0](https://github.com/rswag/rswag/tree/1.6.0)|2.0|2.2.5| diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/rswag-ui/package.json b/rswag-ui/package.json index 1fce627..fd299d1 100644 --- a/rswag-ui/package.json +++ b/rswag-ui/package.json @@ -3,6 +3,6 @@ "version": "1.0.0", "private": true, "dependencies": { - "swagger-ui-dist": "3.17.3" + "swagger-ui-dist": "3.23.11" } } From 90df5bfa30cf5a18d3740e078a594b9a2b799490 Mon Sep 17 00:00:00 2001 From: Hayley Date: Mon, 7 Oct 2019 17:11:16 +0100 Subject: [PATCH 03/10] update package-lock --- package-lock.json | 3 --- rswag-ui/package-lock.json | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) delete mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 48e341a..0000000 --- a/package-lock.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "lockfileVersion": 1 -} diff --git a/rswag-ui/package-lock.json b/rswag-ui/package-lock.json index 664d3fa..8bc72e8 100644 --- a/rswag-ui/package-lock.json +++ b/rswag-ui/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "swagger-ui-dist": { - "version": "3.17.3", - "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.17.3.tgz", - "integrity": "sha1-37lkCMzEZ3UVX3NpGQxdSyAW/lw=" + "version": "3.23.11", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.23.11.tgz", + "integrity": "sha512-ipENHHH/sqpngTpHXUwg55eAOZ7b2UVayUwwuWPA6nQSPhjBVXX4zOPpNKUwQIFOl3oIwVvZF7mqoxH7pMgVzA==" } } } From 2c3c23e46089d97506c6b7f3b9007abe8dc6f57c Mon Sep 17 00:00:00 2001 From: Hayley Date: Mon, 7 Oct 2019 17:23:25 +0100 Subject: [PATCH 04/10] update changelog.md and contributing.md --- CHANGELOG.md | 1 + CONTRIBUTING.md | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8b599..450d1a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added ### Changed +- Update swagger-ui version to 3.23.11 [#239](https://github.com/rswag/rswag/pull/239) ### Deprecated ### Removed ### Fixed diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c22b355..a1b9b5b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -44,6 +44,19 @@ Push to your fork and [submit a Pull Request][pr]. [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 (for maintainers) From ac1d61b08f886824659757e1c39e4730b9bf6180 Mon Sep 17 00:00:00 2001 From: Greg Myers Date: Tue, 8 Oct 2019 15:57:16 +0100 Subject: [PATCH 05/10] Replace webdriver with firefox-headless --- Gemfile | 3 ++- test-app/db/schema.rb | 12 ++++++------ test-app/spec/rails_helper.rb | 11 +++++++---- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index c9d5c4e..b9f1fbe 100644 --- a/Gemfile +++ b/Gemfile @@ -28,7 +28,8 @@ group :test do gem 'rspec-rails' gem 'generator_spec' gem 'capybara' - gem 'capybara-webkit' + gem 'geckodriver-helper' + gem 'selenium-webdriver' gem 'rswag-specs', path: './rswag-specs' end diff --git a/test-app/db/schema.rb b/test-app/db/schema.rb index 8accba1..e01f8f3 100644 --- a/test-app/db/schema.rb +++ b/test-app/db/schema.rb @@ -2,15 +2,15 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160218212104) do +ActiveRecord::Schema.define(version: 2016_02_18_212104) do create_table "blogs", force: :cascade do |t| t.string "title" diff --git a/test-app/spec/rails_helper.rb b/test-app/spec/rails_helper.rb index 98c8fc3..3d9bd5f 100644 --- a/test-app/spec/rails_helper.rb +++ b/test-app/spec/rails_helper.rb @@ -51,9 +51,12 @@ RSpec.configure do |config| # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") - Capybara.javascript_driver = :webkit -end + Capybara.register_driver :firefox_headless do |app| + options = ::Selenium::WebDriver::Firefox::Options.new + options.args << '--headless' -Capybara::Webkit.configure do |config| - config.block_unknown_urls + Capybara::Selenium::Driver.new(app, browser: :firefox, options: options) + end + + Capybara.javascript_driver = :firefox_headless end From d01dff199cfa356d5d082409d102c8ba0f863c45 Mon Sep 17 00:00:00 2001 From: Hayley Date: Tue, 8 Oct 2019 16:43:11 +0100 Subject: [PATCH 06/10] potential test fix --- test-app/spec/features/swagger_ui_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-app/spec/features/swagger_ui_spec.rb b/test-app/spec/features/swagger_ui_spec.rb index 7faa14e..5b90f88 100644 --- a/test-app/spec/features/swagger_ui_spec.rb +++ b/test-app/spec/features/swagger_ui_spec.rb @@ -5,8 +5,8 @@ feature 'swagger-ui', js: true do scenario 'browsing api-docs' do visit '/api-docs' - expect(page).to have_content('GET /blogs Searches blogs') - expect(page).to have_content('POST /blogs Creates a blog') - expect(page).to have_content('GET /blogs/{id} Retrieves a blog') + expect(page).to have_content('GET /blogs Searches blogs', normalize_ws: true) + expect(page).to have_content('POST /blogs Creates a blog', normalize_ws: true) + expect(page).to have_content('GET /blogs/{id} Retrieves a blog', normalize_ws: true) end end From b29373c0c465b59389adb36506275e7de16c8feb Mon Sep 17 00:00:00 2001 From: hdpuk86 Date: Thu, 24 Oct 2019 21:53:48 +0100 Subject: [PATCH 07/10] update swagger-ui to 3.23.11 --- rswag-ui/package-lock.json | 6 ------ rswag-ui/package.json | 4 ---- 2 files changed, 10 deletions(-) diff --git a/rswag-ui/package-lock.json b/rswag-ui/package-lock.json index f1458f1..8bc72e8 100644 --- a/rswag-ui/package-lock.json +++ b/rswag-ui/package-lock.json @@ -5,15 +5,9 @@ "requires": true, "dependencies": { "swagger-ui-dist": { -<<<<<<< HEAD "version": "3.23.11", "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.23.11.tgz", "integrity": "sha512-ipENHHH/sqpngTpHXUwg55eAOZ7b2UVayUwwuWPA6nQSPhjBVXX4zOPpNKUwQIFOl3oIwVvZF7mqoxH7pMgVzA==" -======= - "version": "3.18.2", - "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.18.2.tgz", - "integrity": "sha512-pWAEiKkgWUJvjmLW9AojudnutJ+NTn5g6OdNLj1iIJWwCkoy40K3Upwa24DqFbmIE4vLX4XplND61hp2L+s5vg==" ->>>>>>> origin/master } } } diff --git a/rswag-ui/package.json b/rswag-ui/package.json index 9f600c5..fd299d1 100644 --- a/rswag-ui/package.json +++ b/rswag-ui/package.json @@ -3,10 +3,6 @@ "version": "1.0.0", "private": true, "dependencies": { -<<<<<<< HEAD "swagger-ui-dist": "3.23.11" -======= - "swagger-ui-dist": "3.18.2" ->>>>>>> origin/master } } From ef78b0c98f04522a144c80d2ded503fdd2e6d3ce Mon Sep 17 00:00:00 2001 From: Hayley Prior <31411337+hdpuk86@users.noreply.github.com> Date: Thu, 24 Oct 2019 22:06:00 +0100 Subject: [PATCH 08/10] Delete .yarn-integrity --- node_modules/.yarn-integrity | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 node_modules/.yarn-integrity diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity deleted file mode 100644 index 5e070b6..0000000 --- a/node_modules/.yarn-integrity +++ /dev/null @@ -1,10 +0,0 @@ -{ - "systemParams": "darwin-x64-72", - "modulesFolders": [], - "flags": [], - "linkedModules": [], - "topLevelPatterns": [], - "lockfileEntries": {}, - "files": [], - "artifacts": {} -} \ No newline at end of file From 9cbd85a22a87680353fa75d8c3e7847ccfe456d3 Mon Sep 17 00:00:00 2001 From: Hayley Prior <31411337+hdpuk86@users.noreply.github.com> Date: Thu, 24 Oct 2019 22:06:26 +0100 Subject: [PATCH 09/10] Delete yarn.lock --- yarn.lock | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 yarn.lock diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index fb57ccd..0000000 --- a/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - From 1996f5130fc8fbfc5702bf1bceb33e6d1b66bdf4 Mon Sep 17 00:00:00 2001 From: Greg Myers Date: Sat, 4 Apr 2020 20:00:39 +0100 Subject: [PATCH 10/10] doc: note that content-type is dynamic --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bc6b121..317eaeb 100644 --- a/README.md +++ b/README.md @@ -546,6 +546,8 @@ Rswag::Api.configure do |c| 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 ###