From aed3b513ae56cb8578c458e17be64a59cbc1c77e Mon Sep 17 00:00:00 2001 From: dm1try Date: Thu, 7 Nov 2013 12:05:53 +0300 Subject: [PATCH] if base_path is relative make it absolute using request.base_uri --- lib/grape-swagger.rb | 8 +++++++- spec/non_default_api_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 97c6a0ca..60703e0b 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -219,7 +219,13 @@ def strip_heredoc(string) end def parse_base_path(base_path, request) - (base_path.is_a?(Proc) ? base_path.call(request) : base_path) || request.base_url + if base_path.is_a?(Proc) + base_path.call(request) + elsif base_path.is_a?(String) + URI(base_path).relative? ? URI.join(request.base_url, base_path).to_s : base_path + else + request.base_url + end end end end diff --git a/spec/non_default_api_spec.rb b/spec/non_default_api_spec.rb index 4276d4d0..455c1ab7 100644 --- a/spec/non_default_api_spec.rb +++ b/spec/non_default_api_spec.rb @@ -62,6 +62,35 @@ def app; SimpleApiWithProcBasePath end end end + context "relative base_path" do + before :all do + + class RelativeBasePathMountedApi < Grape::API + desc 'This gets something.' + get '/something' do + { bla: 'something' } + end + end + + class SimpleApiWithRelativeBasePath < Grape::API + mount RelativeBasePathMountedApi + add_swagger_documentation base_path: "/some_value" + end + end + + def app; SimpleApiWithRelativeBasePath 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' + JSON.parse(last_response.body)["basePath"].should == "http://example.org/some_value" + end + end + context "overriding the version" do before :all do