4

I'm building a web api using asp.net webapi 2 and for my documentation i've chosen to go with swagger.

The problem i'm having is that one of my api methods takes a nested array (array of strings inside another array). When testing manually in postman it works great when i type something like this:

http:/localhost:port/setup?array[0]=key1&array[0]=value1&array[1]=key2&array[1]=value2

But in swagger-ui i get one field to type in, with only one parameter per row. And that makes me unable to enter two values for each nested array.

Result should look like this (json):

[["key1","value1"],["key2","value2"]]

Which i have been able to achieve with the query parameters above. While i can only achieve the following in the swagger-ui text field:

[["key1"],["value1"],["key2"],["value2"]]

I have vacuumed the internet for 1-2 hours for an answare but can only find posts asking how to define a nested array in the yaml file. Some errands i've read on the swagger github leaves me thinking that it isn't possible at all. And while it's not a critical feature it would be really nice if all of the tests worked as they should.

So the question is, if possible, how do i type two separate strings in a nested array in swagger-ui.

I am not a pro in any of this stuff. I learned how to use json api's and swagger, all of it this week so please take that in mind while reviewing this.

Thanks in advance!

2
  • If it's key=value pairs, why use an array and not an object/dictionary? {"key1": "value2", "key2": "value2"} Commented Aug 22, 2017 at 19:25
  • In the end i changed it to a list of objects containing all the data for the parameter. Commented Aug 24, 2017 at 11:12

1 Answer 1

3

A nested array is simple:

type: array
items:
  type: array
  items: 
    type: string

But OpenAPI/Swagger does not have a way to serialize nested arrays in the query string like in your example.

If the array has a fixed number of items, a possible workaround is to define each nested array as a separate query parameter:

paths:
  /setup:
    get:
      parameters:
        - in: query
          name: array[0]
          required: true
          type: array
          items:
            type: string
          collectionFormat: multi   # array[0]=key1&array[0]=value1
        - in: query
          name: array[1]
          required: true
          type: array
          items:
            type: string
          collectionFormat: multi   # array[1]=key2&array[1]=value2

A better approach would be to use a POST request and pass the array in the request body:

paths:
  /setup:
    post:
      consumes:
        - application/json
      parameters:
        - in: body
          name: body
          required: true
          schema:
            type: array
            items:
              type: array
              items: 
                type: string
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up with posting the whole object i wanted to pass in the request body like you just explained. Thank you for your answer, now other people can find at least one answer of it on the internet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.