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]tapir_enumeratum doest not handle @customise annotation and shortEnum output String type. #4292

Open
timzaak opened this issue Jan 27, 2025 · 0 comments

Comments

@timzaak
Copy link
Contributor

timzaak commented Jan 27, 2025

Tapir version: 1.11.12

Scala version: 3.6.1

the following code:

import sttp.tapir.Schema.annotations.customise
import sttp.tapir.generic.auto.*
import sttp.tapir.json.circe.*
import sttp.tapir.codec.enumeratum.{schemaForShortEnumEntry, schemaForIntEnumEntry}
import sttp.tapir.docs.apispec.DocsExtensionAttribute.*

@customise(s => s.docsExtension("x-enum-varnames", List("UnPaid", "Paid", "Finish", "Cancel")))
sealed abstract class OrderStatus(val value: Short) extends ShortEnumEntry

object OrderStatus
  extends ShortEnum[OrderStatus]
  with ShortCirceEnum[OrderStatus] {
  case object UnPaid extends OrderStatus(0)
  case object Paid extends OrderStatus(1)
  case object Finish extends OrderStatus(2)
  case object Cancel extends OrderStatus(3)
  val values = findValues

  given orderStatusTypeMapper: TypeMapper[OrderStatus] =
    new TypeMapper[OrderStatus] {
      def jdbcType: JDBCType = JDBCType.TINYINT
      def get(r: ResultSet, idx: Int): OrderStatus =
        OrderStatus.withValue(r.getShort(idx))
      def put(r: PreparedStatement, idx: Int, v: OrderStatus): Unit =
        r.setShort(idx, v.value)
    }
}

would output:

components:
  schemas:
    OrderStatus:
      title: OrderStatus
      type: integer
      enum:
      - '0'
      - '1'
      - '2'
      - '3'

problems are:

  1. does not handle @customise annotation correctly, this may be diffcult to fix.
  2. https://github.com/softwaremill/sttp-apispec/blob/327d37c16677818dbe46ab9f32f72b3473d6705c/jsonschema-circe/src/main/scala/sttp/apispec/internal/JsonSchemaCirceEncoders.scala#L129 does not handle short

If I chnage the code to:

sealed abstract class OrderStatus(val value: Short) extends ShortEnumEntry

object OrderStatus
  extends ShortEnum[OrderStatus]
  with ShortCirceEnum[OrderStatus] {
  case object UnPaid extends OrderStatus(0)
  case object Paid extends OrderStatus(1)
  case object Finish extends OrderStatus(2)
  case object Cancel extends OrderStatus(3)
  val values = findValues

  import sttp.tapir.codec.enumeratum.schemaForShortEnumEntry
  given Schema[OrderStatus] = {
    schemaForShortEnumEntry[OrderStatus]
      .docsExtension("x-enum-varnames", List("UnPaid", "Paid", "Finish", "Cancel")).format("int32")
  }
}

the output. would be

OrderStatus:
      title: OrderStatus
      type: integer
      enum:
      - '0'
      - '1'
      - '2'
      - '3'
      format: int32
      x-enum-varnames:
      - UnPaid
      - Paid
      - Finish
      - Cancel
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

1 participant