-
Notifications
You must be signed in to change notification settings - Fork 146
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
Default case class values are memoized #1055
Comments
@jwcarvana Thanks for reporting! I've just added a missing check for default value memorization in jsoniter-scala and it passes for both Scala 2 and Scala 3 versions. Probably similar macro implementations could be used to fix the issue in zio-json, if it is not designed intentionally to support only constant values for defaults. |
/bounty $100 |
💎 $100 bounty • ZIOSteps to solve:
Thank you for contributing to zio/zio-json! Add a bounty • Share on socials
|
/attempt #1055 Options |
1 similar comment
/attempt #1055 Options |
Side note: default parameters are not working in Scala 3 at all, unless users add the Given the following case class: final case class RandomClass(
str: String,
uuid: UUID = UUID.randomUUID
) And the following code in def getDefaults: Array[Option[Any]] = {
println(s"Random id: ${UUID.randomUUID().toString}")
println(ctx.parameters.map(_.default).toArray.mkString("Array(", ", ", ")"))
ctx.parameters.map(_.default).toArray
} The first logged id is different every time a string is decoded into a
I would tend towards 3 - serialization with random generators isn't referentially transparent and I would lean towards keeping the two separate and doing random generation as part of an effect, serializing the case class with a default null value, and then copying the randomly generated value over. If there are performance considerations, then the following should suffice: final case class RandomClass(
str: String,
@jsonField("id")
private val initialId: UUID = null
) {
val id: UUID = if (initialId == null) {
UUID.randomUUID()
} else {
initialId
}
} @jdegoes , any preferences here? |
@Andrapyre: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏 |
The bounty is up for grabs! Everyone is welcome to |
💡 @Andrapyre submitted a pull request that claims the bounty. You can visit your bounty board to reward. |
zio-json is memoizing a variable's default in case classes even when the default is a method.
Here is a Scala 2 example. I'd expect each deserialization to create a unique UUID.
The text was updated successfully, but these errors were encountered: