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

Added tests to format value classes #1041

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

levinson
Copy link

Pull Request Checklist

  • Have you read through the contributor guidelines?
  • Have you squashed your commits?
  • Have you added copyright headers to new files? N/A
  • Have you updated the documentation? N/A
  • Have you added tests for any changed functionality? N/A

Purpose

This PR demonstrates #1011. The Json.format[UserId] fails to compile with Scala 3:

[error] -- Error: JsonExtensionSpec.scala:755:58
[error] 755 |      implicit val userIdFmt: Format[UserId] = Json.format[UserId]
[error]     |                                               ^^^^^^^^^^^^^^^^^^^
[error]     |            Instance not found: 'ProductOf[play.api.libs.json.UserId]'

@levinson levinson force-pushed the value-class-scala3-compile-bug branch from 09730f7 to 627ffe2 Compare May 11, 2024 20:37
@@ -749,5 +751,19 @@ class JsonExtensionSpec extends AnyWordSpec with Matchers {
formatter.reads(Json.obj("props" -> JsNull)).mustEqual(JsSuccess(Optional(None)))
formatter.reads(Json.obj("props" -> Some("foo"))).mustEqual(JsSuccess(Optional(Some("foo"))))
}
"format value classes" in {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"format value classes" in {
"format value classes" in {

implicit val userIdFmt: Format[UserId] = Json.format[UserId]
val userId = UserId(12345)
val serialized = Json.stringify(Json.toJson(userId))
serialized.mustEqual("""{"id":12345}""")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
serialized.mustEqual("""{"id":12345}""")
serialized.mustEqual("""{"id":12345}""")

serialized.mustEqual("""{"id":12345}""")
Json.fromJson[UserId](Json.parse(serialized)).mustEqual(JsSuccess(userId))
}
"format value classes with value format" in {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"format value classes with value format" in {
"format value classes with value format" in {

implicit val userIdFmt: Format[UserId] = Json.valueFormat[UserId]
val userId = UserId(12345)
val serialized = Json.stringify(Json.toJson(userId))
serialized.mustEqual("""12345""")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
serialized.mustEqual("""12345""")
serialized.mustEqual("""12345""")

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.

2 participants