tapir icon indicating copy to clipboard operation
tapir copied to clipboard

Auto-derive default values

Open jumale opened this issue 8 months ago • 1 comments

Maybe I'm missing something, but I could not find the way how to tell the schema derivation to automatically include the default values.

For example I have a case class:

import sttp.tapir._

case class Settings(isArchived: Boolean = false)
object Settings {
  implicit val schema: Schema[Settings] = Schema.derived
}

By default the property will be derived as required. But I want to make it optional based on the default value. For that I see 2 options:

  1. either modify the schema
object Settings {
  implicit val schema: Schema[Settings] = 
    Schema.derived
      .modify(_.isArchived)(_.default(false))
}
  1. or add an annotation:
case class Settings(@default(false) isArchived: Boolean = false)

In both cases it's a duplication, i.e. if I change later one and forget the other - then I have an inconsistent documentation.

Would it be possible to capture the default values during derivation? Something like Schema.derivedWithDefaultValues

Note: I use Play JSON for encoding, and it provides Json.using[WithDefaultValues].format[Settings] - that's why I assume that a property can be optional in a request if it has a default value.

jumale avatar May 29 '24 07:05 jumale