Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add "null" default for optional fields #714

Closed
erikvanoosten opened this issue Apr 8, 2022 · 2 comments
Closed

Feature request: add "null" default for optional fields #714

erikvanoosten opened this issue Apr 8, 2022 · 2 comments

Comments

@erikvanoosten
Copy link

Most of the fields in my schema are of type Option. I would rather not specify the default (None) in the case class as I want users to explicitly give a value for each field. It is too easy to forget giving a value. Also, it is a lot of typing :)

It would be great if the generated schema could add a default "null" for fields that are a union of "null" and something else (provided they don't have some other default).

@erikvanoosten
Copy link
Author

erikvanoosten commented Apr 8, 2022

As a workaround you can use this code that updates the generated schema.

Use it like this:

import MoreAvroSupport._

case class Person(name: Option[String])

val PersonSchema: Schema = AvroSchema[Person].withOptionDefaults
import org.apache.avro.Schema

import scala.jdk.CollectionConverters._

object MoreAvroSupport {

  def withOptionDefaults(schema: Schema): Schema = {
    schema.getType match {
      case Schema.Type.RECORD =>
        val updatedFields = schema.getFields.asScala.map { field =>
          val newDefault =
            if (
              field.schema().isUnion &&
              field.schema().getTypes.asScala.headOption.exists(_.getType == Schema.Type.NULL) &&
              !field.hasDefaultValue
            ) {
              Schema.Field.NULL_DEFAULT_VALUE
            } else {
              field.defaultVal()
            }
          new Schema.Field(
            field.name(),
            withOptionDefaults(field.schema()),
            field.doc(),
            newDefault,
            field.order()
          )
        }.asJava
        val updatedSchema = Schema.createRecord(
          schema.getName,
          schema.getDoc,
          schema.getNamespace,
          schema.isError,
          updatedFields
        )
        schema.getAliases.asScala.foreach(updatedSchema.addAlias)
        schema.getObjectProps.asScala.foreach { case (k, v) => updatedSchema.addProp(k, v) }
        updatedSchema
      case Schema.Type.MAP =>
        val updatedValueType = Schema.createArray(withOptionDefaults(schema.getValueType))
        schema.getObjectProps.asScala.foreach { case (k, v) => updatedValueType.addProp(k, v) }
        Schema.createMap(updatedValueType)
      case Schema.Type.UNION =>
        val newTypes = schema.getTypes.asScala.map(withOptionDefaults).asJava
        Schema.createUnion(newTypes)
      case Schema.Type.ARRAY =>
        val updatedElementType = Schema.createArray(withOptionDefaults(schema.getElementType))
        schema.getObjectProps.asScala.foreach { case (k, v) => updatedElementType.addProp(k, v) }
        updatedElementType
      case _ => schema
    }
  }

  implicit class SchemaWithOptionDefaults(val schema: Schema) {

    /** @return
     *   `schema` updated to have "null" as default for any union type that contains the "null"
     *   type and doesn't have a default already.
     *
     * To get a default value in the schema Avro4s requires you to write case classes with an
     * explicit default value, like this:
     * {{{
     * case class Person(name: Option[String] = None)
     * }}}
     * This method adds those "null" defaults to the generated schema so you can write case classes
     * like this:
     * {{{
     * case class Person(name: Option[String])
     * }}}
     */
    def withOptionDefaults: Schema = MoreAvroSupport.this.withOptionDefaults(schema)
  }

}

@stale
Copy link

stale bot commented Jun 11, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the abandoned label Jun 11, 2022
@stale stale bot closed this as completed Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant