zio / zio-json

Fast, secure JSON library with tight ZIO integration.
https://zio.dev/zio-json
Apache License 2.0
401 stars 143 forks source link

fix: default case class values memoized #1119

Open Andrapyre opened 1 month ago

Andrapyre commented 1 month ago

Description

This PR closes https://github.com/zio/zio-json/issues/1055 and, in the process, updates zio-json to the Scala Native 0.5.x series (necessary because this requires multiple PRs to Magnolia and Magnolia is already on the 0.5.x series). The solution does the following things:

  1. It provides out-of-the-box support for random generators which are not time-based, as well as support for all classes that extend java.time.temporal.Temporal - all of the following:

    • HijrahDate
    • Instant,
    • JapaneseDate
    • LocalDate
    • LocalDateTime
    • LocalTime
    • MinguoDate
    • OffsetDateTime
    • OffsetTime
    • ThaiBuddhistDate
    • Year
    • YearMonth
    • ZonedDateTime
  2. It introduces a @jsonAlwaysEvaluateDefault annotation to force evaluation of the default parameter every time json is decoded (especially relevant for timestamp values coming from 3rd party libs, e.g. org.joda.time).

The result is that users can implement the following case class and expect the results for each parameter as described in the comments:

final case class DynamicDefaultExample (
    @jsonAlwaysEvaluateDefault
    jodaInstant: joda.time.Instant = joda.time.Instant.now(), //Will always be fresh, due to annotation
    sometimesMemoizedJodaInstant: joda.time.Instant = joda.time.Instant.now(), //Will be flaky. Without @jsonAlwaysEvaluateDefault, this will sometimes be parsed every time; sometimes it will be memoized.
    javaInstant: java.time.Instant = java.time.Instant.now(), //will always be fresh, with or without annotation
    uuid: UUID = UUID.randomUUID(), //will always be fresh, with or without annotation
    random: Double = scala.math.random() //will always be fresh, with or without annotation
)

External dependencies

Several external dependencies need to be closed before this will build in Github Actions:

  1. The core ZIO libraries need to be upgraded to Scala Native 0.5.x. This can be started once upstream dependencies are upgraded. https://github.com/zio/zio/issues/8786
  2. The scala-java-time lib needs to be upgraded to Scala Native 0.5.x. https://github.com/cquiroz/scala-java-time/pull/508
  3. One PR needs to be merged into the Magnolia Scala 2.x series. https://github.com/softwaremill/magnolia/pull/533
  4. One PR needs to be merged into the Magnolia Scala 3.x series. https://github.com/softwaremill/magnolia/pull/534

/claim #1055

Andrapyre commented 1 month ago

@jdegoes , while this is waiting for upstream dependencies to upgrade to Scala Native 0.5.x, could you review the current implementation? I've tried to preserve the benefits of Magnolia's pre-computation as much as possible, while still offering support for a wide array of default values that need to be computed in real time.