With Swagger CodeGen, how do I define valid mime types for an uploaded file?

623 Views Asked by At

I'm using OpenAPI 3.0.1 and swagger-codegen-maven-plugin 3.0.27 (happy to adjust these veersions). I have defined the following for uploading a file (along with other data) ...

  /myobject/:
      post:
        tags:
          - my-objects
        operationId: add
        requestBody:
          content:
            multipart/form-data:
              schema:
                $ref: '#/components/schemas/MyDTO'
          required: true

    ...
  schemas:
    MyDTO:
      type: object
      properties:
    ...
        dataFile:
          type: string
          format: binary

I would like to restrict the types of files uploaded to those with mime types of "application/vnd.ms-excel" and "text/csv". How do I do that with the OpenAPI spec/Swagger CodeGen?

2

There are 2 best solutions below

0
Arno On

You may define the encoding part in your spec, defining the expected mime types, like

...
encoding: # The same level as schema
    dataFile: # Property name
        contentType: application/vnd.ms-excel, text/csv
...

Refer to https://swagger.io/docs/specification/describing-request-body/multipart-requests/

0
Datz On

I would expect this to make requests with invalid data types fail:

  /myobject/:
      post:
        tags:
          - my-objects
        operationId: add
        requestBody:
          content:
            multipart/form-data:
              schema:
                $ref: '#/components/schemas/MyDTO'
              encoding:
                dataFile:
                  contentType: application/vnd.ms-excel, text/csv
          required: true