Skip to content

Commit 393d362

Browse files
committed
Make versioner consider the mount destination path
1 parent 081d09b commit 393d362

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

.rubocop_todo.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2017-01-17 06:04:25 -0500 using RuboCop version 0.47.0.
3+
# on 2017-02-02 01:40:01 +0900 using RuboCop version 0.47.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -13,7 +13,7 @@ Metrics/AbcSize:
1313
# Offense count: 266
1414
# Configuration parameters: CountComments, ExcludedMethods.
1515
Metrics/BlockLength:
16-
Max: 3084
16+
Max: 3104
1717

1818
# Offense count: 1
1919
# Configuration parameters: CountBlocks.
@@ -23,19 +23,19 @@ Metrics/BlockNesting:
2323
# Offense count: 8
2424
# Configuration parameters: CountComments.
2525
Metrics/ClassLength:
26-
Max: 279
26+
Max: 280
2727

28-
# Offense count: 26
28+
# Offense count: 27
2929
Metrics/CyclomaticComplexity:
3030
Max: 14
3131

32-
# Offense count: 975
32+
# Offense count: 983
3333
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
3434
# URISchemes: http, https
3535
Metrics/LineLength:
3636
Max: 215
3737

38-
# Offense count: 54
38+
# Offense count: 55
3939
# Configuration parameters: CountComments.
4040
Metrics/MethodLength:
4141
Max: 33
@@ -45,7 +45,7 @@ Metrics/MethodLength:
4545
Metrics/ModuleLength:
4646
Max: 212
4747

48-
# Offense count: 17
48+
# Offense count: 18
4949
Metrics/PerceivedComplexity:
5050
Max: 14
5151

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).first
299300
end
300301

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

lib/grape/middleware/versioner/path.rb

+11
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 mounted_path?(path)
2829

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

4142
private
4243

44+
def mounted_path?(path)
45+
return false unless mount_path && path.start_with?(mount_path)
46+
rest = path.slice(mount_path.length..-1)
47+
rest.start_with?('/') || rest.empty?
48+
end
49+
50+
def mount_path
51+
@mount_path ||= options[:mount_path] && options[:mount_path] != '/' ? options[:mount_path] : ''
52+
end
53+
4354
def prefix
4455
Grape::Router.normalize_path(options[:prefix].to_s) if options[:prefix]
4556
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)