express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

fileUploader not working - Error: Unexpected end of form

Open Bec-k opened this issue 2 years ago • 3 comments

Using version ^5.0.4

Describe the bug File upload is failing and throws an error:

Error: Unexpected end of form
    at Multipart._final (/var/local/acp/dashboards/node_modules/busboy/lib/types/multipart.js:588:17)
    at callFinal (node:internal/streams/writable:698:12)
    at prefinish (node:internal/streams/writable:710:7)
    at finishMaybe (node:internal/streams/writable:720:5)
    at Multipart.Writable.end (node:internal/streams/writable:634:5)
    at onend (node:internal/streams/readable:705:10)
    at processTicksAndRejections (node:internal/process/task_queues:77:11)

To Reproduce Steps to reproduce the behavior. When specifying middleware with fileUploader: true:

middleware({
    apiSpec,
    fileUploader: true
})

and using below curl: curl -X POST -F "[email protected]" http://localhost:8001/docs/upload

Actual behavior File uploading is not working.

Expected behavior File uploading is working as expected.

Examples and context

  /docs/upload:
    post:
      summary: Upload a single PDF file
      operationId: DocsUpload
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                  description: The pdf document to upload.

Bec-k avatar May 26 '23 13:05 Bec-k

Everything is working if path added to ignore list or fileUploader set to false.

Bec-k avatar May 26 '23 13:05 Bec-k

Validation for the form-data can't be handled with this library, it's a headache to use another validation library to handle APIs specifically which are of form-data. I urge the community to handle the form-data requests as well

/**
 * @swagger
 * /admin/picture:
 *   post:
 *     tags:
 *       - Admin
 *     summary: Upload pictures as an admin
 *     description: Upload pictures as an admin
 *     operationId: uploadPictures
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               picture:
 *                 type: array
 *                 maxItems: 3
 *                 items:
 *                   type: string
 *                   format: binary
 *               caption:
 *                 type: string
 *             required:
 *               - picture
 *               - caption
 *     responses:
 *       "201":
 *         $ref: "#/components/responses/ServerResponse"
 *       "401":
 *         $ref: "#/components/responses/UnauthorizedResponse"
 *       "404":
 *         $ref: "#/components/responses/NotFoundResponse"
 *       "403":
 *         $ref: "#/components/responses/ForbiddenResponse"
 *       "400":
 *         $ref: "#/components/responses/InvalidInputResponse"
 *       "500":
 *         $ref: "#/components/responses/ServerErrorResponse"
 *     security:
 *       - api_key: []
 *       - bearerAuth: [] 
 */

Issue The validation for the /admin/picture endpoint is not working as expected when only a single file is sent in the request. The OpenAPI specification defines the picture property as an array of files, but the validation treats it as a string when a single file is sent.

Actual behavior The validation treats the picture field as a string when a single file is sent in the request, instead of treating it as an array.

Expected behavior The validation should correctly treat the picture field as an array when either a single file or multiple files are sent in the request.

rajathmantis avatar Jun 15 '23 14:06 rajathmantis

I am watching this issue as well. Have just ignored the relevant path for now.

gary-menzel avatar Mar 18 '24 02:03 gary-menzel