如何定义HashMap<String、List<Object>>swagger yml 中的属性?
我正在使用 swagger 在 Java 和 Type 脚本中生成类.我在使用对象列表作为值定义地图属性时遇到问题.我尝试定义如下:
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.
-------------更新 &回答-----------
谢谢@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.
相关文章