Skip to content

[BUG][CSHARP][GENERICHOST] - Array of string/binary for multi-file uploads generates invalid code #21380

Open
@sdukehart-omnesoft

Description

@sdukehart-omnesoft

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

When generating a C# client with the generichost library for an endpoint that accepts multiple file uploads (array of binary strings), the generated code is invalid. The generator creates code that attempts to add the entire collection as a single StreamContent instead of iterating through each file and adding them individually to the multipart content.

openapi-generator version

7.13.0

OpenAPI declaration file content or url

openapi: 3.0.0
info:
  title: File Upload API
  version: 1.0.0
  description: A minimal API with file upload functionality
servers:
  - url: http://localhost:8080
    description: Development server
paths:
  /upload:
    post:
      summary: Upload multiple files
      operationId: uploadFiles
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                files:
                  type: array
                  items:
                    type: string
                    format: binary
              required:
                - files
      responses:
        '200':
          description: Files uploaded successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                  fileCount:
                    type: integer

Generation Details

Using Docker:
  docker run --rm -v "${PWD}:/app" openapitools/openapi-generator-cli:v7.13.0 generate \
    -i /app/openapi.yaml \
    -g csharp \
    -o /app/generated \
    --additional-properties=targetFramework=net9.0,packageName=FileUploadClient,library=generichost
Or with config file:
  {
    "generatorName": "csharp",
    "outputDir": "./generated",
    "inputSpec": "./openapi.yaml",
    "additionalProperties": {
      "targetFramework": "net9.0",
      "packageName": "FileUploadClient",
      "library": "generichost",
      "nullableReferenceTypes": true,
      "useCollection": true,
      "useDateTimeOffset": true,
      "netCoreProjectFile": true
    }
  }

Steps to reproduce

  1. Create the OpenAPI spec file shown above
  2. Run the generator with the generichost library option
  3. Examine the generated DefaultApi.cs file around line 255

Related issues/PRs

Similar issues with file upload handling:

Suggest a fix

The issue is in the generated UploadFilesAsync method. The current code generates:

  multipartContentLocalVar.Add(new StreamContent(files));

But it should generate:

  foreach (var file in files)
  {
      var streamContent = new StreamContent(file);
      streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
      {
          Name = "\"files\"",
          FileName = "\"file\""
      };
      multipartContentLocalVar.Add(streamContent);
  }

The template that needs modification is likely in modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ - specifically the api template that handles multipart form data with array parameters.

Report generated with the help of Claude

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