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");
}
}
}
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
}
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With