Avoid exceptions from jackson serialization of processValues that contain a map with a null key

This commit is contained in:
2024-12-11 14:59:08 -06:00
parent 5434721c8e
commit 63a48eeafa
6 changed files with 129 additions and 7 deletions

View File

@ -352,6 +352,8 @@ public class QJavalinProcessHandler
Map<String, Object> resultForCaller = new HashMap<>();
Exception returningException = null;
String processName = context.pathParam("processName");
try
{
if(processUUID == null)
@ -360,7 +362,6 @@ public class QJavalinProcessHandler
}
resultForCaller.put("processUUID", processUUID);
String processName = context.pathParam("processName");
LOG.info(startAfterStep == null ? "Initiating process [" + processName + "] [" + processUUID + "]"
: "Resuming process [" + processName + "] [" + processUUID + "] after step [" + startAfterStep + "]");
@ -441,10 +442,30 @@ public class QJavalinProcessHandler
// negative side-effects - but be aware. //
// One could imagine that we'd need this to be configurable in the future? //
///////////////////////////////////////////////////////////////////////////////////
context.result(JsonUtils.toJson(resultForCaller, mapper ->
try
{
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
}));
String json = JsonUtils.toJson(resultForCaller, mapper ->
{
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// use this custom serializer to convert null map-keys to empty-strings (rather than having an exception!) //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
mapper.getSerializerProvider().setNullKeySerializer(JsonUtils.nullKeyToEmptyStringSerializer);
});
context.result(json);
}
catch(Exception e)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////
// related to the change above - we've seen at least one new error that can come from the //
// Include.ALWAYS change (a null key in a map -> jackson exception). So, try-catch around //
// the above serialization, and if it does throw, log, but continue trying a default serialization //
// as that is probably preferable to an exception for the caller... //
/////////////////////////////////////////////////////////////////////////////////////////////////////
LOG.warn("Error deserializing process results with serializationInclusion:ALWAYS - will retry with default settings", e, logPair("processName", processName), logPair("processUUID", processUUID));
context.result(JsonUtils.toJson(resultForCaller));
}
}

View File

@ -26,6 +26,7 @@ import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kingsrook.qqq.middleware.javalin.executors.io.ProcessInitOrStepOrStatusOutputInterface;
import com.kingsrook.qqq.middleware.javalin.schemabuilder.SchemaBuilder;
import com.kingsrook.qqq.middleware.javalin.schemabuilder.ToSchema;
@ -117,6 +118,7 @@ public class ProcessInitOrStepOrStatusResponseV1 implements ProcessInitOrStepOrS
** Getter for values
**
*******************************************************************************/
@JsonIgnore // we are doing custom serialization of the values map, so mark as ignore.
public Map<String, Serializable> getValues()
{
return values;

View File

@ -183,6 +183,14 @@ public class ProcessSpecUtilsV1
String name = valueEntry.getKey();
Serializable value = valueEntry.getValue();
///////////////////////////////////////////////////////////////////////////////////////////////////////
// follow the strategy that we use for JsonUtils.nullKeyToEmptyStringSerializer in this rare case... //
///////////////////////////////////////////////////////////////////////////////////////////////////////
if(name == null)
{
name = "";
}
Serializable valueToMakeIntoJson = value;
if(value instanceof String s)
{
@ -213,11 +221,31 @@ public class ProcessSpecUtilsV1
valueToMakeIntoJson = new WidgetBlock(abstractBlockWidgetData);
}
String valueAsJsonString = JsonUtils.toJson(valueToMakeIntoJson, mapper ->
///////////////////////////////////////////////
// ok now, make the value into a JSON string //
///////////////////////////////////////////////
String valueAsJsonString;
try
{
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
});
valueAsJsonString = JsonUtils.toJson(valueToMakeIntoJson, mapper ->
{
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// use this custom serializer to convert null map-keys to empty-strings (rather than having an exception!) //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
mapper.getSerializerProvider().setNullKeySerializer(JsonUtils.nullKeyToEmptyStringSerializer);
});
}
catch(Exception e)
{
LOG.warn("Error deserializing process results with serializationInclusion:ALWAYS - will retry with default settings", e);
valueAsJsonString = JsonUtils.toJson(valueToMakeIntoJson);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// THEN - make it back into a JSONObject or JSONArray, and add it to the valuesAsJsonObject JSONObject //
/////////////////////////////////////////////////////////////////////////////////////////////////////////
if(valueAsJsonString.startsWith("["))
{
valuesAsJsonObject.put(name, new JSONArray(valueAsJsonString));