From 4249c61b491cd6c33230438d5b6a8e353134460e Mon Sep 17 00:00:00 2001 From: dblock Date: Fri, 28 Nov 2014 11:38:58 -0500 Subject: [PATCH] Support multi-tenanted APIs, don't cache base_path. --- .rubocop_todo.yml | 12 ++++++------ CHANGELOG.md | 2 +- lib/grape-swagger.rb | 3 +-- spec/non_default_api_spec.rb | 33 ++++++++++++++++++--------------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 74ab613e..09a263af 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,5 +1,5 @@ # This configuration was generated by `rubocop --auto-gen-config` -# on 2014-11-28 11:04:19 -0500 using RuboCop version 0.27.0. +# on 2014-11-28 11:38:25 -0500 using RuboCop version 0.27.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -7,18 +7,18 @@ # Offense count: 8 Metrics/AbcSize: - Max: 331 + Max: 327 # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 392 + Max: 394 # Offense count: 4 Metrics/CyclomaticComplexity: Max: 93 -# Offense count: 204 +# Offense count: 202 # Configuration parameters: AllowURI, URISchemes. Metrics/LineLength: Max: 254 @@ -26,11 +26,11 @@ Metrics/LineLength: # Offense count: 13 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 357 + Max: 359 # Offense count: 4 Metrics/PerceivedComplexity: - Max: 98 + Max: 96 # Offense count: 7 Style/ClassVars: diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f11fae0..04ee33d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ * [#176](https://github.com/tim-vandecasteele/grape-swagger/pull/176): Added ability to load nested models recursively - [@sergey-verevkin](https://github.com/sergey-verevkin). * [#179](https://github.com/tim-vandecasteele/grape-swagger/pull/179): Document `Virtus::Attribute::Boolean` as boolean - [@eashman](https://github.com/eashman), [@dblock](https://github.com/dblock). * [#178](https://github.com/tim-vandecasteele/grape-swagger/issues/178): Fixed `Hash` parameters, now exposed as Swagger `object` types - [@dblock](https://github.com/dblock). - +* [#167](https://github.com/tim-vandecasteele/grape-swagger/pull/167): Support mutli-tenanted APIs, don't cache `base_path` - [@bradrobertson](https://github.com/bradrobertson), (https://github.com/dblock). * Your contribution here. ### 0.8.0 (August 30, 2014) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index ee2eb099..07d57fcd 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -80,7 +80,6 @@ def self.setup(options) @@markdown = options[:markdown] ? GrapeSwagger::Markdown.new(options[:markdown]) : nil @@hide_format = options[:hide_format] api_version = options[:api_version] - base_path = options[:base_path] authorizations = options[:authorizations] root_base_path = options[:root_base_path] extra_info = options[:info] @@ -214,7 +213,7 @@ def self.setup(options) apis: apis } - base_path = parse_base_path(base_path, request) + base_path = parse_base_path(options[:base_path], request) api_description[:basePath] = base_path if base_path && base_path.size > 0 && root_base_path != false api_description[:models] = parse_entity_models(models) unless models.empty? api_description[:authorizations] = authorizations if authorizations diff --git a/spec/non_default_api_spec.rb b/spec/non_default_api_spec.rb index cdc9049c..d7429dcc 100644 --- a/spec/non_default_api_spec.rb +++ b/spec/non_default_api_spec.rb @@ -50,7 +50,7 @@ class ProcBasePathMountedApi < Grape::API class SimpleApiWithProcBasePath < Grape::API mount ProcBasePathMountedApi - add_swagger_documentation base_path: proc { |request| "#{request.base_url}/some_value" } + add_swagger_documentation base_path: proc { |request| [request.base_url, request.params[:base_path], 'some_value'].compact.join('/') } end end @@ -58,18 +58,26 @@ def app SimpleApiWithProcBasePath end - subject do - get '/swagger_doc/something.json' - JSON.parse(last_response.body) + context 'default' do + subject do + get '/swagger_doc/something.json' + JSON.parse(last_response.body) + end + + it 'retrieves the same given base-path for mounted-api' do + expect(subject['basePath']).to eq 'http://example.org/some_value' + end end - # it "retrieves the given base-path on /swagger_doc" do - # get '/swagger_doc.json' - # JSON.parse(last_response.body)["basePath"].should == "http://example.org/some_value" - # end + context 'param' do + subject do + get '/swagger_doc/something.json?base_path=foobar' + JSON.parse(last_response.body) + end - it 'retrieves the same given base-path for mounted-api' do - expect(subject['basePath']).to eq 'http://example.org/some_value' + it 're-evaluates base-path' do + expect(subject['basePath']).to eq 'http://example.org/foobar/some_value' + end end end @@ -97,11 +105,6 @@ def app JSON.parse(last_response.body) end - # it "retrieves the given base-path on /swagger_doc" do - # get '/swagger_doc.json' - # JSON.parse(last_response.body)["basePath"].should == "http://example.org/some_value" - # end - it 'retrieves the same given base-path for mounted-api' do get '/swagger_doc/something.json' expect(subject['basePath']).to eq 'http://example.org/some_value'