Skip to content
New issue

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

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

Already on GitHub? # to your account

Fixes issue by checking if there's a base is there at all #1946

Merged
merged 3 commits into from
Dec 17, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#### Fixes

* Your contribution here.
* [#1931](https://github.com/ruby-grape/grape/pull/1946): Fixes issue when using namespaces in `Grape::API::Instance` mounted directly - [@myxoh](https://github.com/myxoh).

### 1.2.5 (2019/12/01)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def evaluate_as_instance_with_configuration(block, lazy: false)
self.configuration = value_for_configuration
response
end
if base_instance? && lazy
if base && base_instance? && lazy
Copy link
Member

Choose a reason for hiding this comment

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

Should we be doing this check inside base_instance?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it makes sense, as base_instance? usually means: "This instance is not used anywhere and is just a template for other instances" while, if an API has no base, it will most likely be used directly

lazy_block
else
lazy_block.evaluate_from(configuration)
Expand Down
48 changes: 48 additions & 0 deletions spec/grape/api/instance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'spec_helper'
require 'shared/versioning_examples'

describe Grape::API::Instance do
subject(:an_instance) do
Class.new(Grape::API::Instance) do
namespace :some_namespace do
get 'some_endpoint' do
'success'
end
end
end
end

let(:root_api) do
to_mount = an_instance
Class.new(Grape::API) do
mount to_mount
end
end

def app
root_api
end

context 'when an instance is mounted on the root' do
it 'can call the instance endpoint' do
get '/some_namespace/some_endpoint'
expect(last_response.body).to eq 'success'
end
end

context 'when an instance is the root' do
let(:root_api) do
to_mount = an_instance
Class.new(Grape::API::Instance) do
mount to_mount
end
end

it 'can call the instance endpoint' do
get '/some_namespace/some_endpoint'
expect(last_response.body).to eq 'success'
end
end
end