Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aeoncl opened this issue Jul 31, 2023 · 5 comments
Open

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

aeoncl opened this issue Jul 31, 2023 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@aeoncl
Copy link
Contributor

aeoncl commented Jul 31, 2023

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:
  - url: "http://{hostname}:{port}/{basePath}/"
    variables:
      hostname:
        default: localhost
      port:
        default: "8080"
      basePath:
        default: rest
tags:
  - name: UploadFile
paths:
  "/message":
    post:
      tags:
        - UploadFile
      operationId: sendMessage
      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
            encoding:
              sendMessageRequest:
                contentType: application/json
      responses:
        "200":
          description: "Send Message Response"
          content:
            multipart/form-data:
              schema:
                type: object
                properties:
                  filename:
                    type: string
                    description: filename for associated file content
components:
  schemas:
    SendMessageRequest:
      type: object
      title: SendMessageRequest
      properties:
        foobar:
          type: string
          maxLength: 255
          minLength: 1
      required:
        - foobar
  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:

  • OS & version: Windows 11
  • Browser & version: Postman 10.16.0
  • Specmatic version: 0.73.0
  • JDK version: OpenJDK 17

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
Copy link
Collaborator

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

@harikrishnan83 harikrishnan83 self-assigned this Aug 1, 2023
@harikrishnan83 harikrishnan83 added the bug Something isn't working label Aug 1, 2023
@aeoncl
Copy link
Contributor Author

aeoncl commented Aug 1, 2023

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

@jaydeepk
Copy link
Collaborator

jaydeepk commented Aug 3, 2023

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

@harikrishnan83
Copy link
Member

harikrishnan83 commented Sep 8, 2023

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants