openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] CSharp code generator logic error

Open lld1995 opened this issue 6 months ago • 5 comments

I got boundary loss response when formdata and i check the generation code, i saw these code `string[] contentTypes = new string[] { "multipart/form-data" };

string contentTypeLocalVar = ClientUtils.SelectHeaderContentType(contentTypes);

if (contentTypeLocalVar != null && httpRequestMessageLocalVar.Content != null) httpRequestMessageLocalVar.Content.Headers.ContentType = new MediaTypeHeaderValue(contentTypeLocalVar);`

It reset the MultipartContent's ContentType ,but MultipartContent auto generate boundary in it and had set boundary.It causes boundary loss in ContentType.

Verison: openapi-generator-cli 7.14.0-SNAPSHOT commit : https://github.com/OpenAPITools/openapi-generator/commit/5677f5b09bc7ff293f0a4ac58a68e4f437bdf918 built : -999999999-01-01T00:00:00+18:00 source : https://github.com/openapitools/openapi-generator docs : https://openapi-generator.tech/

lld1995 avatar Jun 05 '25 06:06 lld1995

Sorry I don't understand. What is boundary? What spec are you using? Show the actual code vs expected code.

devhl-labs avatar Jun 05 '25 13:06 devhl-labs

I'm not sure why this issue was closed, this is definitely still a problem. A client generated using this very simple OpenAPI Spec results in an sdk that looks like this.

Ultimately, this sends a request that has a content type header of "multipart/form-data" (with no boundary attribute as described here). This causes problems at leas in ASP.NET core which parses the boundary attribute and fails if it is not present.

grmoon avatar Jul 21 '25 20:07 grmoon

It would be helpful if you could provide the actual c# and the expected c#

devhl-labs avatar Jul 21 '25 20:07 devhl-labs

I am encountering the same issue. The actual C# provided is (generator version 7.17.0):

MultipartContent multipartContentLocalVar = new MultipartContent();

List<KeyValuePair<string?, string?>> formParameterLocalVars = new List<KeyValuePair<string?, string?>>();

multipartContentLocalVar.Add(new FormUrlEncodedContent(formParameterLocalVars)); multipartContentLocalVar.Add(new StreamContent(file));

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

string[] contentTypes = new string[] {
    "multipart/form-data"
};

string? contentTypeLocalVar = ClientUtils.SelectHeaderContentType(contentTypes);

if (contentTypeLocalVar != null && httpRequestMessageLocalVar.Content != null)
    httpRequestMessageLocalVar.Content.Headers.ContentType = new MediaTypeHeaderValue(contentTypeLocalVar);

The main problematic line seems to be the last one which sets the content type in the header but does not include the necessary boundary data. It should look something like this:

Method: POST, RequestUri: [REDACTED], Version: 1.1, Content: System.Net.Http.MultipartFormDataContent, Headers:
{
  <...other stuff...>
  Content-Type: multipart/form-data; boundary="[REDACTED]"
}

But rather it generates something like this:

  Content-Type: multipart/form-data

For my use-case I have replaced the generated c# section above with the following:

MultipartFormDataContent multipartContentLocalVar = new MultipartFormDataContent();
httpRequestMessageLocalVar.Content = multipartContentLocalVar;
# NOTE: The "file" and fileName are added for my own use case, don't think they are needed in the general case
multipartContentLocalVar.Add(new StreamContent(file), "file", fileName);
httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Issue seems similar to this: https://github.com/OpenAPITools/openapi-generator/issues/20347 Can this be reopened?

nox-cadmatic avatar Nov 27 '25 15:11 nox-cadmatic

@wing328 please reopen

devhl-labs avatar Nov 27 '25 16:11 devhl-labs