如何在openapi 3.0中定义引用组件的必需属性?

时间:2019-04-23 16:48:00

标签: swagger openapi

我指的是其他组件,并且其中的某些字段必填。

AbstractQuestion的示例,该组件我将在以后参考:

  AbstractQuestion:
        type: object
        properties:
            title:
                description: Question statement
                type: string
            label:
                description: Question label
                type: string
            required:
                description: whether an answer is required for this question or if it could be left blank
                type: boolean
            question_type:
                description: campaign type
                enum:
                    - profile
                    - feeling
                type: string
            answer_type:
                enum:
                    - string
                    - list
                    - date
                type: string
            max_length:
                description: >-
                    for open ended questions this is the max characters possible. for list type questions
                    this would be the max number of options to select when multiple answers are accepted
                type: integer
            modelizable:
                description: 'whether the questions'' answers are suitable for feeding ML models (e.g. 1,2,3,4)'
                type: boolean
            choices:
                $ref: '#/components/schemas/QuestionChoices'

现在,我将使用它来定义Post请求正文,并指定必填字段。注意:我没有在上一个对象中指定此字段,因为它也用于不需要这些字段的其他上下文中(例如GET或PUT请求)。

    QuestionPost:
      allOf:
        - $ref: '#/components/schemas/AbstractQuestion'
      required:
          - title
          - label
          - required
          - question_type
          - answer_type
          - modelizable

当我尝试此解决方案时,我的测试未通过,因为它们将yaml文件与我的api匹配并发现此错误:

openapi_spec_validator.exceptions.ExtraParametersError: Required list has not defined properties: ['label', 'title', 'answer_type', 'question_type', 'modelizable', 'required']

似乎无法在引用的组件中找到所需的属性。

当我尝试此解决方案时:

    QuestionPost:
      $ref: '#/components/schemas/AbstractQuestion'
      required:
          - title
          - label
          - required
          - question_type
          - answer_type
          - modelizable

我在编辑器中收到警告。我对文档的理解是必填字段将被忽略。

也许我要实现的目标是不可能的?你会怎么做?

0 个答案:

没有答案