Swagger UI : "Choose file" button when input has both file AND json object?

Viewed 756

Asp.Net, .Net 5.0

I have this POST endpoint that requires to BOTH upload a file and a "complex" object (json). In order to test it, I use Swagger UI.

1. Testing the upload of a file alone is easy.

If the endpoint looks like this...

public ActionResult TestUpload(bool testBool, IFormFile file) {

...then Swagger UI looks like this :

enter image description here

2. Testing the upload of a json object alone is easy too.

public class DummyInput {
    public bool Field { get; set; }
}

If the endpoint looks like this...

public ActionResult TestUpload(bool testBool, DummyInput input) {

...then Swagger UI looks like this :

enter image description here

3. I can even make my life easier like this :

public ActionResult TestUpload(bool testBool, [FromForm] DummyInput input) {

enter image description here

That's great.

4. But what if I want BOTH the file and the 'input' json object ?

attempt #1 :

public ActionResult TestUpload(bool testBool, IFormFile file, DummyInput input) {

That obviously doesn't work because you cannot have two parameters "From body", there can be only one.

attempt #2:

public ActionResult TestUpload(bool testBool, [FromForm] IFormFile file, [FromForm] DummyInput input) {

enter image description here

attempt #3:

public class WrapperInput {
    public DummyInput Input { get; set; }
    public IFormFile File { get; set; }
}


public ActionResult TestUpload(bool testBool, WrapperInput input) {

enter image description here

So, in conclusion : how do I get to enter my nice fields AND my "Select file" button? I've looked at this article but it seems overly complicated -- and to be honest I'm not sure it's even related to my needs. I have a feeling that it's for OpenApi 2.0 rather than the better things we get from OpenApi 3.0.

1 Answers

I found the right combination of settings, I was so close :

public class WrapperInput {
    public DummyInput Input { get; set; }
    public IFormFile File { get; set; }
}

public ActionResult TestUpload(bool testBool, [FromForm] WrapperInput input) {

Then you get this (image below). Solved!

enter image description here

Related