Skip to content

[BUG] [Swift6] Binary file uploads not working as expected #21444

@dockay

Description

@dockay

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Setup is with cocoapods currently (if this matters).

We have a problem making binary uploads work with our Backend (Java Spring Boot), as per definition appended here, this seems to be the most best practice to define a file upload. The generated code can't seem to differentiate between a binary file upload (mapFileBinaryToData: true) and a fileURL upload, it packs everything that is Data into an encoded base64 string.

The generated code generates this for file uploads:
Image

This will encode it asParameter and as it is Data he will then encode it to base64 string.

When the upload happens, it will then correctly create a FormDataEncoding-Object:
Image

But on the switch of key value, it will then go into the string part (as it is base64 encoded) instead of being Data as it is a binary upload.

Image

We didn't found a way to determine if we have binary data in this switch and all deterministic functions that help to get information if this base64 string is actually only Data seem not solid enough.

So is there any way that we can make "normal" application/json base64encoded Data uploads and uploads with
mapFileBinaryToData: true work in the same generated code?

openapi-generator version

7.13.0 with latest swif6 templates

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  contact:
    email: foo@bar.com
    name: name
  description: none
  title: test api
  version: 1.0.0
externalDocs:
  description: ''
  url: ''
servers:
  - url: '{protocol}://{environment}:{port}/someapi/v1'
paths:
  /test_request/upload_file:
    post:
      description: upload files
      operationId: uploadAttachements
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/uploadAttachements_request'
        required: true
      responses:
        '200':
          content: {}
          description: Success
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorInformation'
          description: Error
        '500':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorInformation'
          description: Error
      summary: upload files
components:
  schemas:
    ErrorInformation:
      description: ErrorInformation.
      properties:
        code:
          description: code.
          example: 1002
          format: int32
          type: integer
        message:
          description: text.
          example: Required value in FileMetaData is missing.
          type: string
      required:
        - code
        - message
      type: object
    uploadAttachements_request:
      properties:
        attachments:
          items:
            description: something.
            format: binary
            type: string
          type: array
      type: object
  
Generation Details
java -Dlog.level=warn \
        -jar openapi-generator-cli-7.13.0.jar generate \
        -g swift6 \
        --global-property skipFormModel=false \
        --ignore-file-override "${myDir}/Config/openapi-generator-ignore.txt"

openapi-generator-ignore.txt

**/*
!**/OpenAPIs/APIHelper.swift
!**/OpenAPIs/CodableHelper.swift
!**/OpenAPIs/Configuration.swift
!**/OpenAPIs/Extensions.swift
!**/OpenAPIs/JSONDataEncoding.swift
!**/OpenAPIs/JSONEncodingHelper.swift
!**/OpenAPIs/Models.swift
!**/OpenAPIs/OpenISO8601DateFormatter.swift
!**/OpenAPIs/OpenAPIDateWithoutTime.swift
!**/OpenAPIs/SynchronizedDictionary.swift
!**/OpenAPIs/URLSessionImplementations.swift
!**/OpenAPIs/APIs.swift
!**/OpenAPIs/APIs/*
!**/OpenAPIs/Models/*
!**/OpenAPIs/Infrastructure/*

Config

{
    "swiftUseApiNamespace": true,
    "nonPublicApi": true,
    "readonlyProperties" : true,
    "useJsonEncodable": false,
    "validatable": false,
    "interfaceModuleName" : "NetworkService",
    "useCustomDateWithoutTime": true,
    "enumUnknownDefaultCase": true,
    "useSPMFileStructure": false
}
Steps to reproduce
  • generated code
  • upload file via api
Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions