rails/rails — Issue #4873: ActionDispatch::MiddlewareStack does not allow swapping middleware of same class
Author: jonahb
Created: 2012-02-03 21:51:17+00:00
State: open

Ruby 1.9.2p290
Rails 3.2.1

```
config.middleware.swap ActionDispatch::ShowExceptions,
ActionDispatch::ShowExceptions,
lambda { |env| [ 500, {}, ['error!'] }
```

Expected: Middleware stack contains an instance of ActionDispatch::ShowExceptions with the new exceptions app.

Observed: ActionDispatch::ShowExceptions has been deleted from the middleware stack.

The problem is ActionDispatch::MiddlewareStack#swap:

```
def swap(target, *args, &block)
insert_before(target, *args, &block)
delete(target)
end
```

The delete call deletes *all* middlewares of the given class, including the one that was just inserted.

The workaround is to use #delete and #insert, but still, #swap should work as expected.