I can't use NonBodyParameter in .net 6

70 Views Asked by At

I'm migrating an application from .net framewok 4.1 to .net 6, but I've found it difficult to use "OpenApiOperation", in .net6 I can't find "NonBodyParameter" or "Consumes".

public class FileOperationFilter : IOperationFilter
{
 
     private const string FormDataMimeType = "multipart/form-data";
     private static readonly string[] FormFilePropertyNames =
         typeof(IFormFile).GetTypeInfo().DeclaredProperties.Select(x => x.Name).ToArray();
 
     public void Apply(OpenApiOperation operation, OperationFilterContext context)
     {
         if (context.ApiDescription.ParameterDescriptions.Any(x => x.ModelMetadata != null && x.ModelMetadata.ContainerType == typeof(IFormFile)))
         {
             var formFileParameters = operation
                 .Parameters
                 .OfType<NonBodyParameter>()
                 .Where(x => FormFilePropertyNames.Contains(x.Name))
                 .ToArray();
             var index = operation.Parameters.IndexOf(formFileParameters.First());
             foreach (var formFileParameter in formFileParameters)
             {
                 operation.Parameters.Remove(formFileParameter);
             }
 
             var formFileParameterName = context
                 .ApiDescription
                 .ActionDescriptor
                 .Parameters
                 .Where(x => x.ParameterType == typeof(IFormFile))
                 .Select(x => x.Name)
                 .First();
             var parameter = new NonBodyParameter()
             {
                 Name = formFileParameterName,
                 In = "formData",
                 Description = "The file to upload.",
                 Required = true,
                 Type = "file"
             };
             operation.Parameters.Insert(index, parameter);
 
             if (!operation.Consumes.Contains(FormDataMimeType))
             {
                 operation.Consumes.Add(FormDataMimeType);
             }
         }
     }
}

I couldn't find any help in the documentation

0

There are 0 best solutions below