cjbooms / fabrikt

Generates Kotlin Code from OpenApi3 Specifications
Apache License 2.0
156 stars 41 forks source link

Are 'Multipart Requests' supported? #309

Open b0m123 opened 2 months ago

b0m123 commented 2 months ago

Hi, I have a question whether Multipart requests are supported? (see https://swagger.io/docs/specification/describing-request-body/multipart-requests/) I'm trying to generate simple OkHttp client with model (here is my endpoint in openapi.yaml as example)

  /api/import
    post:
      operationId: importMedia
      summary: Import Media
      description: >
        TEST Import media file
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - interaction
              properties:
                metadata:
                  $ref: '#/components/schemas/MediaFileDto'
                mediaFile:
                  type: array
                  items:
                    type: string
                    format: binary

but I'm facing an error Unresolved reference 'MultipartFormData'.

To be clear I'm using fabrikt-gradle-plugin with this config

fabrikt {
    generate("Client") {
        apiFile = file("openapi.yaml")
        externalReferenceResolution = aggressive
        basePackage = "com.example"
        outputDirectory = file("${project.layout.buildDirectory.get().asFile}/generated")
        typeOverrides {
            datetime = OffsetDateTime
        }
        client {
            generate = enabled
            target = OkHttp
        }
        model {
            generate = enabled
            sealedInterfacesForOneOf = enabled
            ignoreUnknownProperties = enabled
            includeCompanionObject = enabled
        }
    }
}

Thanks in advance for any support

ulrikandersen commented 1 month ago

Hi @b0m123,

I can confirm that requests with multipart/form-data does not currently lead to working code being generated.

This applies to the OkHttp client as well as the controller interfaces for Micronaut, Spring and Ktor

For OkHttp we will need to generate specific code for multipart requests. This recipe from OkHttp describes how to do it.

ulrikandersen commented 1 month ago

One possible cause of action would be to add a special case to the request statement builder for operation.requestBody.getPrimaryContentMediaType()?.key == "multipart/form-data", which would construct the requestBody using OkHttp's MultipartBody.Builder().