How to define HashMaplt;String, Listlt;Objectgt;gt; property in swagger yml?(如何定义HashMaplt;String、Listlt;Objectgt;gt;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.
这篇关于如何定义HashMap<String、List<Object>>swagger yml 中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何定义HashMap<String、List<Object>>swagger yml 中的属性?
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
