How to make a field in a definition required for some operations and not others

前端 未结 3 1449
执笔经年
执笔经年 2020-12-10 10:57

I\'m writing my swagger definition in yaml. Say I have a definition that looks something like this.

paths:
  /payloads:
    post:
      summary: create a pay         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 11:18

    You would have to define the models separately.

    However, you have options for the cases of exclusion and difference.

    If you're looking to exclude, which is the easy case, create a model of with the excluded property, say ModelA. Then define ModelB as ModelA plus the additional property:

    ModelB:
      allOf:
        - $ref: "#/definitions/ModelA"
        - type: object
          properties:
            id:
              type: string
    

    If you're looking to define the difference, follow the same method above, and exclude the id from ModelA. Then define ModelB and ModelC as extending ModelA and add the id property to them, each with its own restrictions. Mind you, JSON Schema can allow you to follow the original example above for some cases to "override" a definition. However, since it is not really overriding, and one needs to understand the concepts of JSON Schema better to not make simple mistakes, I'd recommend going this path for now.

提交回复
热议问题