繁体   English   中英

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

[英]how define both required and anyof properties in OpenAPI 3.0

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

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       

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 ...

使用以下建议后

  anyOf:
    - required: [campaign_code]
    - required: [offer_code]

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

在此处输入图片说明

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

      anyOf:
        - required: [campaign_code]
        - required: [offer_code] 

或者,为了使其在 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] 

暂无
暂无

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

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