salesforce-sdk icon indicating copy to clipboard operation
salesforce-sdk copied to clipboard

Force case sensitive JSON for dynamic model deserialization

Open maniax89 opened this issue 7 years ago • 1 comments

JSON deserialization for models using IBMWatsonDynamicModel fails when JSON object keys are case insensitive.

The following Apex code can be run and errors out with: System.JSONException: Duplicate field: IBMDiscoveryV1Models.QueryResult.id_serialized_name

String responseText = '{"id":"1","Id":"2"}';
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(responseText);
Map<String, Object> safeJsonMap = IBMWatsonJSONUtil.prepareResponse(jsonMap);
String jsonString = JSON.serialize(safeJsonMap);
IBMDiscoveryV1Models.QueryResult foo = (IBMDiscoveryV1Models.QueryResult) new IBMWatsonDynamicModel().deserialize(jsonString, safeJsonMap, IBMDiscoveryV1Models.QueryResult.class);
System.debug(foo);

I would expect that the Id field would get deserialized into the additional_properties and id would be deserialized to just id_serialized_name. The alternative is to not try and mix dynamic and static model types and just make the entire object dynamic (everything is a Map<String, Object>)

When reporting a bug, please be sure to include the following

  • [X] SDK version - 1.0
  • [X] Steps to reproduce
  • [X] Expected behavior
  • [X] Actual behavior

maniax89 avatar Mar 06 '18 07:03 maniax89

I took a quick look at this to try and get a fix in for the upcoming release but don't see a quick solution at the moment. The section where the code fails is after calling the built-in JSON.deserialize() method, so I think fixing this would take writing some custom deserialization logic similar to how we've done in other places.

I'll leave this issue open for now as a reminder to maybe look at it in the future (and because I do agree it's a problem), but right now it's just a big limitation of the language that I'm not going to prioritize.

lpatino10 avatar Mar 26 '19 18:03 lpatino10