zio / zio-json

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

codec fails while decoder succeeds in 0.3.0-RC7 #622

Open fanf opened 2 years ago

fanf commented 2 years ago

When trying to test upgrade to ZIO 2.0, I discovered that a codec that was correctly decoding JSON with zio-json 2.0.0-M3 is now failing with 3.0.0-RC7. Even weirder, the decoding starts to work if I remove the encoder from the codec (and only keeps the decoder).

A minimization of the problem is available here: https://gist.github.com/fanf/8e77ffcb6af1be02670338254356135d

fsvehla commented 2 years ago

Thanks for the minimization, @fanf. I can confirm that something’ going on here and I’m working on it.

fsvehla commented 2 years ago

@fanf It looks to me that JsonCodec derivation isn’t considering JsonDecoders inside of other codecs when recursively deriving JsonDecoders.

The most minimal workaround is to also a JsonDecoder for each derived JsonCodec. In line :29 of your example

implicit lazy val eitherListOrValueDecoder: JsonDecoder[Either[List[String], JsonExpectedValueId7_1]] =
  codecJsonEitherValue.decoder

I’m not yet sure about how to fix this properly. Let me know if this workaround ↑ helps you.

fanf commented 2 years ago

I will try, thanks. What is suprising is that it used to work in ZIO 1.0

fanf commented 2 years ago

@fsvehla sorry for the delay, I was finally able to test your workaround and I can confirm that always using a pair of encode, decoder in place of a codec leads to the previous correct result. It's boilterplaty (well, two times more than previous boilerplate) but I least I can go on.

fsvehla commented 2 years ago

Thanks for the update.