Search code examples
swaggerswagger-editoropenapi

swagger 3: schema for dictionary of dictionaries


I want to model a schema where response is dictionary of dictionaries:

{
  'id1': {
    'type': 'type1',
    'active': true,
  }, 
  'id2': {
    'type': 'type2',
    'active': false,
  },
  ... 
  ... 
}

I have defined:

components:
  schemas:
    accountData:
      type: object
      required:
        - type
        - active
      properties:
        type:
          type: string
        active:
          type: boolean

    accounts:
      type: object
      additionalProperties:
        type: object
        properties:
          account-id:
            type: object
            $ref: '#/components/schemas/accountData'

Now I want to define '#/components/schemas/accounts' which is a repeating dictionary of '#/components/schemas/account'.

I am very new to OpenApi 3, I am unable to figure out how to do it.


Solution

  • You are almost there. The schema specified in additonalProperties corresponds to the value type in the <key, value> dictionary. In your example, the values are accountData objects, so you should use:

    components:
      schemas:
        ...
    
        accounts:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/accountData'