Jaxidian
Jaxidian

Reputation: 13509

How can I use a Dictionary parameter with Swagger UI?

I'm looking to use Swashbuckle to generate Swagger docs and Swagger UI for a Web API method that looks like the following (it's effectively a mail-merge with PDFs):

    [HttpPost]
    [ResponseType(typeof(byte[]))]
    public HttpResponseMessage MergeValues([FromUri]Dictionary<string, string> mergeValues,
                                           [FromBody]byte[] pdfTemplate)
    {
        return MergeStuff();
    }

This isn't currently working, or at least I'm not sure how to interact with the resulting Swagger UI.

This creates the following Swagger UI, which seems reasonable, but I'm not sure what to do with it to populate it correctly (or if it's even correct). I'm using pretty much all default Swashbuckle settings from the latest Nuget.

Byte Array: If I enter Base64-encoded text for the byte array, the byte array always shows up null. Turns out I just need my BASE64 text surrounded by double-quotes and then it works.

Dictionary: I've tried various types of JSON expressions (arrays, objects, etc) and am unable to get any of the values in the Dictionary to populate (the resulting Dictionary object has 0 items).

enter image description here

I have the ability to change things and would like to know how I can do this. For example, if changing the dictionary to an array of KeyValuePair<string,string> helps, let's do it.

Options I know that I have that I'd like to avoid:

  1. Changing these input types to strings and doing my own manual deserialization/decoding. I'd like to be explicit with my types and not get too fancy.
  2. Custom binders. I'd like to utilize standard/default binders so I have less code/complexity to maintain.

Upvotes: 4

Views: 8291

Answers (1)

Jaxidian
Jaxidian

Reputation: 13509

My question really was a two-parter. Here are the answers, although only a partial answer to the second question:

Question 1: How do I fill in the data for a byte array?

Answer 1: Paste in your base64-encoded value for it but be sure to surround that content with double-quotes, both at the beginning and end.

Question 2: How do I fill in the data for the Dictionary?

Answer 2: While it doesn't work with [FromUri], this will work with [FromBody] (either Dictionary or IDictionary<string,string> will work):

{"FirstName":"John","LastName":"Doe"}

I'm not sure why this doesn't work with FromUri but I'm going to ask a separate question that's much more focused than this one to get to the bottom of that. For the purposes of this question, both parameters can be put into a DTO, flagged as [FromBody], and all is good then.

Upvotes: 3

Related Questions