如果所有父变量都标记为必需,如何使父模型的变量在子模型中不需要?

how to make parent model's variables as not required in child model, provided all the parent variables are marked as required?

我有一个父定义,它由一个使用 allOf 关键字的子定义扩展。由于我已将所有变量标记为父模型中的必需变量,因此它也被标记为子模型中的必需变量。但我需要将 父变量标记为不需要 。有什么想法吗?

definitions: 
  Parent:
    type: "object"
    required:
      - "id"
    properties:
      id:
        type: "integer"
        format: "int64"
        minimum: 1
        example: "123456"        

  Child:
    allOf:
      - $ref: "#/definitions/Parent"
    type: "object"
    required: 
      - "sample"
    properties:
      sample:
        type: "string"
        format: "full-date"
        example: "2001/12/31"

您不能在子模型中取消对父模型字段的要求。解决方法是定义一个具有可选属性的基本模型,例如 ParentBase,并从该模型继承 ParentChild

definitions:
  ParentBase:
    type: object
    properties:
      id:
        type: integer
        format: int64
        minimum: 1
        example: 123456

  Parent:
    allOf:
      - $ref: "#/definitions/ParentBase"
      - required:
          - id

  Child:
    allOf:
      - $ref: "#/definitions/ParentBase"
      - type: object
        required: 
          - sample
        properties:
          sample:
            type: string
            format: full-date
            example: "2001/12/31"


在一个不相关的注释中,id 属性 被定义为一个整数,但它的示例值是一个字符串。这是错误的——示例数据类型应该匹配 属性 类型。您需要使用不带引号的 123456

id:
  type: integer
  #example: "123456"  # Wrong
  example: 123456     # Correct