OpenAPI 3 - How to describe array with allowed key-value attributes in schema?

Viewed 368

I am trying to create a schema which includes key - value arrays, but the properties are themselves separately defined. For example, lets say I would like to describe a file with following properties:

  • id
  • fileName
  • fileType

where fileType is an object with id and a specific set of acceptable key-value combination, for example

  • 0, Word
  • 1, Excel
  • 2, PDF
  • ... etc

I am a bit confused on how to describe this with OpenApi 3 as from the documentation I can't seem to find a way to describe arrays with specific key - value combinations.

Right now I am using an enum like below

components:
  schemas:
   fileType:
     type: string
     description: file type
     enum:
      - Word
      - Excel
      - PDF

but that is inadequate. Thanks

1 Answers

You can use something like this:

Your fileType Schema

components:
  schemas:
    fileType:
      type: object
      description: file type
      properties:
        1:
          type: string
          example: "Word"
        2:
          type: string
          example: "Excel"
        3:
          type: string
          example: "PDF"

Here is your requestBudy

requestBody:
  description: File
  content:
    '*/*':
      schema:
        properties:
          id:
            type: string
          fileName:
            type: string
          fileType:
            $ref: '#/components/schemas/fileType'

Looks like

enter image description here

Hope this help.

Related