Skip to content

Unable to set nonempty default for optional field #518

Open
@Kazark

Description

@Kazark

I am working on converting some contracts from using Avro4s to Vulcan. Because they are existing contracts I am constrained in what I can do---I am attempting to produce precisely equivalent Avro before and after.

I've hit a case that's giving me trouble. The following is a MWE.

import vulcan.*

final case class Foo(bar: Option[Int])

object Foo {
  implicit lazy val codec: Codec[Foo] =
    Codec.record(
      name = "Foo",
      namespace = "com.example",
    )(field =>
      field(
        "bar",
        _.bar,
        default = Some(Some(1337)),
      ).map(Foo.apply)
    )
}

This results in a Foo.codec.schema which is a Left containing

vulcan.AvroException$$anon$1: org.apache.avro.AvroTypeException: Invalid default for field bar: 1337 not a ["null","int"]

In my example, I was using an enum, not an integer, and got the same behavior. I noticed that Some(None) works as a default, but this does not match my existing contracts.

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