OpenAPITools / OpenAPITools/openapi-generator

[C#] csharp client support for 'string/binary'

Open
#1,381 10 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When generating code from a spec that has format: binary and type: string using the csharp client, the generated code builds an interface with System.IO.Stream but this usage pattern is not supported by restsharp, including the latest version.

openapi-generator version

Main branch.

OpenAPI declaration file content or url

Partial content:

"paths": {
   "/op": {
       "parameters": [
              {
                  "name": "data",
                  "in": "body",
                  "description": "Binary body data",
                  "required": true,
                  "schema": {
                      "type": "string",
                      "format": "binary"
                   }
              }
      ]
  }
}
Command line used for generation

java -jar openapi-generator-cli.jar generate -i swagger.json -l csharp -o ClientAPI -c openapi-generation-parameters.json

Steps to reproduce

Generate client, then either inspect code or try to use it. Trying to run results in an exception of type package_.Client.ApiException:
Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.

Related issues/PRs

#934 for cpprest client
#1327 for aspdotnetcore server

Suggest a fix/enhancement

Internally, it appears that the type used is file. This is translated to a ''System.IO.Stream`` type in AbstractCSharpCodegen.java around line 172:

        typeMapping.put("file", "System.IO.Stream");

In the generated code, the method contains this code:

            if (body != null && body.GetType() != typeof(byte[]))
            {
                localVarPostBody = this.Configuration.ApiClient.Serialize(body); // http body (model) parameter
            }
            else
            {
                localVarPostBody = body; // byte array
            }

The localVarPostBody is eventually passed to PrepareRequest, which adds it as a parameter:

            if (postBody != null) // http body (model or byte[]) parameter
            {
                request.AddParameter(contentType, postBody, ParameterType.RequestBody);
            }

AddParameter is a method in restsharp object RestRequest. A type of System.IO.Stream is not supported here; the AddParameter method attempts to serialize the Stream object itself instead of the stream's contents.

Easiest fix is to not support binary file streams, and translate this specification to a byte array (byte []), just like a string/byte OpenAPI type. This would require an override in the CSharpClientCodegen.java code.

Actually streaming binary files with restsharp appears to be harder, as a web search only turns up sending byte arrays or files as MIME multi-part (i.e. base64 encoded). Neither apply here if a stream is to be used.

Note that while this would be a breaking change for the generated code, given that the current generated code doesn't work it's pretty unlikely anyone's using it.

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 with AbstractCSharpCodegen.java and CSharpClientCodegen.java, then inspect the generated request path through PrepareRequest and RestRequest.AddParameter. Compare the current System.IO.Stream mapping with the proposed byte-array behavior and verify that a generated client can send the binary body without RestSharp serializing the stream object or raising the reported exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.