Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Scala 3, drop Scala 2.12 & Play 2.8 #165

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rtyley
Copy link
Member

@rtyley rtyley commented Nov 22, 2024

Need to import play.api.libs.ws._ for the BodyWritable[Map[String, Seq[String]]]

Panda's OAuth class posts some url-encoded data (defined as Map[String, Seq[String]] in Scala) with ws.url(dd.token_endpoint).post, and this needs an implicit instance of BodyWritable[Map[String, Seq[String]]] in order to work! For some reason, in Scala 2, the compiler was able to find the correct implicit somewhere, but in Scala 3 we get a compilation error:

[error] 81 |        }.flatMap { response =>
[error]    |         ^
[error]    |Cannot find an instance of Map[K, Seq[String]] to WSBody. Define a BodyWritable[Map[K, Seq[String]]] or extend play.api.libs.ws.ahc.DefaultBodyWritables
[error]    |
[error]    |where:    K is a type variable with constraint >: String
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |  import play.api.libs.ws.DefaultBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.WSBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.writeableOf_urlEncodedForm

Importing the whole ws package fixes the problem:

import play.api.libs.ws._

@rtyley rtyley force-pushed the support-scala-3-drop-scala-2.12-and-play-2.8 branch 2 times, most recently from 0d5dab3 to 83e81a2 Compare November 23, 2024 19:24
def logout = Action { implicit request =>
def logout: Action[AnyContent] = Action { implicit request =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly declaring the return type is necessary here or Scala 3 reports an 'ambiguous overload' error:

[error] -- [E051] Reference Error: /Users/Roberto_Tyley/code/pan-domain-authentication/pan-domain-auth-example/app/controllers/AdminController.scala:42:15 
[error] 42 |  def logout = Action { implicit request =>
[error]    |               ^^^^^^
[error]    |Ambiguous overload. The overloaded alternatives of method apply in trait ActionBuilder with types
[error]    | (block: play.api.mvc.Request[play.api.mvc.AnyContent] => play.api.mvc.Result):
[error]    |  play.api.mvc.Action[play.api.mvc.AnyContent]
[error]    | [A]
[error]    |  (bodyParser: play.api.mvc.BodyParser[A]):
[error]    |    play.api.mvc.ActionBuilder[play.api.mvc.Request, A]
[error]    |both match arguments (<?> => <?>)
[error]    |
[error]    | longer explanation available when compiling with `-explain`

@rtyley rtyley force-pushed the support-scala-3-drop-scala-2.12-and-play-2.8 branch from 83e81a2 to 053c2b0 Compare November 23, 2024 21:41
import play.api.libs.ws.{WSClient, WSResponse}
import play.api.libs.ws._
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panda's OAuth class posts some url-encoded data (defined as Map[String, Seq[String]] in Scala) with ws.url(dd.token_endpoint).post:

ws.url(dd.token_endpoint).post {
Map(
"code" -> code,
"client_id" -> Seq(config.clientId),
"client_secret" -> Seq(config.clientSecret),
"redirect_uri" -> Seq(redirectUrl),
"grant_type" -> Seq("authorization_code")
)
}.flatMap { response =>

This needs an implicit instance of BodyWritable[Map[String, Seq[String]]] in order to work! For some reason, in Scala 2, the compiler was able to find the correct implicit somewhere, but in Scala 3 we get a compilation error:

[error] 81 |        }.flatMap { response =>
[error]    |         ^
[error]    |Cannot find an instance of Map[K, Seq[String]] to WSBody. Define a BodyWritable[Map[K, Seq[String]]] or extend play.api.libs.ws.ahc.DefaultBodyWritables
[error]    |
[error]    |where:    K is a type variable with constraint >: String
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |  import play.api.libs.ws.DefaultBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.WSBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.writeableOf_urlEncodedForm

Importing the whole ws package fixes the problem.

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.9.5")
addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.5")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See:

Note that the Play sbt-plugin is only required for the exampleApp app, not any of the other subprojects.

@rtyley rtyley force-pushed the support-scala-3-drop-scala-2.12-and-play-2.8 branch 2 times, most recently from 4ef8a99 to 3e3b1bb Compare November 28, 2024 19:25
@rtyley rtyley force-pushed the support-scala-3-drop-scala-2.12-and-play-2.8 branch 2 times, most recently from bb8fe07 to d7c7a35 Compare December 10, 2024 10:44
@gu-scala-library-release
Copy link
Contributor

@rtyley has published a preview version of this PR with release workflow run #22, based on commit d7c7a35:

7.0.1-PREVIEW.support-scala-3-drop-scala-212-and-play-28.2024-12-10T1132.d7c7a35e

Want to make another preview release?

Click 'Run workflow' in the GitHub UI, specifying the support-scala-3-drop-scala-2.12-and-play-2.8 branch, or use the GitHub CLI command:

gh workflow run release.yml --ref support-scala-3-drop-scala-2.12-and-play-2.8

Want to make a full release after this PR is merged?

Click 'Run workflow' in the GitHub UI, leaving the branch as the default, or use the GitHub CLI command:

gh workflow run release.yml

rtyley added 2 commits January 7, 2025 18:58
See:

* #149 - dropping Scala 2.12
* #161 - supporting Scala 3

Explicitly declaring the return type is necessary on many endpoint definitions, or Scala 3
reports an 'ambiguous overload' error:

```
[error] -- [E051] Reference Error: /Users/Roberto_Tyley/code/pan-domain-authentication/pan-domain-auth-example/app/controllers/AdminController.scala:42:15
[error] 42 |  def logout = Action { implicit request =>
[error]    |               ^^^^^^
[error]    |Ambiguous overload. The overloaded alternatives of method apply in trait ActionBuilder with types
[error]    | (block: play.api.mvc.Request[play.api.mvc.AnyContent] => play.api.mvc.Result):
[error]    |  play.api.mvc.Action[play.api.mvc.AnyContent]
[error]    | [A]
[error]    |  (bodyParser: play.api.mvc.BodyParser[A]):
[error]    |    play.api.mvc.ActionBuilder[play.api.mvc.Request, A]
[error]    |both match arguments (<?> => <?>)
[error]    |
[error]    | longer explanation available when compiling with `-explain`
```

Panda's `OAuth` class posts some url-encoded data (defined as `Map[String, Seq[String]]`
in Scala) with `ws.url(dd.token_endpoint).post`, and this needs an implicit instance of
`BodyWritable[Map[String, Seq[String]]]` in order to work! For some reason, in Scala 2,
the compiler was able to find the correct implicit somewhere, but in Scala 3 we get a
compilation error:

```
[error] 81 |        }.flatMap { response =>
[error]    |         ^
[error]    |Cannot find an instance of Map[K, Seq[String]] to WSBody. Define a BodyWritable[Map[K, Seq[String]]] or extend play.api.libs.ws.ahc.DefaultBodyWritables
[error]    |
[error]    |where:    K is a type variable with constraint >: String
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |
[error]    |One of the following imports might fix the problem:
[error]    |
[error]    |  import play.api.libs.ws.DefaultBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.WSBodyWritables.writeableOf_urlEncodedForm
[error]    |  import play.api.libs.ws.writeableOf_urlEncodedForm
```

Importing the whole `ws` package fixes the problem:

```
import play.api.libs.ws._
```
@rtyley rtyley force-pushed the support-scala-3-drop-scala-2.12-and-play-2.8 branch from d7c7a35 to 8057140 Compare January 7, 2025 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant