Skip to content

besom-json: support derives JsonReader/Encoder on Scala 3 enums with case class variants #596

@lbialy

Description

@lbialy

Problem

derives JsonReader and derives Encoder on Scala 3 enums with parameterized cases fails with a macro expansion error:

enum Endpoint derives JsonReader, Encoder:
  case HttpIngress(host: String, port: Int)
  case NodePort(port: Int, protocol: String)

Error:

Exception occurred while executing macro expansion.
scala.MatchError: ... (of class scala.quoted.runtime.impl.ExprImpl)
  at besom.json.ProductFormatsMacro$.jsonReaderImpl(ProductFormats.scala:177)

Current Workaround

Using sealed trait + case classes instead:

sealed trait Endpoint
object Endpoint:
  case class HttpIngress(...) extends Endpoint derives JsonReader, Encoder
  case class NodePort(...) extends Endpoint derives JsonReader, Encoder

Expected

Enum with case class variants should work with derives JsonReader, Encoder, producing a discriminated union JSON representation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/coreThe SDK's core codekind/improvementAn improvement with existing workaround

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions