Skip to content

Commit

Permalink
Fixes body of FormParameters in multipart/form-data requests without …
Browse files Browse the repository at this point in the history
…files sent by generated C# (RestSharp) (#20351)

* fix: set RestRequest flag to force multipart/form-data when given as Content-Type

* fix: removed serialization of FormParameters for multipart/form-data

* updated csharp-restsharp samples
  • Loading branch information
lewishazell authored Dec 22, 2024
1 parent ab8d359 commit ff0fe26
Show file tree
Hide file tree
Showing 67 changed files with 448 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ namespace {{packageName}}.Client
}
}
if (options.HeaderParameters != null)
{
if (options.HeaderParameters.TryGetValue("Content-Type", out var contentTypes) && contentTypes.Any(header => header.Contains("multipart/form-data")))
{
request.AlwaysMultipartFormData = true;
}
}
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ namespace {{packageName}}.{{apiPackage}}
};

var localVarContentType = {{packageName}}.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -375,7 +376,7 @@ namespace {{packageName}}.{{apiPackage}}
{{/isArray}}
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}})); // form parameter
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{#isPrimitiveType}}{{packageName}}.Client.ClientUtils.ParameterToString({{paramName}}){{/isPrimitiveType}}{{^isPrimitiveType}}localVarMultipartFormData ? {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}}) : {{packageName}}.Client.ClientUtils.Serialize({{paramName}}){{/isPrimitiveType}}); // form parameter
{{/isFile}}
{{/required}}
{{^required}}
Expand All @@ -397,7 +398,7 @@ namespace {{packageName}}.{{apiPackage}}
{{/isArray}}
{{/isFile}}
{{^isFile}}
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{packageName}}.Client.ClientUtils.{{#isPrimitiveType}}ParameterToString{{/isPrimitiveType}}{{^isPrimitiveType}}Serialize{{/isPrimitiveType}}({{paramName}})); // form parameter
localVarRequestOptions.FormParameters.Add("{{baseName}}", {{#isPrimitiveType}}{{packageName}}.Client.ClientUtils.ParameterToString({{paramName}}){{/isPrimitiveType}}{{^isPrimitiveType}}localVarMultipartFormData ? {{packageName}}.Client.ClientUtils.ParameterToString({{paramName}}) : {{packageName}}.Client.ClientUtils.Serialize({{paramName}}){{/isPrimitiveType}}); // form parameter
{{/isFile}}
}
{{/required}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestAuthHttpBasicWithHttpInfo
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -412,6 +413,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestAuthHttpBearerWithHttpInf
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ public System.IO.Stream TestBinaryGif(int operationIndex = 0)
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -792,6 +793,7 @@ public System.IO.Stream TestBinaryGif(int operationIndex = 0)
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -932,6 +934,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1078,6 +1081,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1218,6 +1222,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1352,6 +1357,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1486,6 +1492,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1620,6 +1627,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1754,6 +1762,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1888,6 +1897,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestBodyMultipartFormdataArra
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -532,6 +533,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestFormObjectMultipartWithHt
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand All @@ -543,7 +545,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestFormObjectMultipartWithHt
localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
}

localVarRequestOptions.FormParameters.Add("marker", Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter
localVarRequestOptions.FormParameters.Add("marker", localVarMultipartFormData ? Org.OpenAPITools.Client.ClientUtils.ParameterToString(marker) : Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter

localVarRequestOptions.Operation = "FormApi.TestFormObjectMultipart";
localVarRequestOptions.OperationIndex = operationIndex;
Expand Down Expand Up @@ -682,6 +684,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestFormObjectMultipartWithHt
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public Org.OpenAPITools.Client.ApiResponse<string> TestsPathStringPathStringInte
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -843,6 +844,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1005,6 +1007,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1163,6 +1166,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1347,6 +1351,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1483,6 +1488,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1621,6 +1627,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1759,6 +1766,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -1897,6 +1905,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -2035,6 +2044,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ private RestRequest NewRequest(
}
}

if (options.HeaderParameters != null)
{
if (options.HeaderParameters.TryGetValue("Content-Type", out var contentTypes) && contentTypes.Any(header => header.Contains("multipart/form-data")))
{
request.AlwaysMultipartFormData = true;
}
}

return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down Expand Up @@ -497,6 +498,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand All @@ -511,12 +513,12 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
localVarRequestOptions.FormParameters.Add("status", Org.OpenAPITools.Client.ClientUtils.ParameterToString(status)); // form parameter
if (marker != null)
{
localVarRequestOptions.FormParameters.Add("marker", Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter
localVarRequestOptions.FormParameters.Add("marker", localVarMultipartFormData ? Org.OpenAPITools.Client.ClientUtils.ParameterToString(marker) : Org.OpenAPITools.Client.ClientUtils.Serialize(marker)); // form parameter
}
localVarRequestOptions.FileParameters.Add("file", file);
if (statusArray != null)
{
localVarRequestOptions.FormParameters.Add("statusArray", Org.OpenAPITools.Client.ClientUtils.Serialize(statusArray)); // form parameter
localVarRequestOptions.FormParameters.Add("statusArray", localVarMultipartFormData ? Org.OpenAPITools.Client.ClientUtils.ParameterToString(statusArray) : Org.OpenAPITools.Client.ClientUtils.Serialize(statusArray)); // form parameter
}

localVarRequestOptions.Operation = "MultipartApi.MultipartMixed";
Expand Down Expand Up @@ -657,6 +659,7 @@ public Org.OpenAPITools.Client.ExceptionFactory ExceptionFactory
};

var localVarContentType = Org.OpenAPITools.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
var localVarMultipartFormData = localVarContentType == "multipart/form-data";
if (localVarContentType != null)
{
localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ private RestRequest NewRequest(
}
}

if (options.HeaderParameters != null)
{
if (options.HeaderParameters.TryGetValue("Content-Type", out var contentTypes) && contentTypes.Any(header => header.Contains("multipart/form-data")))
{
request.AlwaysMultipartFormData = true;
}
}

return request;
}

Expand Down
Loading

0 comments on commit ff0fe26

Please sign in to comment.