I am trying to get the swagger generated UI to allow users to test a web api method that contains a list of object for the parameter.
The web api method allows the caller to submit a batch of documents with a single batch ID and each document has it own description and byte array.

The "Document" class looks like the following:

public class Document
{
    public byte[] File { get; set; } // I use IFormFile but byte array is easier example to show for this demo

    public string Description { get; set; }   
}

The web API method looks like the following:

public async Task<FileUploadResult> UploadDocuments([FromQuery, BindRequired] int batchID, [FromForm] IList<Document> documents)
{
    // do some work and return some result
}

The default swagger generated document will have a Request Body of type "multipart/form-data" with a button "Add Object Item" that allows the user to add an array of File and Description.

{
  "File": "string",
  "Description": "string"
}

What I want is a UI with a field for "Description" and a button for the "File".

Here is the default swagger.
enter image description here

If I add an IOperationFilter and intercept the parameter of type IList, I can get close but it is not an array of Document.

var fileUploadParams = context.MethodInfo.GetParameters()
.Where(p => p.ParameterType == typeof(IList<Document>));

if (fileUploadParams.Any())
{
    operation.RequestBody = new OpenApiRequestBody
    {
        Content = new Dictionary<string, OpenApiMediaType>
        {
            ["multipart/form-data"] = new OpenApiMediaType
            {
                Schema = new OpenApiSchema
                {
                    Type = "object",
                    Properties = new Dictionary<string, OpenApiSchema>
                    {
                        ["Description"] = new OpenApiSchema
                        {
                            Type = "string"
                        },
                        ["File"] = new OpenApiSchema
                        {
                            Type = "array",
                            Items = new OpenApiSchema
                            {
                                Type = "file",
                                Description = "Upload Files"
                            }
                        }
                    }
                }
            }
        }
    };
}

Here is the swagger UI for the above code. NOTE: it does not have an array of Description and File.
enter image description here

Couple of questions.

  1. Is it possible to use the Document type in the OpenApiSchema rather than manually typing Description and File? This is ok but I would like the code to match the Document class. I did to use OpenApiReference where Type = ReferenceType.Schema and Id = "Document" but that only showed a json string. This is not a show stopper but move of a nice to have.
  2. Is it possible to have an array of Document object using the swagger UI for testing with it being a json string? I can't use the json string because of the byte array.

All this works fine from http clients, I just can't figure out how to get the swagger UI to show the array in the request body.

Here is a mockup photo of the desired result. enter image description here

0

There are 0 best solutions below