Search code examples
jsonwso2wso2-micro-integratorapache-synapse

JsonNull UnsupportedOperationException on null value using WSO2 JsonTransform Mediator


I'm using a JsonTransform Mediator in my sequence to make sure a phone number field in my database query is returned as a string instead of as a number.

<jsontransform description="JSON Transform" schema="conf:custom/mySchema.json">

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "nodes": {
      "type": "object",
      "properties": {
        "node": {
      "type": "array",
      "items": {
        "type": "object",
            "properties": {
              "phone": {
        "type": "string"
              }
        }
      }
    }
      }
    }
  }
}

The problem is that field can also be null. If the field is not null, the mediator works fine, but if it is null, I get the following exception:

JsonNull java.lang.UnsupportedOperationException: JsonNull
    at com.google.gson.JsonElement.getAsString(JsonElement.java:192)
    at org.apache.synapse.commons.json.jsonprocessor.validators.ObjectValidator.validateAndUpdateEntriesMap(ObjectValidator.java:349)

I'm using JsonTransform because that's what I've seen as the recommended way to ensure the field is a string, but there's no JsonTransform examples in the WSO2 documentation where a field can also be null. I've tried adding

<property name="synapse.commons.enableXmlNullForEmptyElement" value="false"/>

and

<property name="synapse.commons.json.output.autoPrimitive" value="true"/>

but I still got the same exception. I'm open to solutions that use other mediators but hoping not to have to write a custom mediator to check for nulls.


Solution

  • Just specify both types as in here.

    "phone": { "type": ["string", "null"] }