Repository: falconry/falcon

Issue:
Pass params to process_resource
This is needed for feature parity with global *before* hooks. We can use a shim to avoid breaking existing middleware.

Discussion:
kgriffs: I think this makes sense. It is a breaking change, but one that should be easy for developers to accommodate. A couple testing suggestions inline.

Patch:
diff --git a/falcon/api.py b/falcon/api.py
index cdc2c66..1a6b944 100644
--- a/falcon/api.py
+++ b/falcon/api.py
@@ -178,7 +178,8 @@ class API(object):
                 # e.g. a 404.
                 responder, params, resource = self._get_responder(req)
 
-                self._call_rsrc_mw(middleware_stack, req, resp, resource)
+                self._call_rsrc_mw(middleware_stack, req, resp, resource,
+                                   params)
 
                 responder(req, resp, **params)
                 self._call_resp_mw(middleware_stack, req, resp, resource)
@@ -537,13 +538,13 @@ class API(object):
             # Put executed component on the stack
             stack.append(component)  # keep track from outside
 
-    def _call_rsrc_mw(self, stack, req, resp, resource):
+    def _call_rsrc_mw(self, stack, req, resp, resource, params):
         """Run process_resource middleware methods."""
 
         for component in self._middleware:
             _, process_resource, _ = component
             if process_resource is not None:
-                process_resource(req, resp, resource)
+                process_resource(req, resp, resource, params)
 
     def _call_resp_mw(self, stack, req, resp, resource):
         """Run process_response middleware."""


Test patch:
diff --git a/tests/test_httpstatus.py b/tests/test_httpstatus.py
index 3569e6b..f525be1 100644
--- a/tests/test_httpstatus.py
+++ b/tests/test_httpstatus.py
@@ -166,7 +166,7 @@ class TestHTTPStatusWithGlobalHooks(testing.TestBase):
     def test_raise_status_in_process_resource(self):
         """ Make sure we can raise status from middleware process resource """
         class TestMiddleware:
-            def process_resource(self, req, resp, resource):
+            def process_resource(self, req, resp, resource, params):
                 raise HTTPStatus(falcon.HTTP_200,
                                  headers={"X-Failed": "False"},
                                  body="Pass")
diff --git a/tests/test_middlewares.py b/tests/test_middlewares.py
index 226e57f..519852a 100644
--- a/tests/test_middlewares.py
+++ b/tests/test_middlewares.py
@@ -1,3 +1,5 @@
+import json
+
 import falcon
 import falcon.testing as testing
 from datetime import datetime
@@ -11,7 +13,7 @@ class RequestTimeMiddleware(object):
         global context
         context['start_time'] = datetime.utcnow()
 
-    def process_resource(self, req, resp, resource):
+    def process_resource(self, req, resp, resource, params):
         global context
         context['mid_time'] = datetime.utcnow()
 
@@ -34,7 +36,7 @@ class ExecutedFirstMiddleware(object):
         context['executed_methods'].append(
             '{0}.{1}'.format(self.__class__.__name__, 'process_request'))
 
-    def process_resource(self, req, resp, resource):
+    def process_resource(self, req, resp, resource, params):
         global context
         context['executed_methods'].append(
             '{0}.{1}'.format(self.__class__.__name__, 'process_resource'))
@@ -55,9 +57,17 @@ class RemoveBasePathMiddleware(object):
         req.path = req.path.replace('/base_path', '', 1)
 
 
+class AccessParamsMiddleware(object):
+
+    def process_resource(self, req, resp, resource, params):
+        global context
+        params['added'] = True
+        context['params'] = params
+
+
 class MiddlewareClassResource(object):
 
-    def on_get(self, req, resp):
+    def on_get(self, req, resp, **kwargs):
         resp.status = falcon.HTTP_200
         resp.body = {'status': 'ok'}
 
@@ -368,3 +378,23 @@ class TestRemoveBasePathMiddleware(TestMiddleware):
         self.assertEqual(self.srmock.status, falcon.HTTP_200)
         self.simulate_request('/base_pathIncorrect/sub_path')
         self.assertEqual(self.srmock.status, falcon.HTTP_404)
+
+
+class TestResourceMiddleware(TestMiddleware):
+
+    def test_can_access_resource_params(self):
+        """Test that params can be accessed from within process_resource"""
+        global context
+
+        class Resource:
+            def on_get(self, req, resp, **params):
+                resp.data = json.dumps(params)
+
+        self.api = falcon.API(middleware=AccessParamsMiddleware())
+        self.api.add_route('/path/{id}', Resource())
+        resp = self.simulate_request('/path/22')
+
+        self.assertIn('params', context)
+        self.assertTrue(context['params'])
+        self.assertEqual(context['params']['id'], '22')
+        self.assertEqual(json.loads(resp[0]), {"added": True, "id": "22"})