Skip to content

Commit 58e25f4

Browse files
committed
Make versioner consider the mount destination path
1 parent 081d09b commit 58e25f4

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### 0.19.2 (Next)
22

3+
#### Fixes
4+
5+
* [#1570](https://github.com/ruby-grape/grape/pull/1570): Make versioner consider the mount destination path - [@namusyaka](https://github.com/namusyaka).
6+
* Your contribution here.
7+
38
#### Features
49

510
* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).

lib/grape/endpoint.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ def build_stack(helpers)
295295
stack.use Grape::Middleware::Versioner.using(namespace_inheritable(:version_options)[:using]),
296296
versions: namespace_inheritable(:version) ? namespace_inheritable(:version).flatten : nil,
297297
version_options: namespace_inheritable(:version_options),
298-
prefix: namespace_inheritable(:root_prefix)
298+
prefix: namespace_inheritable(:root_prefix),
299+
mount_path: namespace_stackable(:mount_path)[0]
299300
end
300301

301302
stack.use Grape::Middleware::Formatter,

lib/grape/middleware/versioner/path.rb

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def default_options
2525

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

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

4142
private
4243

44+
def mount_path
45+
@mount_path ||= (options[:mount_path] && options[:mount_path] != '/') ? options[:mount_path] : ''
46+
end
47+
4348
def prefix
4449
Grape::Router.normalize_path(options[:prefix].to_s) if options[:prefix]
4550
end

spec/grape/api_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,33 @@ def static
30903090
versioned_get '/users/hello', 'two', using: :header, vendor: 'test'
30913091
expect(last_response.body).to eq('two')
30923092
end
3093+
3094+
it 'recognizes potential versions with mounted path' do
3095+
a = Class.new(Grape::API) do
3096+
version :v1, using: :path
3097+
3098+
get '/hello' do
3099+
'hello'
3100+
end
3101+
end
3102+
3103+
b = Class.new(Grape::API) do
3104+
version :v1, using: :path
3105+
3106+
get '/world' do
3107+
'world'
3108+
end
3109+
end
3110+
3111+
subject.mount a => '/one'
3112+
subject.mount b => '/two'
3113+
3114+
get '/one/v1/hello'
3115+
expect(last_response.status).to eq 200
3116+
3117+
get '/two/v1/world'
3118+
expect(last_response.status).to eq 200
3119+
end
30933120
end
30943121
end
30953122

spec/grape/middleware/versioner/path_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@
4848
expect(subject.call('PATH_INFO' => '/v3/foo').last).to eq('v3')
4949
end
5050
end
51+
52+
context 'with mount path' do
53+
let(:options) { { mount_path: '/mounted', versions: [:v1] } }
54+
it 'recognizes potential version' do
55+
expect(subject.call('PATH_INFO' => '/mounted/v1/foo').last).to eq('v1')
56+
end
57+
end
5158
end

0 commit comments

Comments
 (0)