Skip to content

Commit 4d1883f

Browse files
Vasiliydblock
authored andcommitted
Fix multiple version definitions for path versioning (#1414)
Closes #1400.
1 parent b9ba3d8 commit 4d1883f

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
1616
* [#1380](https://github.com/ruby-grape/grape/pull/1380): Fix `allow_blank: false` for `Time` attributes with valid values causes `NoMethodError` - [@ipkes](https://github.com/ipkes).
1717
* [#1384](https://github.com/ruby-grape/grape/pull/1384): Fix parameter validation with an empty optional nested `Array` - [@ipkes](https://github.com/ipkes).
18+
* [#1414](https://github.com/ruby-grape/grape/pull/1414): Fix multiple version definitions for path versioning - [@304](https://github.com/304).
1819

1920
0.16.2 (4/12/2016)
2021
==================

lib/grape/dsl/routing.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ def version(*args, &block)
3030
if args.any?
3131
options = args.extract_options!
3232
options = options.reverse_merge(using: :path)
33+
requested_versions = args.flatten
3334

3435
raise Grape::Exceptions::MissingVendorOption.new if options[:using] == :header && !options.key?(:vendor)
3536

36-
@versions = versions | args
37+
@versions = versions | requested_versions
3738

3839
if block_given?
3940
within_namespace do
40-
namespace_inheritable(:version, args)
41+
namespace_inheritable(:version, requested_versions)
4142
namespace_inheritable(:version_options, options)
4243

4344
instance_eval(&block)
4445
end
4546
else
46-
namespace_inheritable(:version, args)
47+
namespace_inheritable(:version, requested_versions)
4748
namespace_inheritable(:version_options, options)
4849
end
4950
end

spec/grape/api_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,27 @@ def subject.enable_root_route!
302302

303303
describe 'path versioned APIs' do
304304
before do
305-
subject.version 'v1', using: :path
305+
subject.version version, using: :path
306306
subject.enable_root_route!
307307
end
308308

309-
it 'without a format' do
310-
versioned_get '/', 'v1', using: :path
309+
context 'when a single version provided' do
310+
let(:version) { 'v1' }
311+
312+
it 'without a format' do
313+
versioned_get '/', 'v1', using: :path
314+
end
315+
316+
it 'with a format' do
317+
get '/v1/.json'
318+
end
311319
end
312320

313-
it 'with a format' do
314-
get '/v1/.json'
321+
context 'when array of versions provided' do
322+
let(:version) { %w(v1 v2) }
323+
324+
it { versioned_get '/', 'v1', using: :path }
325+
it { versioned_get '/', 'v2', using: :path }
315326
end
316327
end
317328

0 commit comments

Comments
 (0)