如何定义HashMap< String,List< Object>> swagger yml的财产? [英] How to define HashMap<String, List<Object>> property in swagger yml?

查看:417
本文介绍了如何定义HashMap< String,List< Object>> swagger yml的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swagger在Java和Type脚本中生成类。
我有定义map属性的问题,对象列表作为值。我试着定义如下:

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'

以下代码生成的yml定义java:

Above yml definition generated following code in java:

  class DataMap extends HashMap<String, Data> {
    }

如何配置yml以生成包含数据列表的密钥?类似于以下类:

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

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

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

swagger 2.0可以实现吗?我正在考虑定义另一个扩展ArrayList的DataList类,然后将此类用作Map的值。

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&答案-----------

谢谢@nickb

我我使用swagger-codegen-maven-plugin版本2.2.1和yml定义生成地图如下:

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'


推荐答案

我正在使用Swagger具有以下模型定义的codegen v2.1.6:

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'

这会产生 Bar 具有以下字段的Java类:

This generates a Bar Java class with the following field:

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

如果你看到不同的行为,你可能会偶然发现回归。尝试测试早期版本,或者专门查看2.1.6是否正确生成此模型。

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.

这篇关于如何定义HashMap&lt; String,List&lt; Object&gt;&gt; swagger yml的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆