Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger HashMap property type

Tags:

Is there any way to define a HashMap or Generic Object type in the models section? I have a REST service that returns products and those products can have different options. The options property are basically a HashMap, where the id is the option name and its value is the option value.

like image 680
jplaza Avatar asked Apr 16 '13 17:04

jplaza


People also ask

How does swagger define HashMap?

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 are swagger properties?

The properties keyword is used to define the object properties – you need to list the property names and specify a schema for each property.

Is HashMap and dictionary same?

In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary. You are advised to use the HashMap though.

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.


1 Answers

Yes it's possible.

In OpenAPI (fka. Swagger) 2.0 and 3.0, a hashmap is always a <string, something> map:

  • The key is always a string and do not need to be defined.
  • The value type is what you want and is defined with additionalProperties.

Let's say you want to describe a <string, string> hashmap like this one:

{
  "key1": "value1",
  "key2": "value2"
}

The corresponding OpenAPI 2.0 definition will be:

definitions:
  StringStringMap:
    type: object
    additionalProperties:
      type: string

In OpenAPI 3.0 the definition will be:

components:
  schemas:
    StringStringMap:
      type: object
      additionalProperties:
        type: string


A <string, object> hashmap like this

{
  "key1": {"someData": "data", "someOtherData": true},
  "key2": {"someData": "data2", "someOtherData": false}
}

will be defined this way in OpenAPI 2.0:

definitions:
  ComplexObject:
    type: object
    properties:
      someData:
        type: string
      someOtherData:
        type: boolean

  StringObjectMap:
    type: object
    additionalProperties:
      $ref: "#/definitions/ComplexObject"

and in OpenAPI 3.0:

components:
  schemas:
    ComplexObject:
      type: object
      properties:
        someData:
          type: string
        someOtherData:
          type: boolean

    StringObjectMap:
      type: object
      additionalProperties:
        $ref: "#/definitions/ComplexObject"

I've just covered this in depth in part 4 of my OpenAPI (fka Swagger tutorial).

The OpenAPI (fka. Swagger) specification explains briefly this too.

like image 170
Arnaud Lauret Avatar answered Sep 19 '22 07:09

Arnaud Lauret