Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a map in swagger?

I'm working on an API which also generates swagger documentation. The issue is that for some reasons the request model/schema is not displayed in swagger UI but I don't get any error either. I need to represent map to an array of strings . e.g. map[string][]string. Definition object definition is below.

{
  "definitions": {
    "versions": {
      "type": "string",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    }
  }
}
like image 826
The user with no hat Avatar asked Feb 21 '15 10:02

The user with no hat


People also ask

How do you describe a map in swagger?

A dictionary (also known as a map, hashmap or associative array) is a set of key/value pairs. OpenAPI lets you define dictionaries where the keys are strings. To define a dictionary, use type: object and use the additionalProperties keyword to specify the type of values in key/value pairs.

What is operationId in swagger?

operationId is an optional unique string used to identify an operation. If provided, these IDs must be unique among all operations described in your API. /users: operationId: getUsers.

What is oas3 in swagger?

OAS 3.0, which is based on the original Swagger 2.0 specification, is meant to provide a standard format to unify how an industry defines and describes RESTful APIs.


1 Answers

The support for maps is still not available in the UI - https://github.com/swagger-api/swagger-ui/issues/913.

You'd also want to change your definitions like this:

{
  "definitions": {
    "versions": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    }
  }
}

To be clear, this defines a map where the values are arrays of strings.

like image 55
Ron Avatar answered Sep 26 '22 11:09

Ron