Skip to content

Fix multiple version definitions for path versioning #1414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
* [#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).
* [#1384](https://github.com/ruby-grape/grape/pull/1384): Fix parameter validation with an empty optional nested `Array` - [@ipkes](https://github.com/ipkes).
* [#1414](https://github.com/ruby-grape/grape/pull/1414): Fix multiple version definitions for path versioning - [@304](https://github.com/304).

0.16.2 (4/12/2016)
==================
Expand Down
7 changes: 4 additions & 3 deletions lib/grape/dsl/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ def version(*args, &block)
if args.any?
options = args.extract_options!
options = options.reverse_merge(using: :path)
requested_versions = args.flatten

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

@versions = versions | args
@versions = versions | requested_versions

if block_given?
within_namespace do
namespace_inheritable(:version, args)
namespace_inheritable(:version, requested_versions)
namespace_inheritable(:version_options, options)

instance_eval(&block)
end
else
namespace_inheritable(:version, args)
namespace_inheritable(:version, requested_versions)
namespace_inheritable(:version_options, options)
end
end
Expand Down
21 changes: 16 additions & 5 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,27 @@ def subject.enable_root_route!

describe 'path versioned APIs' do
before do
subject.version 'v1', using: :path
subject.version version, using: :path
subject.enable_root_route!
end

it 'without a format' do
versioned_get '/', 'v1', using: :path
context 'when a single version provided' do
let(:version) { 'v1' }

it 'without a format' do
versioned_get '/', 'v1', using: :path
end

it 'with a format' do
get '/v1/.json'
end
end

it 'with a format' do
get '/v1/.json'
context 'when array of versions provided' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above should be in its own contect context since for example let(:version) isn't used here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done => b0b9586

let(:version) { %w(v1 v2) }

it { versioned_get '/', 'v1', using: :path }
it { versioned_get '/', 'v2', using: :path }
end
end

Expand Down