[英]How to have multiple infinite objects without an array - Open API / Swagger
我正在尝试为以下 JSON 片段设置一个开放的 api 规范:
"test1": {
"1739573957": {
"tester1": 123,
"tester2": "Company"
},
"64903826718": {
"tester1": 123,
"tester2": "Company"
}
"5902849189": {
"tester1": 123,
"tester2": "Company"
}
}
test1 中的对象具有随机化的 guid,并以通常是数组的方式列出,但实际上不是。 测试 1 中可能有无数个对象。有人知道如何设置吗?
您正在寻找自由形式的对象:
自由形式的对象(任意属性/值对)定义为:
type: object
这相当于
type: object additionalProperties: true
和
type: object additionalProperties: {}
但是,如果您可以更改 API,我强烈建议将其更改为一组测试或对象定义的任何内容。 将 id 作为属性放入此对象中,您就可以开始了。 这使得为其创建 DTO 变得更加容易。
test1
是一个字符串到对象的字典,可以定义如下(假设 OpenAPI 3):
components:
schemas:
Tester: # Or however you would name the nested objects
tester1:
type: integer
example: 123
tester2:
type: string
example: Company
...
test1:
type: object
additionalProperties:
$ref: '#/components/schemas/Tester'
# Optional example for the `test1` property
example:
'1739573957':
tester1: 123
tester2: Company
'64903826718':
tester1: 123
tester2: Company
test1 中的对象有随机的 guids
在 OpenAPI 3.1 中,您可以使用patternProperties
而不是additionalProperties
来定义test1
中的键是数字字符串。 早期的 OpenAPI 版本无法定义字典键的格式。
# openapi: 3.1.0
test1:
type: object
patternProperties: # <-----
'^\d+$': # <-----
$ref: '#/components/schemas/Tester'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.