Skip to main content

Adding Support for Enums in oneOf and anyOf in Java SDKs

· 2 min read

Java SDKs now support enum types defined as oneOf/anyOf types. You can define as many distinct enum types as required with this support.

Details

This feature is an addition to the oneOf and anyOf support where enum types are used as oneOf/anyOf types. The support for enums, defined as oneOf/anyOf types, has now been added in Java SDK.

You can define as many distinct enum types as oneOf/anyOf types as you need. Here is an example to oneOf types containing enums for the month of a year:

"oneOf": [
{
"$ref": "#/components/schemas/MonthName"
},
{
"$ref": "#/components/schemas/MonthNumber"
}
]

Here we have 2 distinct enums like month name of type string and month number of type integer. Java SDK allows you to send/receive value from oneOf/anyOf enums with the help of container classes. Handling of this case in Java SDK is pretty simple when it comes to sending the enums in the request, you just initialize the container class with oneOf/anyOf types.

Here is how you can initialize a month's name:

OneofMultipleEnum oneofMultipleEnum =
OneofMultipleEnum.fromMonthName(MonthNameEnum.DECEMBER); // December

Or you can initialize with month number enum:

OneofMultipleEnum oneofMultipleEnum =
OneofMultipleEnum.fromMonthNumber(MonthNumberEnum.FEBRUARY); // 2

Now in order to handle the oneOf/anyOf response, you just provide the implementation of each case instead of type checking like:

responseEnum.match(new OneofMultipleEnum.Cases<Void>() {

@Override
public Void monthName(MonthNameEnum monthName) {
// Do something with month name enum
return null;
}

@Override
public Void monthNumber(MonthNumberEnum monthNumber) {
// Do something with month number enum
return null;
}
});

Here responseEnum is the actual response that is received from the server.