【问题标题】:how define both required and anyof properties in OpenAPI 3.0如何在 OpenAPI 3.0 中定义 required 和 anyof 属性
【发布时间】:2019-12-30 23:49:09
【问题描述】:

我正在为一个 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       

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 编辑器架构/模型视图未显示任何内容,如下所示 -

【问题讨论】:

    标签: json swagger schema openapi


    【解决方案1】:

    对,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] 
    

    【讨论】:

    • 验证错误消失了,但现在 swagger 编辑器无法识别那里的任何内容。看上面问题中的图片
    • @ArpitMittal 我添加了一个替代解决方案
    • 它工作并显示在架构中,但招摇无法生成示例请求正文。有任何想法吗 ? {“同意”:“接受”,“cid”:“AB5160001725799”,“卡”:1234123412341234,“enrollmentDateTime”:{},“channelInfo”:{“channel”:“BOA”,“operatorType”:“Officer Id” ", "operatorId": "z56995909", "transitNumber": 12345 } }
    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2010-10-31
    相关资源
    最近更新 更多