Skip to main content

Adding Support for Optional/Required Properties for oneOf and anyOf in Java SDKs

· 2 min read

OAS allows you to set optional/required properties in models. While serializing a model, optional properties will be omitted if the value of property is not set (i.e. initialized with null) and, required properties will be serialized in any case. This is a case of factoring schema in oneOf/anyOf. Now with this support, you can define properties with the same names but with different optional/required constraints in request/response of an endpoint.

Details

Below is an example of two models with same property names but different optional/required constraints defined in an OpenAPI specification.

"Dog": {
"title": "Dog",
"required": [
"bark",
"age"
],
"type": "object",
"properties": {
"bark": {
"type": "boolean"
},
"age": {
"type": "integer",
"format": "int32"
},
"cute": {
"type": "boolean"
},
"breed": {
"type": "string",
"nullable": true
}
},
"description": "A model with 2 required properties: bark, age\n and 2 optional properties: cute, breed"
}
"Cat": {
"title": "Cat",
"required": [
"bark",
"cute"
],
"type": "object",
"properties": {
"bark": {
"type": "boolean"
},
"age": {
"type": "integer",
"format": "int32"
},
"cute": {
"type": "boolean"
},
"breed": {
"type": "string",
"nullable": true
}
},
"description": "A model with 2 required property: bark, cute and 2 optional properties: age, breed"
}

In Java SDKs, these models will be treated as a case of factoring schema. Annotations are being used to handle them in models so developers do not need to worry about specifying anything rather just initialize the model properties as per the requirements.