title: How to use a generic session with Actix and Paperclip?
body: <p>I've created a server using Actix Web which has authentication support. Now I want to add an OpenAPI Specification using <a href="https://docs.rs/paperclip/latest/paperclip/" rel="nofollow noreferrer">paperclip</a>.</p>
  <p>Normally you only have to add <code>#[derive(Apiv2Security)]</code> to the struct which represents the session of an authorized user. In my case this does not seem to do the trick because of some indirection: The app_data contains a generic Context type which defines what storage should be used (for example a database or an in memory store). The Context has an associated type which specifies the type of session it can load. The code looks something like this:</p>
  <pre class="lang-rust prettyprint-override"><code>use actix_http::Payload;
  use actix_web::{App, FromRequest, HttpRequest, HttpServer};
  use paperclip::actix::{api_v2_operation, web, Apiv2Security, OpenApiExt};
  use serde::{Deserialize, Serialize};
  use std::convert::Infallible;
  use std::future::{ready, Ready};
  
  trait Context {
      type Session: Session;
      fn session_by_id(&amp;self, session_id: usize) -&gt; Self::Session;
  }
  
  #[derive(Clone)]
  struct InMemoryContext;
  
  impl Context for InMemoryContext {
      type Session = SimpleSession;
  
      fn session_by_id(&amp;self, session_id: usize) -&gt; Self::Session {
          SimpleSession {
              id: session_id
          }
      }
  }
  
  pub trait Session {
      fn id(&amp;self) -&gt; usize;
  }
  
  #[derive(Clone, Deserialize, Serialize, Apiv2Security)]
  #[openapi(
      apiKey,
      in = &quot;header&quot;,
      name = &quot;Authorization&quot;,
      description = &quot;Use format 'Bearer TOKEN'&quot;
  )]
  pub struct SimpleSession {
      id: usize,
  }
  
  impl Session for SimpleSession {
      fn id(&amp;self) -&gt; usize {
          self.id
      }
  }
  
  impl FromRequest for SimpleSession {
      type Error = Infallible;
      type Future = Ready&lt;Result&lt;Self, Self::Error&gt;&gt;;
  
      fn from_request(req: &amp;HttpRequest, payload: &amp;mut Payload) -&gt; Self::Future {
          let ctx = req
              .app_data::&lt;web::Data&lt;InMemoryContext&gt;&gt;()
              .expect(&quot;InMemoryContext must be available&quot;)
              .get_ref()
              .clone();
          ready(Ok(ctx.session_by_id(42)))
      }
  }
  
  #[api_v2_operation]
  async fn hello_world_handler&lt;C: Context&gt;(session: C::Session) -&gt; web::Json&lt;usize&gt; {
      web::Json(session.id())
  }
  
  #[actix_web::main]
  async fn main() -&gt; std::io::Result&lt;()&gt; {
      HttpServer::new(|| {
          App::new()
              .app_data(web::Data::new(InMemoryContext {}))
              .wrap_api()
              .route(
                  &quot;/hello&quot;,
                  web::get().to(hello_world_handler::&lt;InMemoryContext&gt;),
              )
              .with_json_spec_at(&quot;/api/spec/v2&quot;)
              .build()
      })
      .bind((&quot;127.0.0.1&quot;, 8080))?
      .run()
      .await
  }
  </code></pre>
  <p>The compiler throws errors because it cannot find the methods generated by the derive macro:</p>
  <pre><code>error[E0599]: no function or associated item named `update_parameter` found for associated type `&lt;C as Context&gt;::Session` in the current scope
    --&gt; src/main.rs:61:1
     |
  61 | #[api_v2_operation]
     | ^^^^^^^^^^^^^^^^^^^ function or associated item not found in `&lt;C as Context&gt;::Session`
     |
     = note: this error originates in the attribute macro `api_v2_operation` (in Nightly builds, run with -Z macro-backtrace for more info)
  </code></pre>
  <p>Do you know how to get this working?</p>