Skip to content

Commit

Permalink
Make versioner consider the mount destination path
Browse files Browse the repository at this point in the history
  • Loading branch information
namusyaka committed Feb 1, 2017
1 parent 081d09b commit 58e25f4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### 0.19.2 (Next)

#### Fixes

* [#1570](https://github.com/ruby-grape/grape/pull/1570): Make versioner consider the mount destination path - [@namusyaka](https://github.com/namusyaka).
* Your contribution here.

#### Features

* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ def build_stack(helpers)
stack.use Grape::Middleware::Versioner.using(namespace_inheritable(:version_options)[:using]),
versions: namespace_inheritable(:version) ? namespace_inheritable(:version).flatten : nil,
version_options: namespace_inheritable(:version_options),
prefix: namespace_inheritable(:root_prefix)
prefix: namespace_inheritable(:root_prefix),
mount_path: namespace_stackable(:mount_path)[0]
end

stack.use Grape::Middleware::Formatter,
Expand Down
5 changes: 5 additions & 0 deletions lib/grape/middleware/versioner/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def default_options

def before
path = env[Grape::Http::Headers::PATH_INFO].dup
path.sub!(mount_path, '') if mount_path && path.start_with?(mount_path)

if prefix && path.index(prefix) == 0 # rubocop:disable all
path.sub!(prefix, '')
Expand All @@ -40,6 +41,10 @@ def before

private

def mount_path
@mount_path ||= (options[:mount_path] && options[:mount_path] != '/') ? options[:mount_path] : ''
end

def prefix
Grape::Router.normalize_path(options[:prefix].to_s) if options[:prefix]
end
Expand Down
27 changes: 27 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,33 @@ def static
versioned_get '/users/hello', 'two', using: :header, vendor: 'test'
expect(last_response.body).to eq('two')
end

it 'recognizes potential versions with mounted path' do
a = Class.new(Grape::API) do
version :v1, using: :path

get '/hello' do
'hello'
end
end

b = Class.new(Grape::API) do
version :v1, using: :path

get '/world' do
'world'
end
end

subject.mount a => '/one'
subject.mount b => '/two'

get '/one/v1/hello'
expect(last_response.status).to eq 200

get '/two/v1/world'
expect(last_response.status).to eq 200
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/grape/middleware/versioner/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@
expect(subject.call('PATH_INFO' => '/v3/foo').last).to eq('v3')
end
end

context 'with mount path' do
let(:options) { { mount_path: '/mounted', versions: [:v1] } }
it 'recognizes potential version' do
expect(subject.call('PATH_INFO' => '/mounted/v1/foo').last).to eq('v1')
end
end
end

0 comments on commit 58e25f4

Please sign in to comment.