Skip to content

Support recursive record schemas #58

Closed
@vlovgr

Description

@vlovgr

Codec.record does currently not support recursive types.

The following example:

import vulcan.Codec

final case class Recursive(next: Option[Recursive])

object Recursive {
  implicit def recursiveCodec: Codec[Recursive] =
    Codec.record[Recursive]("Recursive") { field => 
      field("next", _.next).map(Recursive(_))
    }
}

yields a StackOverflowError for Codec[Recursive].

scala> Codec[Recursive]
java.lang.StackOverflowError
  at scala.Option.fold(Option.scala:175)
  at vulcan.Codec$.record(Codec.scala:1468)
  at Recursive$.recursiveCodec(<pastie>:28)
  at Recursive$.$anonfun$recursiveCodec$1(<pastie>:30)
  at vulcan.Codec$.record(Codec.scala:1469)
  at Recursive$.recursiveCodec(<pastie>:28)
  at Recursive$.$anonfun$recursiveCodec$1(<pastie>:30)
  ...

Codec.derive also fails to derive a Codec with the following error message.

scala> Codec.derive[Recursive]
error: magnolia: could not find Codec.Typeclass for type Option[Recursive]
    in parameter 'next' of product type Recursive

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions