Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenAPI 3.0 - how to define file download operation correctly

Tags:

openapi

I use online Swagger Editor with OpenAPI 3.0 and I have to create a definition file download. I develop server-side and a customer should be able to create a client using YAML description without my "addition explanation". The relevant part of YAML is:

/files/download/{fileName}:
get:
  summary: download file
  operationId: downloadFile
  description: this API is for file download
  parameters:
    - in: path
      name: fileName
      schema:
        type: string
      required: true
      description: The name of file to download
  responses:
    200:
      description: Operation performed successfully.
      content:
        application/octet-stream:
          schema:
            type: string
            format: binary
   ...

The questions are:

  • Content. Currently, it is defined as octet-stream as a most common type, but actually, it depends on the type of a file from some predefined set of file types. Is there any way to define such kind of mapping (file type/content type) and use it with a content tag?

  • The response header should include a key/value pair attachment or inline and file name. The file name is defined in path - {fileName}. Is there any way to describe the concatenation of enum {attachment, inline} and the value of fileName?

like image 949
bw_dev Avatar asked Aug 30 '25 18:08

bw_dev


1 Answers

I believe content is what you're looking for. You can also use contentReference to reference a reusable object in components. For example:

paths:
  /files/{fileName}:
    get:
      summary: download file
      operationId: downloadFile
      description: this API is for file download
      parameters:
        - in: path
          name: fileName
          schema:
            type: string
          required: true
          description: The name of file to download
      responses:
        200:
          description: Operation performed successfully.
          content:
            application/pdf:
              schema:
                type: string
                format: binary

components:
  contentTypes:
    application/pdf:
      schema:
        type: string
        format: binary
like image 115
Spacewink Avatar answered Sep 04 '25 23:09

Spacewink