swagger-api / swagger-api/swagger-codegen

[C#] Stream file uploads instead of copy all bytes into memory

Open
#2,819 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Enhancement: General
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

ApiClient::ParameterToFile will copy complete file into ram before upload to rest service.

what about streaming a file to avoid OutOfMemory Exceptions on large files like:

public FileParameter ParameterToFile(string name, Stream stream)
        {
            var fp = new FileParameter
            {
                Writer = s =>
                {
                    using (StreamReader file = new StreamReader(stream))
                    {
                        file.BaseStream.CopyTo(s);
                    }
                },
                Name = name
            };


            if (stream is FileStream)
                fp.FileName = Path.GetFileName(((FileStream) stream).Name);
            else
                fp.FileName = "no_file_name_provided";
            return fp;
        }

current implementation:
ApiClient.mustache

if (stream is FileStream)
                return FileParameter.Create(name, ReadAsBytes(stream), Path.GetFileName(((FileStream)stream).Name));
            else
                return FileParameter.Create(name, ReadAsBytes(stream), "no_file_name_provided");

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache at ParameterToFile and compare its ReadAsBytes calls with the requested Stream-based approach. Trace how FileParameter is consumed during uploads, then verify that large streams are passed through without loading the complete file into memory while preserving the filename behavior shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.