Play 2.4 ActionBuilder / ActionFunction, BodyParsers and JSON
<p>In a Play 2.4 project, I created an Action that do three things.</p>

<ol>
<li>check that a specific header is present. If not, returns a HTTP error.</li>
<li>use the header value to authenticate the user (a authentication function will be passed to that Action). If the auth fails returns an HTTP error</li>
<li>parse the JSON body to a case class and give it to the Action block code.</li>
</ol>

<p>To do that, I used the Play Action composition mecanism explain in this page : <a href="https://www.playframework.com/documentation/2.4.x/ScalaActionsComposition" rel="nofollow">https://www.playframework.com/documentation/2.4.x/ScalaActionsComposition</a><br>
but more specifically, the final result I wanted is explained here :
<a href="https://www.playframework.com/documentation/2.4.x/ScalaActionsComposition#Putting-it-all-together" rel="nofollow">https://www.playframework.com/documentation/2.4.x/ScalaActionsComposition#Putting-it-all-together</a></p>

<p>I succeeded to write this:</p>

<pre class="lang-scala prettyprint-override"><code>package actions

import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json._
import play.api.mvc.Results._
import play.api.mvc.{WrappedRequest, _}

import scala.concurrent.Future

object Actions {

  case class WithApiKeyRequest[A](apiKey: String, request: Request[A]) extends WrappedRequest[A](request)
  case class ParsedJsonRequest[A](parsed: Any, request: Request[A]) extends WrappedRequest[A](request)

  def AuthenticatedAndParsed[T, A](authencation: String =&gt; Future[_])(implicit reader: Reads[T]): ActionBuilder[ParsedJsonRequest] =
    WithApiKeyHeaderAction andThen AuthentificationAction(authencation) andThen JsonAction

  private[this] def WithApiKeyHeaderAction = new ActionBuilder[WithApiKeyRequest] {
    override def invokeBlock[A](request: Request[A], block: (WithApiKeyRequest[A]) =&gt; Future[Result]): Future[Result] =
      request.headers.get("ApiKey") match {
        case Some(apiKey: String) =&gt; block(WithApiKeyRequest(apiKey, request))
        case _                    =&gt; Future.successful { BadRequest(Json.obj("errors" -&gt; "ApiKey header needed")) }
      }
  }

  private[this] def AuthentificationAction(authencationFunction: String =&gt; Future[_]) = new ActionFilter[WithApiKeyRequest] {
    override protected def filter[A](request: WithApiKeyRequest[A]): Future[Option[Result]] =
      authencationFunction(request.apiKey)
        .map { _ =&gt; None } // Do not filter the request
        .recover { case _ =&gt; Some(Unauthorized) }
  }

  private[this] def JsonAction[T](implicit reader: Reads[T]) = new ActionBuilder[ParsedJsonRequest] {
    composeParser(BodyParsers.parse.json)
    override def invokeBlock[A](request: Request[A], block: (ParsedJsonRequest[A]) =&gt; Future[Result]): Future[Result] = {
      request.body.asInstanceOf[JsValue].validate[T].fold(
        errors =&gt; Future { BadRequest(Json.obj("errors" -&gt; JsError.toJson(errors))) },
        (parsedJson: T) =&gt; block(ParsedJsonRequest(parsedJson, request))
      )
    }
  }
}
</code></pre>

<p>It seems to work well but it's not perfect because I'm force to use the <code>Any</code> type in the <code>case class ParsedJsonRequest[A](parsed: Any, request: Request[A])</code> because it seems that I can't do that: 
<code>case class ParsedJsonRequest[T, A](parsed: T, request: Request[A])</code>    </p>

<p>Is it possible to do that ?
Do you think I can improve my solution ? How ?</p>

<p>My question is not about how to do Action composition. I understand how it works and I succeeded to write my ActionBuilders and my wanted composition.
My question is about how to improve my composition.</p>

<p>Thanks<br>
Jules</p>