如何在没有数组的情况下拥有多个无限对象-Open API/Swagger [英] How to have multiple infinite objects without an array - Open API / Swagger
本文介绍了如何在没有数组的情况下拥有多个无限对象-Open API/Swagger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为以下JSON片段设置开放式API规范:
"test1": {
"1739573957": {
"tester1": 123,
"tester2": "Company"
},
"64903826718": {
"tester1": 123,
"tester2": "Company"
}
"5902849189": {
"tester1": 123,
"tester2": "Company"
}
}
Test1内的对象具有随机化的GUID,并以通常为数组的方式列出,但实际上并非如此。测试1中可能有无限数量的对象。有人知道如何设置吗?
推荐答案
test1
是字符串到对象的字典,定义如下(假设OpenAPI3):
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内的对象具有随机GUID
在OpenAPI 3.1中,您可以使用patternProperties
代替additionalProperties
来定义test1
中的键是数字字符串。早期的OpenAPI版本无法定义字典键的格式。
# openapi: 3.1.0
test1:
type: object
patternProperties: # <-----
'^d+$': # <-----
$ref: '#/components/schemas/Tester'
这篇关于如何在没有数组的情况下拥有多个无限对象-Open API/Swagger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文