1

I am using openapi 3.0.3 version and we have scanario where we don't want to make one field mandatory for Response. Below is the example of Open Api Schema Snippit.

For example here is the MerchantDetails object where we have 'merchantName' and 'merchantId' as required field.

MerchantDetails:
  description: Used to pass merchant specific data during the transaction.
  properties:
    merchantName:
      type: string
      maxLength: 64
    merchantId:
      type: string
      maxLength: 16
  required:
    - merchantName
    - merchantId

In Response we are using same object but we want to keep only one field 'merchantId' as required. Is there any way do achieve this without creating new object?

MerchantDetails:
  description: Used to pass merchant specific data during the transaction.
  properties:
    merchantName:
      type: string
      maxLength: 64
    merchantId:
      type: string
      maxLength: 16
  required:
    - merchantId
Pratiyush Kumar Singh
  • 1,977
  • 3
  • 19
  • 39

1 Answers1

1

You can use the readOnly and writeOnly keywords to mark specific properties as only in response or only in request.

If a readOnly or writeOnly property is included in the required list, required affects just the relevant scope – responses only or requests only. That is, read-only required properties apply to responses only, and write-only required properties – to requests only.

Not sure if all tools support these keywords. I have tested that Redoc can generate document correctly.

MerchantDetails:
  description: Used to pass merchant specific data during the transaction.
  properties:
    merchantName: 
      type: string
      maxLength: 64
      writeOnly: true  # only in request
    merchantId:
      type: string
      maxLength: 16
  required:
    - merchantName
    - merchantId

Please read the Read-Only and Write-Only Properties section in https://swagger.io/docs/specification/data-models/data-types/

aleung
  • 9,848
  • 3
  • 55
  • 69