Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload with Swagger 5.0.0-rcX and .Net-Core 3

I am using .NetCore 3 and Swagger 5.0.0-rc4. I am trying to upload file(image) using Swagger but it does not work because the apply method in the IOperationFilter or even Swashbuckle.AspNetCore.Swagger are missing some attributes. For instance NonBodyParameter and Consumes do not exit in Swagger 5.0

Do anyone use face the same problem or tried to solve it?

public class FileOperationFilter : IOperationFilter
    {

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.OperationId.ToLower() == "apivaluesuploadpost")
            {
                operation.Parameters.Clear();
                operation.Parameters.Add(new **NonBodyParameter**
                {
                    Name = "uploadedFile",
                    In = "formData",
                    Description = "Upload File",
                    Required = true,
                    Type = "file"
                });
                operation.**Consumes**.Add("multipart/form-data");
            }
        }
    }
like image 886
SamiR Avatar asked Nov 06 '22 11:11

SamiR


1 Answers

AS for the missing Parameters Now these are changed to OpenApiParameter and OpenApiOperation.

public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.OperationId == "MyOperation")
            {
                operation.Parameters.Clear();
                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = "formFile",
                    In = ParameterLocation.Header,
                    Description = "Upload File",
                    Required = true,
                    Schema= new OpenApiSchema
                    {
                        Type="file",
                        Format="binary"
                    }
                }); 
                var uploadFileMediaType = new OpenApiMediaType()
                {
                    Schema = new OpenApiSchema()
                    {
                        Type = "object",
                        Properties =
                    {
                        ["uploadedFile"] = new OpenApiSchema()
                        {
                            Description = "Upload File",
                            Type = "file",
                            Format = "binary"

                        }
                    },
                        Required = new HashSet<string>()
                    {
                        "uploadedFile"
                    }
                    }
                };
                operation.RequestBody = new OpenApiRequestBody
                {
                    Content =
                {
                    ["multipart/form-data"] = uploadFileMediaType
                }
                };


            }
        }
like image 93
ayush garg Avatar answered Nov 12 '22 11:11

ayush garg