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:

Resolving patch:
diff --git a/falcon/api.py b/falcon/api.py
--- a/falcon/api.py
+++ b/falcon/api.py
@@ -178,7 +178,8 @@ def __call__(self, env, start_response):
                 # 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 @@ def _call_req_mw(self, stack, req, resp):
             # 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
--- a/tests/test_httpstatus.py
+++ b/tests/test_httpstatus.py
@@ -166,7 +166,7 @@ def process_request(self, req, resp):
     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
--- 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 @@ def process_request(self, req, resp):
         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 @@ def process_request(self, req, resp):
         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 @@ def process_request(self, req, resp):
         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 @@ def test_base_path_is_removed_before_routing(self):
         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"})