1

In my code I use endpoint:

[Authorize]
[HttpPost("UploadAlbum")]
public async Task<ResponseHolderModel<UploadAlbumResponseModel>> UploadAlbum([FromForm]List<FileHolderModel> files)

This is FileHolderModel class:

public class FileHolderModel
{
    public IFormFile? File { get; set; }
    public float Price { get; set; }
}

My problem Is that I want to be abble to use this endpoint by swagger UI and post files with prices easily using UI. I tried to force swagger to recognize files to append filepicker by:

public void Apply(OpenApiOperation operation, OperationFilterContext context)  
{  
    var fileUploadMime = "multipart/form-data";  
    if (operation.RequestBody == null || !operation.RequestBody.Content.Any(x => x.Key.Equals(fileUploadMime, StringComparison.InvariantCultureIgnoreCase)))  
        return;  

    var fileParams = context.MethodInfo.GetParameters().Where(p => p.ParameterType == typeof(List<FileHolderModel>));  
    operation.RequestBody.Content[fileUploadMime].Schema.Properties =  
        fileParams.ToDictionary(k => k.Name, v => new OpenApiSchema()  
        {  
            Type = "string",  
            Format = "binary"  
        });  
} 

Unfortunately I am lost in OpenApiSchema. Could you help me how to write this piece of code to fit List<FoleHolderModel where File should be selectable by filepicker? If you know some other and better way how to do it write an answer please.

1
  • I was able to run it using postman. Did you find a solution to your problem using swagger?
    – Nir
    Commented Sep 18, 2022 at 7:37

0

Browse other questions tagged or ask your own question.