Skip to content

Conversation

@devhl-labs
Copy link
Contributor

@devhl-labs devhl-labs commented Jan 25, 2026

Fix MultipartFormDataContent Generation for File Uploads in C# GenericHost

Summary

This PR fixes critical issues with multipart file upload code generation in the C# GenericHost library. The generated code now correctly handles single files, arrays of files, and mixed form parameters.

Closes

Changes

Issues Fixed

  1. Content-Type Header: Changed from MultipartContent (multipart/mixed) to MultipartFormDataContent (multipart/form-data) to match OpenAPI specifications

  2. Array of Files Support: Added proper iteration logic for file parameters that are arrays/containers

    • Single files are added directly to multipart content
    • File arrays are iterated and each file is added individually
  3. Proper Field Names: Files now use their actual parameter names instead of hardcoded values, allowing servers to correctly parse form fields

  4. Conditional FormUrlEncodedContent: Only adds FormUrlEncodedContent when there are actual non-file form parameters, preventing empty content additions

  5. Content Assignment Order: httpRequestMessageLocalVar.Content is now assigned after the multipart content is fully populated, improving code clarity and reliability

Generated Code Behavior

Before:

MultipartContent multipartContentLocalVar = new MultipartContent();
httpRequestMessageLocalVar.Content = multipartContentLocalVar;
multipartContentLocalVar.Add(files);  // Adds entire collection as single content
multipartContentLocalVar.Add(new FormUrlEncodedContent(emptyList));  // Unnecessary

After:

MultipartFormDataContent multipartContentLocalVar = new MultipartFormDataContent();
foreach (var file in files)
{
    var streamContentLocalVar = new StreamContent(file);
    multipartContentLocalVar.Add(streamContentLocalVar, "fieldName");
}
if (formParameters.Count > 0)
    multipartContentLocalVar.Add(new FormUrlEncodedContent(formParameters));
httpRequestMessageLocalVar.Content = multipartContentLocalVar;

Testing

Generated code now:

  • ✅ Compiles without errors for file upload endpoints
  • ✅ Correctly handles single file parameters
  • ✅ Correctly handles array of files parameters
  • ✅ Properly sets multipart/form-data content type
  • ✅ Includes correct field names for form parsing
  • ✅ Only adds form parameters when necessary

Files Modified

  • modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache - Updated file parameter handling in request building logic

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds correct multipart/form-data handling and multi-file upload support to C# GenericHost clients. Fixes broken file parameter generation and updates tests/samples to validate single and multiple file uploads. Fixes #21380 and #21449.

  • New Features

    • Support arrays/collections of files by iterating and adding each file with the correct field name.
    • Added a new spec endpoint (/pet/{petId}/uploadImages) and regenerated C# samples to exercise multi-file uploads.
  • Bug Fixes

    • Use MultipartFormDataContent instead of MultipartContent to send multipart/form-data.
    • Add FormUrlEncodedContent only when non-file form fields are present.
    • Set field names from parameter names.

Written for commit ff5bd0b. Summary will update on new commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant