zio / zio-schema

Compositional, type-safe schema definitions, which enable auto-derivation of codecs and migrations.
https://zio.dev/zio-schema
Apache License 2.0
142 stars 162 forks source link

JsonDecoder fails on discriminated ADT with case object #609

Closed ajaychandran closed 1 year ago

ajaychandran commented 1 year ago

Versions

Scastie

import zio.schema.*
import zio.schema.codec.JsonCodec

@annotation.discriminatorName("type")
sealed trait Command derives Schema
object Command:

  case class Buy(credits: Int) extends Command
  case object Cash extends Command

val decoder = JsonCodec.jsonDecoder(summon[Schema[Command]])
val encoder = JsonCodec.jsonEncoder(summon[Schema[Command]])

var encoding = encoder.encodeJson(Command.Buy(100))

println(encoding)

println(decoder.decodeJson(encoding))

encoding = encoder.encodeJson(Command.Cash)

println(encoding)

println(decoder.decodeJson(encoding))

Output:

{"type":"Buy","credits":100}
Right(Buy(100))
{"type":"Cash"}
Left(.type.Cash(expected '{' got '}'))