Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define HashMap<String, List<Object>> property in swagger yml?

I am using swagger to generate classes in Java and Type script. I have problem defining map property with list of objects as value. I tried to define as follows:

DataMap
type: object
additionalProperties:
 #type: array -- This config does not work.
  $ref: '#/definitions/Data'

Above yml definition generated following code in java:

  class DataMap extends HashMap<String, Data> {
    }

How can I configure yml to generate key with list of data ? something like following class:

 class DataMap extends HashMap<String, List<Data>> {
        }

or

 class DataInfo {
     Map<String, List<Data>> dataMap;
   }

Is this possible with swagger 2.0? I am thinking of defining another DataList class which extends ArrayList then use this class as value to Map.

--------------UPDATE & ANSWER-----------

Thanks @nickb

I am using swagger-codegen-maven-plugin version 2.2.1 and yml definition to generate map as follows:

 DataInfo
    type: object
    properties:
    dataMap:
     type: object
     additionalProperties:
        type: array 
        items:
          $ref: '#/definitions/Data'
like image 341
nayakam Avatar asked Jul 11 '17 06:07

nayakam


1 Answers

I'm using Swagger codegen v2.1.6 with the following model definitions:

 foo:
    properties:
      baz:
        type: string
  bar:
    properties:
      map:
        type: object
        additionalProperties:
          type: array
          items:
            $ref: '#/definitions/foo'

This generates a Bar Java class with the following field:

Map<String, List<Foo>> map = new HashMap<String, List<Foo>>();

If you're seeing different behavior, you could've stumbled into a regression. Try testing earlier versions, or specifically see if 2.1.6 correctly generates this model.

like image 183
nickb Avatar answered Oct 22 '22 12:10

nickb