简体   繁体   English

在 openAPI 3.1 中定义精确的自定义属性

[英]Define exact custom Properties in openAPI 3.1

I have a JSON schema I am trying to describe, a JSON object which has a additionalProperties node which contains an array of key value pairs.我有一个 JSON 架构,我试图描述一个 JSON object,它有一个additionalProperties属性节点,其中包含一组键值对。

{
  "additionalProperties": [
    {
        "key": "optionA",
        "value": "1"
    },
    {
        "key": "optionB",
        "value": "0"
    },
    {
        "key": "optionC",
        "value": "1"
    }
  ],
}

Whilst I can use quite a generic schema for this like this虽然我可以像这样使用相当通用的模式

    additionalProperties:
      properties:
        key:
          type: string
        value:
          type: string
      required:
        - key
        - value
      type: object

I ideally wish to explain what the various keys that can appear and what they mean.理想情况下,我希望解释可能出现的各种键以及它们的含义。 Ie optionA means this and OptionB means that.即 optionA 表示这个,OptionB 表示那个。 Is there a way I can describe the exact options which will appear in the array?有没有办法可以描述将出现在数组中的确切选项?

The description field is used when you want to provide additional information or context to the reader that isn't necessarily explained by schema alone.当您想向读者提供不一定由模式单独解释的附加信息或上下文时,使用description字段。

additionalProperties:
  description: Your explanation goes here. Note that you can use markdown formatting if desired.
  properties:
    key:
      type: string
    value:
      type: string
  required:
    - key
    - value
  type: object

You can also more accurately describe your options in the schema if they are all known values using oneOf , allOf , or anyOf .如果它们都是使用oneOfallOfanyOf的已知值,您还可以更准确地描述架构中的选项。 ( Documentation here ) 文档在这里

additionalProperties:
  properties:
    anyOf:
      - $ref: '#/components/schemas/optionA'
      - $ref: '#/components/schemas/optionB'
      - $ref: '#/components/schemas/optionC'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM