OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.61k stars 6.52k forks source link

[BUG] Multiple file upload + metadata fails as multipart, works as json+base64 #13110

Open sebpiller opened 2 years ago

sebpiller commented 2 years ago

Bug Report Checklist

Description

I'm trying to use openapi + spring boot to declare an endpoint where I can upload multiple files, each of them having metadatas (for example, an "urgent" flag that can be set for each file). I tried to use a multipart/form-data endpoint with a binary property but the generation of the beans&api is failing with a warning: [WARNING] Could not compute datatypeWithEnum from UploadFileModel, null

If I change the code to use a non-multipart request, eg. application/json+base64encoding for the upload file, then the issue disappear.

The code is available as a minimal reproducible example here: https://github.com/sebpiller/openapi-bug (toggle comment at demo.yaml, lines 11+12, 44+45 to have a working implem)

openapi-generator version

6.0.1, but fails similarly with all version I've tested

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Demo
  version: '1.0'
paths:
  '/demo/ok':
    post:
      operationId: ok
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/UploadFilesRequest'
      responses:
        '200':
          description: OK
          headers: { }
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UploadResponse'
components:
  schemas:
    UploadFilesRequest:
      title: Upload several files
      required:
        - files
      properties:
        files:
          type: array
          items:
            $ref: '#/components/schemas/UploadFile'

    UploadFile:
      title: A file upload with some metadata
      properties:
        urgent:
          type: boolean
        filename:
          type: string
        data:
          type: string
          format: binary

    UploadResponse:
      title: A stub response
      properties:
        accepted:
          type: boolean
Generation Details

Maven generator 6.0.1, see linked project's pom

Steps to reproduce

Run clean install on the demo project. It fails with a warning "[WARNING] Could not compute datatypeWithEnum from UploadFileModel, null"

Switching to application/json + base64 encoding of uploads works, but is suboptimal

aaokunev commented 1 year ago

Faced similar problem. Try to set skipFormModel = false