title: How to add a custom decorator to a FastAPI route?
body: <p>I want to add an <code>auth_required</code> decorator to my endpoints.
  (<em>Please consider that this question is about decorators, not middleware</em>)</p>
  <p>So a simple decorator looks like this:</p>
  <pre><code>def auth_required(func):
      def wrapper(*args, **kwargs):
          if user_ctx.get() is None:
              raise HTTPException(...)
          return func(*args, **kwargs)
      return wrapper
  </code></pre>
  <p>So there are 2 usages:</p>
  <pre><code>@auth_required
  @router.post(...)
  </code></pre>
  <p>or</p>
  <pre><code>@router.post(...)
  @auth_required
  </code></pre>
  <p>The first way doesn't work because <code>router.post</code> creates a router that saved into <code>self.routes</code> of APIRouter object. The second way doesn't work because it fails to verify pydantic object. For any request model, it says <code>missing args, missing kwargs</code>.</p>
  <p>So my question is - how can I add any decorators to FastAPI endpoints? Should I get into <code>router.routes</code> and modify the existing endpoint? Or use some <code>functools.wraps</code> like functions?</p>