How to define enum mapping in OpenAPI?

OpenAPI 3.1

OpenAPI 3.1 uses the latest JSON Schema, and the recommended way to annotate individual enum values in JSON Schema is to use oneOf+const instead of enum. This way you can specify both custom names (title) and descriptions for enum values.

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

OpenAPI 3.0 and 2.0

These versions do not have a way to define custom names for enum values, but some tools provide x- extensions for this purpose. For example:

  • AutoRest supports x-ms-enum.

  • NSwag supports x-enumNames:

    Severity:
      type: integer
      enum: [2, 1, 0]
      x-enumNames: [HIGH, MEDIUM, LOW]
    
  • openapi-typescript-codegen supports x-enum-varnames:

    Severity:
      type: integer
      enum: [2, 1, 0]
      x-enum-varnames: [HIGH, MEDIUM, LOW]
    

Check with your tooling vendors to see if they have a similar extension.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)