-
Notifications
You must be signed in to change notification settings - Fork 149
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
Opaque types may cause deserialization to never complete #444
Comments
The relevant stack trace is following. Would it suggest a bad interaction between magnolia and scala's internal implementation of givens? This would suggest #434 result would be dearly needed
|
Additionally: declaring Email's JsonDecoder this way: inline given JsonDecoder[Email] = JsonDecoder[String].map(Email.apply) will cause the generated decoder to immediately crash due to NoSuchMethodError:
(in other words, Magnolia will not notice that the JsonDecoder[Email] is declared inline and has no existence within the jar. Hopefully the native derivation-based generator will be able to pull the decoder AST directly, whether inline or not) |
Thanks for the report. |
I have the same issue with zio-json 0.5.0. Is there a workaround that works with opaque types? The compiler warns me of the infinite loop when I do this with opaque type: |
Same for me with 0.5.0:
for opaque type UserName = String
object UserName {
def apply(value: String): UserName = value
given JsonDecoder[UserName] = JsonDecoder[String].map(UserName.apply)
}
|
I think it's not related to this library ( As a workaround you can use given JsonDecoder[UserName] = JsonDecoder.string |
You're right ! And thank you for your feedback :) The example I've just written to test this workaround : // ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0"
//> using dep "dev.zio::zio-json:0.5.0"
//> using options "-Yretain-trees" // When case classes are using default values
// ---------------------
import zio.*, zio.json.*, zio.worksheet.*
import java.time.OffsetDateTime
opaque type UserName = String
object UserName {
def apply(value: String): UserName = value
given JsonCodec[UserName] = JsonCodec.string
}
opaque type LastSeenDateTime = OffsetDateTime
object LastSeenDateTime {
def apply(value: OffsetDateTime): LastSeenDateTime = value
given JsonCodec[LastSeenDateTime] = JsonCodec.offsetDateTime
}
case class User(
userName: UserName,
lastSeen: LastSeenDateTime
) derives JsonCodec
val json =
"""{
| "userName":"joe",
| "lastSeen":"2021-04-09T17:19:17.000Z"
|}""".stripMargin
val app =
for
person <- ZIO.fromEither(json.fromJson[User])
_ <- Console.printLine(person.toJsonPretty)
yield ()
app.unsafeRun |
Using zio-json 2.0.0-M1 under Scala 3.0.2.
The following scastie worksheet works fine: https://scastie.scala-lang.org/VGtUGBteQjiB9Y8Xlz4K5A
for reference, the code contents is:
However, splitting the above code into three separate files (as one would usually do) causes the
fromJson
call to apparently never return:hello/Email.scala:
hello/Structure.scala:
hello/TestCase.scala:
Expected behaviour
calling
"""{ "name": "Toto", "email": "[email protected]"}""".fromJson[Structure]
works even if the "email" field is defined as an opaque field with a suitable decoderObserved behaviour
While the code appears to work as long as everything is in the same compilation unit (as it is in Scastie), it fails when split over distinct files.
Changing the Structure's email field type back to String works around the issue.
The text was updated successfully, but these errors were encountered: