0

I'm trying to extend the SecureAction of the pac4j-play module, because i want some extra stuff validated and added to the request, for easy access in the controller.

But i run into a type-mismatch compilation issue, and i cannot seem to grasp why.

class SecureSiteAction[P<:CommonProfile, ContentType, R[X]>:AuthenticatedRequest[P, X]<:Request[X]](clients: String, authorizers: String, matchers: String, multiProfile: Boolean, parser: BodyParser[ContentType], playSessionStore: PlaySessionStore, config: Config)(implicit implicitExecutionContext: ExecutionContext)
  extends SecureAction[P, ContentType, R](clients, authorizers, matchers, multiProfile, parser, playSessionStore, config) {

  override def invokeBlock[A](request: Request[A], block: R[A] => Future[Result]): Future[Result] = {
    super.invokeBlock(request, { request: R[A] =>
      val site = Site(request.host)
      val profiles = request.asInstanceOf[AuthenticatedRequest[P, A]].profiles
      block(AuthenticatedSiteRequest(site, profiles, request))
    })
  }
}

case class AuthenticatedSiteRequest[P<:CommonProfile, +A](site: Site, profiles: List[P], request: Request[A]) extends WrappedRequest[A](request)

I have imported stuff like import scala.language.higherKinds and the rest from the original SecureAction class (see https://github.com/pac4j/play-pac4j/blob/master/shared/src/main/scala/org/pac4j/play/scala/Security.scala).

the following line is the issue:

block(AuthenticatedSiteRequest(site, profiles, request))

which gives the type error:

type mismatch;
[error]  found   : binders.AuthenticatedSiteRequest[P,A]
[error]  required: R[A]
[error]       block(AuthenticatedSiteRequest(site, profiles, request))

The "funny" thing is, that when i copy/paste the source code directly from pac4j-play's SecureAction case class into my IDE, i get the exact same compilation error.

What can make this compile? Is it a flag in sbt, or something i don't know about?

Martin Hansen
  • 2,033
  • 1
  • 18
  • 34

1 Answers1

0

You need to change the lower type bound to use your AuthenticatedSiteRequest in order to pin the one type parameter to the given parameter P of the SecureSiteAction class:

class SecureSiteAction[
    P <: CommonProfile, ContentType,
    R[X] >: AuthenticatedSiteRequest[P, X] <: Request[X]
  ] //      ^^^^^^^^^^^^^^^^^^^^^^^^ 

The "funny" thing is, that when i copy/paste the source code directly from pac4j-play's SecureAction case class into my IDE, i get the exact same compilation error.

That seems to be a problem of your IDE, proably IntelliJ?

cbley
  • 4,538
  • 1
  • 17
  • 32
  • I've tried that too, and did not work. Both my IDE and SBT complaints about the type. – Martin Hansen Feb 03 '20 at 10:54
  • Maybe there is some other, non-obvious, difference. Try to make a [reprex](https://stackoverflow.com/help/minimal-reproducible-example) – cbley Feb 03 '20 at 13:37