Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using below code to upload file(IformFile) using swagger. I came to know that in new framework we must implement
IOperationFilter
.

Here is my code:

C#
<pre>using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace LoanCare.Services.Payment.Api.Filters;

/// <summary>
/// FileUpload Attribute
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class FileUploadAttribute : Attribute { }


/// <summary>
/// Filter for IFormFile
/// </summary>
public class FormFileOperationFilter : IOperationFilter
{
    private static readonly string[] FileParameters = { "contentType", "contentDisposition", "headers", "length", "name", "fileName" };
    private const string FormData = "multipart/form-data";
    private const string Json = "application/json";
    /// <summary>
    /// Apply method of IOperationFilter
    /// </summary>
    /// <param name="operation"></param>
    /// <param name="context"></param>
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        //request argument null check
        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (operation is null)
        {
            throw new ArgumentNullException(nameof(operation));
        }

        var attr = context.MethodInfo.GetCustomAttributes(true).OfType<FileUploadAttribute>().Any();
        if (!attr)
        {
            return;
        }

        var content = operation.RequestBody.Content;

        var jsonProperties = GetProperties(content, Json);
        foreach (var parameter in jsonProperties.Where(p => FileParameters.Contains(p.Key)))
        {
            jsonProperties.Remove(parameter);
        }
        if (jsonProperties.Count == 0)
        {
            content.Remove(Json);
        }

        var formDataProperties = GetProperties(content, FormData);
        formDataProperties.Add("file", new OpenApiSchema { Type = "file" });
    }

    private static IDictionary<string, OpenApiSchema> GetProperties(IDictionary<string, OpenApiMediaType> contents, string mediaType)
    {
        if (!contents.ContainsKey(mediaType))
        {
            contents.Add(mediaType, new OpenApiMediaType
            {
                Schema = new OpenApiSchema
                {
                    Type = "object",
                    Properties = new Dictionary<string, OpenApiSchema>()
                }
            });
        }
        return contents[mediaType].Schema.Properties;
    }
}


I have to write unit test for this file.

What I have tried:

I tried this article-
Swashbuckle.AspNetCore/AnnotationsOperationFilterTests.cs at master · domaindrivendev/Swashbuckle.AspNetCore · GitHub[^]
Posted
Comments
[no name] 10-Sep-22 11:14am    
You don't return any results; so there's no obvious way to check the results of the operation. You have to examine any objects that are impacted and confirm the results, if any. The fact it crashes if you pass a null argument does not make for "robust" testing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900