【问题标题】:Which is the correct way to restrict openapi type in properties?在属性中限制 openapi 类型的正确方法是什么?
【发布时间】:2018-08-11 18:23:16
【问题描述】:

到目前为止,我有以下内容:

Links:
  description: Must contain links objects
  type: object
  properties:
    $ref: "#/components/schemas/Link"
ErrorLinks:
  allOf:
    - $ref: "#/components/schemas/Links"
    - properties:
        about:
          $ref: "#/components/schemas/Link"

Links 中,我不在乎任何properties 的名称是什么,只是它们都是Link 对象。在ErrorLinks 中,我希望有一个about 属性,它也是一个Link 对象。

这是定义它的正确方法,还是我说(在 Links 中)我希望 properties 节点本身是 Link 对象?

我发现 openapi 文档有点缺乏有用的示例,以上验证过,所以我不完全确定。

【问题讨论】:

标签: openapi


【解决方案1】:

Links中,我不关心任何属性的名称是什么,只是它们都是Link对象。

Links 是一个字典(哈希图)。字典是使用additionalProperties 定义的,如this answer 中所述:

Links:
  description: Must contain links objects
  type: object
  additionalProperties:
    $ref: "#/components/schemas/Link"

ErrorLinks 中,我希望有一个about 属性,它也是一个Link 对象。

你快到了。不需要allOf,你只需要一个简单的对象模式:

ErrorLinks:
  type: object
  properties:
    about:
      $ref: "#/components/schemas/Link"
  required:
    - about

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-03-10
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多