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

Problem with reading $ref in schema with multipart

Open diaes opened this issue 2 years ago • 1 comments

In my yaml I have some request defined in schema. But for some reason, when I mix multipart and $ref - reference is not imported correctly. I got message message: 'request/body/imageDescription must be object',. When I replace $ref with its contents - works perfectly.

To Reproduce I reproduced it on existing tests in this project. In test add to multipart.yaml new definition:

components:
  schemas:
    Image:
      type: object
      required:
        - image
        - imageDescription
      properties:
        image:
          description: The image
          type: string
          format: binary
        imageDescription:
          $ref: '#/components/schemas/Description'
    Description:
      type: object
      required:
        - imageNumber
      properties:
       imageNumber:
         description: The image
         type: number

and add test to multipart.spec.ts

    it(' embeded $ref', async () =>
        request(app)
        .post(`${app.basePath}/sample_6`)
        .set('Content-Type', 'multipart/form-data')
        .attach('image', 'package.json')
        .field('imageDescription', '{"imageNumber": 1}')
        .expect(200));

When I remove reference and past this directly to property it works as expected. Here is yaml without #ref.

    Image:
      type: object
      required:
        - image
        - imageDescription
      properties:
        image:
          description: The image
          type: string
          format: binary
        imageDescription:
          type: object
          required:
            - imageNumber
          properties:
            imageNumber:
              description: The image
              type: number

Actual behavior Throws error:

{
  message: 'request/body/imageDescription must be object',
  errors: [
    {
      path: '/body/imageDescription',
      message: 'must be object',
      errorCode: 'type.openapi.validation'
    }
  ]
}

Expected behavior It should behave like in case when no $ref is used.

diaes avatar Feb 22 '23 12:02 diaes

The version 5.0.2 with the fix here : https://github.com/cdimascio/express-openapi-validator/commit/e5cb5d68607fa0ad37c074363c471bb11d7791bb allows the use of this format to avoid the issue above :

    requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                document:
                  type: object
                  allOf:
                    - $ref: "#/components/schemas/Document"
                file:
                  type: string
                  format:
                    binary

It still does not support this though

       requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                document:
                  $ref: "#/components/schemas/Document"
                file:
                  type: string
                  format:
                    binary

fredbonin avatar Mar 01 '23 17:03 fredbonin