我试图使用OpenAPI 3.0记录一个微系统服务,该服务返回的Json具有任意字段名,可以包含任意数组、字符串、数字、真、假、空,但不包含对象。字符串可以包含ISO日期。只有极少数已知字段名(例如"@id")
示例:
{ "@unid" : "a34", "color" : "blue", "value" : 42, "active" : true }无效样本:
{ "@unid" : "b55",
"shape" : "square",
"params" : { "value" : 42, "active" : true }
}所以我的第一次尝试是:
title: Root Type for Document
description: A generic document
type: object
properties:
'@unid':
description: Primary key
type: string
'@created':
format: date-time
description: Document creation dateTime as ISO date
type: string
'@size':
format: int32
description: Storage size on backend
type: integer
'@unread':
description: Did user accnowledge having read it
type: boolean
additionalProperties:
anyOf:
-
type: string
-
type: boolean
-
type: integer
-
format: date-time
type: string
example:
'@unid': EA219565FC07ADC600258695004FCE92
'@created': '2021-03-11T14:31:42Z'
'@size': 31
'@unread': false
Color: Red
form: SampleForm
Cost: 31.3
Active: true
lastaccessed: '2021-03-11T14:31:42.000Z'我查看了OpenAPI字典规范,它建议使用:
type: object
additionalProperties: true但这将允许物体。
我在正确的轨道上吗?或者说“任何名称、任何值,只是不是另一个对象”是否更容易/正确?
发布于 2021-03-16 11:47:43
一种非OpenAPI 3兼容但符合OpenAPI 3.1的方法是使用not关键字:
type: object
additionalProperties: true
not:
additionalProperties:
type: object这里的逻辑是:
https://stackoverflow.com/questions/66584982
复制相似问题