[BUG] CSharp code generator logic error
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/
Sorry I don't understand. What is boundary? What spec are you using? Show the actual code vs expected code.
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.
It would be helpful if you could provide the actual c# and the expected c#
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?
@wing328 please reopen