We have a REST service that gets data from external service to salesforce.
The data can be achieved using RestContext.request.requestBody.toString();
So I create an inner class to deserialize the request body into it.
My issue is that a param that is sent from the external service is a List of String, but if it's empty they sent an empty String ("").
So, on one hand, I need my inner class to have a List param, but on another, if it's empty - the param is equal to "" - it's not a List type anymore its a String, and its fails.
I know that one solution is to change the type it sent from empty String to empty array in the external service for example but it takes too long to get answers from them, and since it is urgent we decided to do it on our own.
another solution I thought about, is to substring the requestBody by the param name and check if it's an empty string or an array - and this solution could work, but I don't have one param, I have many.
So I wonder if there are other ways of doing this check, before the deserialization.
@HttpPost
global static String getDataFromExternal(){
try{
RootObject data = (RootObject) JSON.deserialize(RestContext.request.requestBody.toString(),RootObject.class);
}catch(Exception e){
System.debug('e : ' + e.getMessage());
}
public class RootObject{
List<String> shouldBeListParamA;
List<String> shouldBeListParamB;
List<String> shouldBeListParamC;
String textParamA;
String textParamB;
String textParamC;
public RootObject(){
}
}
}
And the Data received can be:
{"textParamA":"ABC","textParamB":"ABC","textParamC":"ABC","shouldBeListParamA":["ABC","CDE"],"shouldBeListParamB":"","shouldBeListParamC":"".......}
Clarifications:
The blank value - is for a param inside the body and not for the whole body.
means - a param type List<String>
if it have data in it it will be like that:
"shouldBeListParamA":["ABC","CDE"]
but if this specific param received without data it will be empty, means ""
instead of []
(empty array).
example:
"shouldBeListParamA":""
I need to create the type in my inner class - and I created it like that:
List<String> shouldBeListParamA;
but when its empty (just this param or other param with the same type) it will fail in the deserialization.
Many Thanks!