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

北城以北 提交于 2019-12-06 08:14:06

问题


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'

回答1:


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.



来源:https://stackoverflow.com/questions/45027428/how-to-define-hashmapstring-listobject-property-in-swagger-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!