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

Auto-derive default values #3800

Open
jumale opened this issue May 29, 2024 · 1 comment
Open

Auto-derive default values #3800

jumale opened this issue May 29, 2024 · 1 comment

Comments

@jumale
Copy link
Contributor

jumale commented May 29, 2024

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.

@adamw
Copy link
Member

adamw commented Jun 17, 2024

If I remember correctly we decided not to use the default values for (a) consistency with other schema configuration, (b) separating the schema that's exposed to users from the one that's used internally (you might not want to expose a default value to the outside world, but still use it internally).

Anyway, I think such customisation should be possible - probably Configuration would be the best place to put it? With some additional constructors to make the change binary-compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants