添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
打酱油的槟榔  ·  .NET ...·  1 周前    · 
不拘小节的紫菜  ·  python ...·  1 月前    · 
坚韧的大葱  ·  Problem with creating ...·  5 月前    · 

Forum Discussion

as
New Contributor
5 years ago

Swagger Response Body require object if property equals a value OpenApi Spec 3

Let's say we have a simple response

{
"check_completed": true,
"check_details": {
    "check_number": 123123.
    "check_mechanic": "Joe Blogs"
}
CheckResponse:
      type: object
      required:
        - check_completed
      properties:
        check_completed:
             type: boolean
        check_details:
             $ref: '#/components/schemas/checkdetails'

Is it possible to mark the check_details as required if check_completed is true?

  • HKosova
    SmartBear Alumni (Retired)

    It's possible but cumbersome. You need to use anyOf , like this:

    CheckResponse:
      type: object
      required:
        - check_completed
      properties:
        check_completed:
          type: boolean
        check_details:
          $ref: '#/components/schemas/checkdetails'
      anyOf:
        - properties:
            check_completed:
              enum: [false]
        - properties:
            check_completed:
              enum: [true]
          required:
            - check_details

    I would recommend describing this dependency in the schema/property descriptions instead.

    OpenAPI 3.1 (future version) will make this easier thanks to using the latest JSON Schema which includes if...then..else among other things.

    CheckResponse::
      type: object
      required:
        - check_completed
      properties:
        check_completed:
          type: boolean
        check_details:
          $ref: '#/components/schemas/checkdetails'