Skip to main content

Adding Support for Nullable Types in oneOf and anyOf in Java SDKs

· 2 min read

OAS allows marking properties as nullable, but previously this flag was not being respected in the Java SDK where oneOf/anyOf was being used. Now with this release, the Java SDK fully respects the nullable attribute during serialization & deserialization for oneOf/anyOf properties.

Details

Here is how you can define a nullable oneOf case in OpenAPI specification:

"oneOf": [
{
"type": "number"
},
{
"type": "boolean"
},
{
"nullable": true
}
]

In Java SDK, these oneOf properties will be translated into the oneOf containers that will allow you to set null in any of the cases like for instance, initializing with number type in this case.

OneofNullableCase oneofNullableCase =
OneofNullableCase.fromNumber(null); // here the oneOf container will be set to null

Or you can initialize with some value like:

OneofNullableCase oneofNullableCase =
OneofNullableCase.fromNumber(1265); // here the oneOf container will be initialized with the provided value i.e. 1265

For anyOf cases, you can make the case nullable like you did for oneOf as shown below:

"anyOf": [
{
"type": "number"
},
{
"type": "boolean"
},
{
"nullable": true
}
]

In Java SDK, you can consume anyOf cases like:

AnyofNullableCase anyofNullableCase =
AnyofNullableCase.fromBoolean(null); // here the anyOf container will be set to null

Or with some value like:

AnyofNullableCase anyofNullableCase =
AnyofNullableCase.fromBoolean(false); // here the anyOf container will be initialized with the provided case value i.e. false