4

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.

Arindam Choudhury
  • 2,816
  • 2
  • 18
  • 22
  • What have you tried? How did that not work? See if this helps: [Swagger HashMap property type](https://stackoverflow.com/q/16042885/113116), [Swagger: map of ](https://stackoverflow.com/q/36136885/113116) – Helen Feb 05 '18 at 14:53
  • So you have a `` map. Check out the answers to the linked questions in my previous comment. Those answers are for OpenAPI 2.0, so replace `definitions` with `components.schemas` to adjust for the 3.0 syntax. – Helen Feb 05 '18 at 15:16
  • @helen I have updated the question. Can you please check if I am doing it right? – Arindam Choudhury Feb 05 '18 at 15:58

1 Answers1

4

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'
Helen
  • 87,344
  • 17
  • 243
  • 314