private static void AppendGetMultipartFormDataContentRequestMethod( StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, bool useIFormFileDirectly) { var modelSchema = endpointMethodMetadata.GetRequestBodySchema() !; var modelName = modelSchema.GetModelName(); sb.AppendLine(); sb.AppendLine( 8, string.IsNullOrEmpty(modelName) ? "private async Task<MultipartFormDataContent> GetMultipartFormDataContentFromFiles(List<IFormFile> request)" : $"private async Task<MultipartFormDataContent> GetMultipartFormDataContentFrom{modelName}({modelName} request)"); sb.AppendLine(8, "{"); sb.AppendLine(12, "var formDataContent = new MultipartFormDataContent();"); if (modelSchema.IsTypeArray()) { sb.AppendLine(12, "if (request is not null)"); sb.AppendLine(12, "{"); sb.AppendLine(16, "foreach (var item in request)"); sb.AppendLine(16, "{"); sb.AppendLine(20, "var bytesContent = new ByteArrayContent(await item.GetBytes());"); sb.AppendLine(20, "formDataContent.Add(bytesContent, \"Request\", item.FileName);"); sb.AppendLine(16, "}"); sb.AppendLine(12, "}"); sb.AppendLine(); } else { foreach (var schemaProperty in modelSchema.Properties) { var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper(); if (schemaProperty.Value.IsFormatTypeBinary()) { sb.AppendLine(12, $"if (request.{propertyName} is not null)"); sb.AppendLine(12, "{"); sb.AppendLine(16, $"var bytesContent = new ByteArrayContent(await request.{propertyName}.GetBytes());"); sb.AppendLine( 16, useIFormFileDirectly ? $"formDataContent.Add(bytesContent, \"Request.{propertyName}\", request.FileName);" : $"formDataContent.Add(bytesContent, \"Request.{propertyName}\", request.{propertyName}.FileName);"); sb.AppendLine(12, "}"); sb.AppendLine(); } else if (schemaProperty.Value.HasItemsWithFormatTypeBinary()) { sb.AppendLine(12, $"if (request.{propertyName} is not null)"); sb.AppendLine(12, "{"); sb.AppendLine(16, $"foreach (var item in request.{propertyName})"); sb.AppendLine(16, "{"); sb.AppendLine(20, "var bytesContent = new ByteArrayContent(await item.GetBytes());"); sb.AppendLine(20, $"formDataContent.Add(bytesContent, \"Request.{propertyName}\", item.FileName);"); sb.AppendLine(16, "}"); sb.AppendLine(12, "}"); sb.AppendLine(); } else if (schemaProperty.Value.IsTypeArray()) { sb.AppendLine(12, $"if (request.{propertyName} is not null && request.{propertyName}.Count > 0)"); sb.AppendLine(12, "{"); sb.AppendLine(16, $"foreach (var item in request.{propertyName})"); sb.AppendLine(16, "{"); sb.AppendLine(20, $"formDataContent.Add(new StringContent(item), \"Request.{propertyName}\");"); sb.AppendLine(16, "}"); sb.AppendLine(12, "}"); sb.AppendLine(); } else { sb.AppendLine(12, $"formDataContent.Add(new StringContent(request.{propertyName}), \"Request.{propertyName}\");"); } } } sb.AppendLine(12, "return formDataContent;"); sb.AppendLine(8, "}"); }