znsio / specmatic

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
https://specmatic.io
MIT License
280 stars 52 forks source link

Stubbing "multipart-form-data" request with part array #690

Open aeoncl opened 1 year ago

aeoncl commented 1 year ago

Description

Hi 😁, here's the issue:

In a multipart-form-data, you can have an array of parts by specifing the same partname multiple times.

Here is how it looks in the OpenApi definition (pdfFiles):

requestBody:
  content:
    multipart/form-data:
      schema:
        type: object
        properties:
          sendMessageRequest:
            $ref: "#/components/schemas/SendMessageRequest"
          pdfFiles:
            type: array
            items:
              type: string
              format: binary
        required:
          - sendMessageRequest
          - pdfFiles

We expect this to be an array of pdfFiles: image

If i send this request to the Specmatic Stub, i get the following error:

image

This happens because Specmatic thinks a multipart part of type array should contain an array, and not be one: Internally this is interpreted as a ListPattern which matches only if the value is an array.

Snippet from ListPattern.kt image

If i change my part value to an array, it works, but that's not the behaviour i expected: image

Steps to reproduce

  1. Create a stub using the following OpenApi Definition:
    
    openapi: 3.0.3
    info:
    title: Multipart With Array Example
    description: Service to add context to a github issue
    version: 1.0.0

servers:

  1. Make an HTTP POST request to the /message endpoint containing the following parts:

image

Expected behavior I expect to receive a 200 OK generated response

System Information:

Additional context To fix it, we'll need to change the way Specmatic interprets "type: array" on multipart parts. Changing the meaning from "contains a json array" to "expect to have the same part multiple times"

jaydeepk commented 1 year ago

Thanks for reporting this @aeoncl. We'll have a look at this and get back to you.

aeoncl commented 1 year ago

Hi guys, i've started working on this. i'll submit a PR in the coming days for you to review

jaydeepk commented 1 year ago

Hi @aeoncl, really appreciate the initiative. Please do let us know if you have any queries regarding the code.

harikrishnan83 commented 1 year ago

@aeoncl Thank you for your patience. The reason we were taking time on this PR is to make sure we also understood the http RFC around this. And it does look like the expected behaviour in your bug description is valid as per this section in RFC 7578. We are now reviewing your PR in this regard. cc @jaydeepk

tomknee-accelins commented 2 months ago

Hi, I seem to be running into similar issue trying to upload a file in request body where content type is multipart/form-data