cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Frequent Visitor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

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

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'
  if:
    properties:
      check_completed:
        const: true
  then:
    required:
      - check_details

Helen Kosova
SmartBear Technical Writer
________________________

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!

View solution in original post

1 REPLY 1
Highlighted
Moderator

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

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'
  if:
    properties:
      check_completed:
        const: true
  then:
    required:
      - check_details

Helen Kosova
SmartBear Technical Writer
________________________

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!

View solution in original post