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

support for number backed enums #1612

Open
stanislav-chetvertkov opened this issue Oct 19, 2022 · 0 comments
Open

support for number backed enums #1612

stanislav-chetvertkov opened this issue Oct 19, 2022 · 0 comments

Comments

@stanislav-chetvertkov
Copy link
Contributor

according to
https://stackoverflow.com/questions/66465888/how-to-define-enum-mapping-in-openapi
openapi 3.1 allows the following format for defining number backed enums

openapi: 3.1.0
info:
  title: test
servers:
  - url: /

components:
  schemas:

    Severity:
      type: integer
      oneOf:
        - title: HIGH
          const: 2
          description: An urgent problem
        - title: MEDIUM
          const: 1
        - title: LOW
          const: 0
          description: Can wait forever

it fails with the following error

Error:Unknown type for the following structure (No type definition, class: io.swagger.v3.oas.models.media.JsonSchema, .components.schemas.Severity):
  Tracker(class JsonSchema {
      class Schema {
          type: [integer]
      }
  }, Vector(.components, .schemas, .Severity))

One could generate something similar to this instead

sealed abstract class Severity(val code: Int, val value: String) {
  override def toString: String = value
}
object Severity {
  object members {
    case object HIGH extends Severity(2, "HIGH")
    case object MEDIUM extends Severity(1, "MEDIUM")
    case object LOW extends Severity(0, "LOW")
  }
  val HIGH: Severity  = members.HIGH
  val MEDIUM: Severity = members.MEDIUM
  val LOW: Severity  = members.LOW

  val values  = Vector(HIGH, MEDIUM, LOW)
 
}

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