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

[BUG] Default value for enumeratum enums is not added to OpenAPI schema #1800

Closed
chessman opened this issue Jan 28, 2022 · 3 comments · Fixed by #1852
Closed

[BUG] Default value for enumeratum enums is not added to OpenAPI schema #1800

chessman opened this issue Jan 28, 2022 · 3 comments · Fixed by #1852
Assignees

Comments

@chessman
Copy link

Tapir version: 0.20.0-M6

Scala version: 2.12.15

Describe the bug

When I specify a default value for a string using annotations, it is added as a default value in yaml. But it is not happening in case of enumeratum.

How to reproduce?

import enumeratum._
import io.circe.generic.auto._
import sttp.tapir.Schema.annotations.default
import sttp.tapir._
import sttp.tapir.codec.enumeratum._
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter
import sttp.tapir.generic.auto._
import sttp.tapir.json.circe._
import sttp.tapir.openapi.OpenAPI
import sttp.tapir.openapi.circe.yaml._

object TapirTest extends App {

  sealed trait E extends EnumEntry
  object E extends Enum[E] with CirceEnum[E] {
    case object Value extends E
    override def values: scala.collection.immutable.IndexedSeq[E] = findValues
  }

  case class Input(
    @default(E.Value)
    e: E,
    @default("it works for strings")
    string: String
  )

  val testEndpoint = endpoint.in(jsonBody[Input])

  val docs: OpenAPI = OpenAPIDocsInterpreter().toOpenAPI(testEndpoint, "My Test", "1.0")
  println(docs.toYaml)
}

You can see that in the following output there is no default value for E:

  ...
  schemas:
    E:
      type: string
      enum:
      - Value
    Input:
      type: object
      properties:
        e:
          $ref: '#/components/schemas/E'
        string:
          type: string
          default: it works for strings
@mkrzemien mkrzemien self-assigned this Feb 7, 2022
@kubinio123 kubinio123 linked a pull request Feb 18, 2022 that will close this issue
@mkrzemien
Copy link
Contributor

An encoded version (the one included in the documentation) of a default value is automatically derived only in case of basic types (like strings or numbers). It is currently not supported for complex types like enumeratum enums.

Since version 0.20.0-M10 it is possible to provide custom encoded value, e.g.:

case class Input(
    @default(E.Value, encoded=Some("Value"))
    e: E
    //...
)

// or

case class Input(
    @default(E.Value, encoded=Some(E.Value))
    e: E
    //...
)

@chessman
Copy link
Author

chessman commented Mar 5, 2022

@sijarsu Thanks for the fix!

I tested it out and noticed that it doesn't work when an enum is wrapped in Option:

  case class Input(
     e: Option[E]
  )

Neither @default(E.Value, Some(E.Value)) nor @default(Option(E.Value), Some(E.Value)) works in this case.

@chessman
Copy link
Author

chessman commented Mar 5, 2022

Oh, it is not related to enumeratum. Annotations for case classes wrapped in Option also don't work. Like:

    @encodedExample(A("a").asJson)
    @default(A("a"), Some("a"))
    a: Option[A]

created an issue: #1920

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

Successfully merging a pull request may close this issue.

2 participants