如何在 OpenAPI 3.0 中定义 required 和 anyof 属性

Arp*_*tal 2 schema json swagger openapi

我正在为一个 api 创建一个 OpenAPI 3 规范,该规范具有需要某些属性的对象,而对于某些对象来说,它们是任意的。当我创建如下规范时,它会抛出一个错误,我无法修复该错误。

EnrollementRequest:
      type: object
      properties:
        consent:
          $ref: "#/components/schemas/ConsentEnum"
        cid:
          $ref: '#/components/schemas/CID'
        card:
          $ref: '#/components/schemas/Card'
        enrollmentDateTime :
          description: The date and time of enrollment with respective timezone
          format: date-time
          example: 2018-11-13T20:20:39+00:00
        campaign_code: 
          description: the campaign-code for which customer wants to enroll
          type: string
        offer_code:
          description: the offer-code for which customer wants to enroll
          type: string
        channelInfo:
          $ref: '#/components/schemas/Channel'
      required:
        - consent
        - cid
        - enrollmentDateTime
        - channelInfo
      anyOf:
        - campaign_code
        - offer_code       
Run Code Online (Sandbox Code Playgroud)

Swagger 编辑器给出如下错误 -

Errors

Structural error at components.schemas.EnrollementRequest.anyOf.0
should be object
Jump to line ...
Structural error at components.schemas.EnrollementRequest.anyOf.1
should be object
Jump to line ...
Run Code Online (Sandbox Code Playgroud)

使用以下建议后

  anyOf:
    - required: [campaign_code]
    - required: [offer_code]
Run Code Online (Sandbox Code Playgroud)

验证错误消失了,但 swagger 编辑器架构/模型视图未显示任何内容,如下所示 -

在此输入图像描述

Maf*_*for 5

对,anyOf一定是一个对象列表。尝试这个:

      anyOf:
        - required: [campaign_code]
        - required: [offer_code] 
Run Code Online (Sandbox Code Playgroud)

或者,为了使其在 Swagger 编辑器中看起来更好:

    EnrollementRequest:
      type: object
      properties:
        consent:
          $ref: "#/components/schemas/ConsentEnum"
        cid:
          $ref: '#/components/schemas/CID'
        card:
          $ref: '#/components/schemas/Card'
        enrollmentDateTime :
          description: The date and time of enrollment with respective timezone
          format: date-time
          example: 2018-11-13T20:20:39+00:00
        channelInfo:
          $ref: '#/components/schemas/Channel'
      required:
        - consent
        - cid
        - enrollmentDateTime
        - channelInfo
      anyOf:
        - properties:
            campaign_code: 
              description: the campaign-code for which customer wants to enroll
              type: string
          required: [campaign_code] 
        - properties: 
            offer_code:
              description: the offer-code for which customer wants to enroll
              type: string
          required: [offer_code] 
Run Code Online (Sandbox Code Playgroud)