Skip to content

Commit 56cc8ad

Browse files
committed
Fix error climbing controller hierarchy
When using namespaced resources, climbing the controller hierarchy was broken in really specific circumstances. Bug introduced in Apipie#872 (cherry picked from commit 3929761)
1 parent 661fc15 commit 56cc8ad

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

lib/apipie/application.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def valid_search_args?(version, resource_id, method_name)
454454
end
455455

456456
def version_prefix(klass)
457-
version = controller_versions(klass.to_s).first
457+
version = controller_versions(klass).first
458458
base_url = get_base_url(version)
459459
return "/" if base_url.blank?
460460
base_url[1..-1] + "/"

lib/apipie/resource_description.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def doc_url
101101
Apipie.full_url crumbs.join('/')
102102
end
103103

104-
def api_url; "#{Apipie.api_base_url(_version)}#{@_path}"; end
104+
def api_url
105+
"#{Apipie.api_base_url(_version)}#{@_path}"
106+
end
105107

106108
def valid_method_name?(method_name)
107109
@_methods.keys.map(&:to_s).include?(method_name.to_s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe Api::V2::EmptyMiddleController do
4+
let(:resource_description) { Apipie.get_resource_description(described_class, '2.0') }
5+
6+
describe 'resource description' do
7+
subject { resource_description }
8+
9+
context 'when namespaced resources are enabled' do
10+
before { Apipie.configuration.namespaced_resources = true }
11+
after { Apipie.configuration.namespaced_resources = false }
12+
13+
# we don't actually expect the resource description to be nil, but resource IDs
14+
# are computed at file load time, and altering the value of namespaced_resources
15+
# after the fact doesn't change the resource ID, so it can't be found
16+
it { is_expected.to be_nil }
17+
end
18+
19+
context 'when namespaced resources are disabled' do
20+
it { is_expected.to be_nil }
21+
end
22+
end
23+
end

spec/dummy/app/controllers/api/v2/empty_middle_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ class EmptyMiddleController < V2::BaseController
44
# This is an empty controller, used to test cases where controllers
55
# may inherit from a middle controler that does not define a resource_description,
66
# but the middle controller's parent does.
7+
8+
def inconsequential_method
9+
# This method is here to ensure that the controller is not empty.
10+
# It triggers method_added, which is used to add the resource description.
11+
end
712
end
813
end
914
end

0 commit comments

Comments
 (0)