Skip to content

Commit 83b767c

Browse files
committed
Using strings or symbols for middleware class names is deprecated.
Convert things like this: middleware.use "Foo::Bar" to this: middleware.use Foo::Bar
1 parent 435b224 commit 83b767c

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

actionpack/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Using strings or symbols for middleware class names is deprecated. Convert
2+
things like this:
3+
4+
middleware.use "Foo::Bar"
5+
6+
to this:
7+
8+
middleware.use Foo::Bar
9+
110
* ActionController::TestSession now accepts a default value as well as
211
a block for generating a default value based off the key provided.
312

actionpack/lib/action_controller/metal.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def build_middleware(klass, args, block)
5353
list = except
5454
end
5555

56-
57-
Middleware.new(klass, args, list, strategy, block)
56+
Middleware.new(get_class(klass), args, list, strategy, block)
5857
end
5958
end
6059

actionpack/lib/action_dispatch/middleware/stack.rb

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@
44
module ActionDispatch
55
class MiddlewareStack
66
class Middleware
7-
attr_reader :args, :block, :name, :classcache
7+
attr_reader :args, :block, :klass
88

9-
def initialize(klass_or_name, args, block)
10-
@klass = nil
11-
12-
if klass_or_name.respond_to?(:name)
13-
@klass = klass_or_name
14-
@name = @klass.name
15-
else
16-
@name = klass_or_name.to_s
17-
end
18-
19-
@classcache = ActiveSupport::Dependencies::Reference
20-
@args, @block = args, block
9+
def initialize(klass, args, block)
10+
@klass = klass
11+
@args = args
12+
@block = block
2113
end
2214

23-
def klass
24-
@klass || classcache[@name]
25-
end
15+
def name; klass.name; end
2616

2717
def ==(middleware)
2818
case middleware
@@ -31,7 +21,7 @@ def ==(middleware)
3121
when Class
3222
klass == middleware
3323
else
34-
normalize(@name) == normalize(middleware)
24+
normalize(name) == normalize(middleware)
3525
end
3626
end
3727

@@ -123,8 +113,25 @@ def assert_index(index, where)
123113

124114
private
125115

116+
def get_class(klass)
117+
if klass.is_a?(String) || klass.is_a?(Symbol)
118+
classcache = ActiveSupport::Dependencies::Reference
119+
converted_klass = classcache[klass.to_s]
120+
ActiveSupport::Deprecation.warn <<-eowarn
121+
Passing strings or symbols to the middleware builder is deprecated, please change
122+
them to actual class references. For example:
123+
124+
"#{klass}" => #{converted_klass}
125+
126+
eowarn
127+
converted_klass
128+
else
129+
klass
130+
end
131+
end
132+
126133
def build_middleware(klass, args, block)
127-
Middleware.new(klass, args, block)
134+
Middleware.new(get_class(klass), args, block)
128135
end
129136
end
130137
end

actionpack/test/abstract_unit.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
123123

124124
def self.build_app(routes = nil)
125125
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
126-
middleware.use "ActionDispatch::ShowExceptions", ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
127-
middleware.use "ActionDispatch::DebugExceptions"
128-
middleware.use "ActionDispatch::Callbacks"
129-
middleware.use "ActionDispatch::ParamsParser"
130-
middleware.use "ActionDispatch::Cookies"
131-
middleware.use "ActionDispatch::Flash"
132-
middleware.use "Rack::Head"
126+
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
127+
middleware.use ActionDispatch::DebugExceptions
128+
middleware.use ActionDispatch::Callbacks
129+
middleware.use ActionDispatch::ParamsParser
130+
middleware.use ActionDispatch::Cookies
131+
middleware.use ActionDispatch::Flash
132+
middleware.use Rack::Head
133133
yield(middleware) if block_given?
134134
end
135135
end

actionpack/test/dispatch/middleware_stack/middleware_test.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ def setup
3333
@stack = ActionDispatch::MiddlewareStack.new
3434
end
3535

36-
def test_string_class
37-
stack = ActionDispatch::MiddlewareStack.new
38-
stack.use Omg.name
39-
assert_equal Omg, stack.first.klass
40-
end
41-
4236
def test_double_equal_works_with_classes
4337
k = Class.new
4438
stack.use k

actionpack/test/dispatch/middleware_stack_test.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ def setup
2525
end
2626

2727
test "use should push middleware as a string onto the stack" do
28-
assert_difference "@stack.size" do
29-
@stack.use "MiddlewareStackTest::BazMiddleware"
28+
assert_deprecated do
29+
assert_difference "@stack.size" do
30+
@stack.use "MiddlewareStackTest::BazMiddleware"
31+
end
32+
assert_equal BazMiddleware, @stack.last.klass
3033
end
31-
assert_equal BazMiddleware, @stack.last.klass
3234
end
3335

3436
test "use should push middleware as a symbol onto the stack" do
35-
assert_difference "@stack.size" do
36-
@stack.use :"MiddlewareStackTest::BazMiddleware"
37+
assert_deprecated do
38+
assert_difference "@stack.size" do
39+
@stack.use :"MiddlewareStackTest::BazMiddleware"
40+
end
41+
assert_equal BazMiddleware, @stack.last.klass
3742
end
38-
assert_equal BazMiddleware, @stack.last.klass
3943
end
4044

4145
test "use should push middleware class with arguments onto the stack" do
@@ -88,8 +92,10 @@ def setup
8892
end
8993

9094
test "unshift adds a new middleware at the beginning of the stack" do
91-
@stack.unshift :"MiddlewareStackTest::BazMiddleware"
92-
assert_equal BazMiddleware, @stack.first.klass
95+
assert_deprecated do
96+
@stack.unshift :"MiddlewareStackTest::BazMiddleware"
97+
assert_equal BazMiddleware, @stack.first.klass
98+
end
9399
end
94100

95101
test "raise an error on invalid index" do
@@ -103,15 +109,11 @@ def setup
103109
end
104110

105111
test "lazy evaluates middleware class" do
106-
assert_difference "@stack.size" do
107-
@stack.use "MiddlewareStackTest::BazMiddleware"
112+
assert_deprecated do
113+
assert_difference "@stack.size" do
114+
@stack.use "MiddlewareStackTest::BazMiddleware"
115+
end
116+
assert_equal BazMiddleware, @stack.last.klass
108117
end
109-
assert_equal BazMiddleware, @stack.last.klass
110-
end
111-
112-
test "lazy compares so unloaded constants are not loaded" do
113-
@stack.use "UnknownMiddleware"
114-
@stack.use :"MiddlewareStackTest::BazMiddleware"
115-
assert @stack.include?("::MiddlewareStackTest::BazMiddleware")
116118
end
117119
end

0 commit comments

Comments
 (0)