Skip to main content

Improved oneOf and anyOf Support in Java

· 4 min read

We have incorporated oneOf and anyOf discriminator and meta case support in Java SDKs to enable seamless value mapping. This enhancement enhances the versatility and ease of use of our Java SDKs, allowing developers to efficiently handle complex data structures and streamline their workflow.

Details

This release brings additions to the generated Java SDK, including the introduction of oneOf and anyOf discriminators, as well as enhanced support for meta cases. Furthermore, it improves the developer experience through enhancements to the Java SDK Documentation and Readme file.

What's Changed?

Following improvements are now made part of the Java SDK and Documentation:

  • Added discriminator support for oneOf and anyOf types
  • Added meta case support for oneOf and anyOf types
  • Added support for oneOf and anyOf Validation Exceptions

Following changes are made in the Java Documentation and ReadMe:

  • Added oneOf and anyOf type containers in the generated Readme file
  • Added oneOf and anyOf type response handling with match cases in generated code samples

Discriminator support for oneOf and anyOf types

We have added support for the discriminator defined with oneOf and anyOf types. The following example shows a discriminator type defined between oneOf Lion and Deer instance.

"OneOfLionDeerType": {
"oneOf": [
{
"$ref": "#/components/schemas/Lion"
},
{
"$ref": "#/components/schemas/Deer"
}
],
"description": "OneOf (Lion, Deer) with type discriminator of mapping (hunter:Lion, Hunted:Deer)",
"discriminator": {
"propertyName": "type",
"mapping": {
"hunter": "Lion",
"Hunted": "Deer"
}
},
}

The discriminator value mapping defines that the instance would be of Lion if type value is defined as hunter or Deer if type value is defined as Hunted.

The oneOf and anyOf discriminator support has also been added to the SDK documentation.

oaf-discriminator.gif

Meta Case support for oneOf and anyOf types

The support for assigning names or descriptions to the Type Cases within oneOf and anyOf has now been added to the Java SDK and documentation. The following example shows how to add name and description for OneOfPrimitive oneOf type using x-oneof-cases-meta extension tag.

"OneOfPrimitive": {
"example": 2,
"oneOf": [
{
"type": "string"
},
{
"type": "integer",
"format": "int32"
}
],
"description": "OneOf with primitive types and metadata.",
"x-oneof-cases-meta": [
{
"name": "Sender name",
"description": "Name of sender."
},
{
"name": "Message id",
"description": ""
}
]
},

The generated SDK will now use the name provided with the oneOf cases meta instead of auto generated names.

OneOfPrimitive OneOfPrimitive = OneOfPrimitive.fromSenderName(
"John Doe"
);

OneOfPrimitive OneOfPrimitive = OneOfPrimitive.fromMessageId(
2
);

oneOf and anyOf Validation Exception

The support for exceptions of oneOf and anyOf validation has now been added to the Java SDK. The Java SDK throws oneOf and anyOf Validation Exception whenever an invalid request or response for oneOf/anyOf type is encountered. This feature enforces type strictness and validation of request and response within the SDK.

Following code example will end up in OneOfValidationException catch block if response JSON data maps to more then one of the multiple types. If none of the types could be mapped on to the response JSON data, then AnyOfValidationException catch block will be executed.

try {
result = client.getConnectionController().getConnection();
} catch (OneOfValidationException e) {
// If more then one type matches
System.out.println(e.getMessage());
} catch (AnyOfValidationException e) {
// If none of the types matches
System.out.println(e.getMessage());
}

Code sample of endpoint response handling

The code samples for utilizing the oneOf and anyOf endpoint response has been added to the SDK documentation.

queryController.sendPrimitiveAsync(OneOfPrimitive, AnyOfPrimitive, OneOfAndAnyOfPrimitive).thenAccept(result -> {
result.match(new AnyOfPrimitive.Cases<Void>() {

@Override
public Void senderName(String senderName) {
// TODO success callback handler
System.out.println(senderName);
return null;
}

@Override
public Void messageId(int messageId) {
// TODO success callback handler
System.out.println(messageId);
return null;
}
});

oneOf and anyOf containers in ReadMe

The missing oneOf and anyOf containers are now added to local ReadMe file generated with the SDK. The broken links in the endpoint and controller ReadMe have also been fixed.