定义"flat" OpenAPI 文档模式?



我正在尝试使用OpenAPI 3.0记录一个微型服务该服务返回Json,该Json具有任何字段名,并且可以包含数组、字符串、数字、true、false、null中的任何一个,但不能包含对象。字符串可以包含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

但这将允许对象。

我走对了吗?或者有一个更容易/正确的说法";任何名称,任何值,只是不是另一个对象">

一种不符合OpenAPI 3但符合OpenAPI 3.1的方法是使用not关键字:

PD_6这里的逻辑是:

  • 任何其他属性都可以
  • 但不是具有类型对象的附加属性

最新更新