softwaremill / softwaremill/tapir

Auto-derive default values

Open
#3,800 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.5k
Forks
468
Avg merge
5h 37m
Merged PRs (30d)
34

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Schema.derived entry point described in the issue and compare its handling of case-class defaults with Play JSON's WithDefaultValues behavior. Define how default-valued properties should appear in the derived schema, then verify that changing a case-class default is reflected without duplicating it in schema customization or annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.