Skip to content

[4.x] Fix #603 - Add guard code to collections, tidy up Workflow serializer #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@

public class WorkflowModule extends SimpleModule {

private static final long serialVersionUID = 510l;
private static final long serialVersionUID = 510L;

private WorkflowPropertySource workflowPropertySource;
private ExtensionSerializer extensionSerializer;
private ExtensionDeserializer extensionDeserializer;
private final WorkflowPropertySource workflowPropertySource;
private final ExtensionSerializer extensionSerializer;
private final ExtensionDeserializer extensionDeserializer;

public WorkflowModule() {
this(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ public void serialize(FunctionRef functionRef, JsonGenerator gen, SerializerProv
&& (functionRef.getInvoke() == null
|| functionRef.getInvoke().equals(FunctionRef.Invoke.SYNC))
&& functionRef.getRefName() != null
&& functionRef.getRefName().length() > 0) {
&& !functionRef.getRefName().isEmpty()) {
gen.writeString(functionRef.getRefName());
} else {
gen.writeStartObject();

if (functionRef.getRefName() != null && functionRef.getRefName().length() > 0) {
if (functionRef.getRefName() != null && !functionRef.getRefName().isEmpty()) {
gen.writeStringField("refName", functionRef.getRefName());
}

if (functionRef.getArguments() != null && !functionRef.getArguments().isEmpty()) {
gen.writeObjectField("arguments", functionRef.getArguments());
}

if (functionRef.getSelectionSet() != null && functionRef.getSelectionSet().length() > 0) {
if (functionRef.getSelectionSet() != null && !functionRef.getSelectionSet().isEmpty()) {
gen.writeStringField("selectionSet", functionRef.getSelectionSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,187 +15,171 @@
*/
package io.serverlessworkflow.api.serializers;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.serverlessworkflow.api.Workflow;
import io.serverlessworkflow.api.error.ErrorDefinition;
import io.serverlessworkflow.api.events.EventDefinition;
import io.serverlessworkflow.api.functions.FunctionDefinition;
import io.serverlessworkflow.api.interfaces.Extension;
import io.serverlessworkflow.api.interfaces.State;
import io.serverlessworkflow.api.retry.RetryDefinition;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.UUID;

public class WorkflowSerializer extends StdSerializer<Workflow> {

public WorkflowSerializer() {
this(Workflow.class);
}

protected WorkflowSerializer(Class<Workflow> t) {
super(t);
}

private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

@Override
public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider provider)
throws IOException {

gen.writeStartObject();

if (workflow.getId() != null && !workflow.getId().isEmpty()) {
gen.writeStringField("id", workflow.getId());
} else {
gen.writeStringField("id", generateUniqueId());
}

if (workflow.getKey() != null) {
gen.writeStringField("key", workflow.getKey());
}
gen.writeStringField("name", workflow.getName());

if (workflow.getDescription() != null && !workflow.getDescription().isEmpty()) {
gen.writeStringField("description", workflow.getDescription());
}

if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) {
gen.writeStringField("version", workflow.getVersion());
}

if (workflow.getAnnotations() != null && !workflow.getAnnotations().isEmpty()) {
gen.writeObjectField("annotations", workflow.getAnnotations());
}

if (workflow.getDataInputSchema() != null) {
if (workflow.getDataInputSchema().getSchema() != null
&& workflow.getDataInputSchema().getSchema().length() > 0
&& workflow.getDataInputSchema().isFailOnValidationErrors()) {
gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getSchema());

} else if (workflow.getDataInputSchema().getSchema() != null
&& workflow.getDataInputSchema().getSchema().length() > 0
&& !workflow.getDataInputSchema().isFailOnValidationErrors()) {
gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema());
}
}

if (workflow.getStart() != null) {
gen.writeObjectField("start", workflow.getStart());
}

if (workflow.getSpecVersion() != null && !workflow.getSpecVersion().isEmpty()) {
gen.writeStringField("specVersion", workflow.getSpecVersion());
}

if (workflow.getExtensions() != null && !workflow.getExpressionLang().isEmpty()) {
gen.writeStringField("expressionLang", workflow.getExpressionLang());
}

if (workflow.isKeepActive()) {
gen.writeBooleanField("keepActive", workflow.isKeepActive());
}

if (workflow.isAutoRetries()) {
gen.writeBooleanField("autoRetries", workflow.isAutoRetries());
}

if (workflow.getMetadata() != null && !workflow.getMetadata().isEmpty()) {
gen.writeObjectField("metadata", workflow.getMetadata());
}

if (workflow.getEvents() != null && !workflow.getEvents().getEventDefs().isEmpty()) {
gen.writeArrayFieldStart("events");
for (EventDefinition eventDefinition : workflow.getEvents().getEventDefs()) {
gen.writeObject(eventDefinition);
}
gen.writeEndArray();
}

if (workflow.getFunctions() != null && !workflow.getFunctions().getFunctionDefs().isEmpty()) {
gen.writeArrayFieldStart("functions");
for (FunctionDefinition function : workflow.getFunctions().getFunctionDefs()) {
gen.writeObject(function);
}
gen.writeEndArray();
}

if (workflow.getRetries() != null && !workflow.getRetries().getRetryDefs().isEmpty()) {
gen.writeArrayFieldStart("retries");
for (RetryDefinition retry : workflow.getRetries().getRetryDefs()) {
gen.writeObject(retry);
}
gen.writeEndArray();
}

if (workflow.getErrors() != null && !workflow.getErrors().getErrorDefs().isEmpty()) {
gen.writeArrayFieldStart("errors");
for (ErrorDefinition error : workflow.getErrors().getErrorDefs()) {
gen.writeObject(error);
}
gen.writeEndArray();
}

if (workflow.getSecrets() != null && !workflow.getSecrets().getSecretDefs().isEmpty()) {
gen.writeArrayFieldStart("secrets");
for (String secretDef : workflow.getSecrets().getSecretDefs()) {
gen.writeString(secretDef);
}
gen.writeEndArray();
}

if (workflow.getConstants() != null && !workflow.getConstants().getConstantsDef().isEmpty()) {
gen.writeObjectField("constants", workflow.getConstants().getConstantsDef());
}

if (workflow.getTimeouts() != null) {
gen.writeObjectField("timeouts", workflow.getTimeouts());
}

if (workflow.getAuth() != null && !workflow.getAuth().getAuthDefs().isEmpty()) {
gen.writeObjectField("auth", workflow.getAuth().getAuthDefs());
}

if (workflow.getStates() != null && !workflow.getStates().isEmpty()) {
gen.writeArrayFieldStart("states");
for (State state : workflow.getStates()) {
gen.writeObject(state);
}
gen.writeEndArray();
}

if (workflow.getExtensions() != null && !workflow.getExtensions().isEmpty()) {
gen.writeArrayFieldStart("extensions");
for (Extension extension : workflow.getExtensions()) {
gen.writeObject(extension);
}
gen.writeEndArray();
}

gen.writeEndObject();
}

protected static String generateUniqueId() {
try {
MessageDigest salt = MessageDigest.getInstance("SHA-256");

salt.update(UUID.randomUUID().toString().getBytes("UTF-8"));
return bytesToHex(salt.digest());
} catch (Exception e) {
return UUID.randomUUID().toString();
}
}

protected static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

public WorkflowSerializer() {
this(Workflow.class);
}

protected WorkflowSerializer(Class<Workflow> t) {
super(t);
}

protected static String generateUniqueId() {
try {
MessageDigest salt = MessageDigest.getInstance("SHA-256");
salt.update(UUID.randomUUID().toString().getBytes("UTF-8"));
return bytesToHex(salt.digest());
} catch (Exception e) {
return UUID.randomUUID().toString();
}
}

protected static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}

// Helper to write either an array of items or a single string reference
private <T> void writeListOrRef(
JsonGenerator gen,
SerializerProvider prov,
String fieldName,
List<T> items,
String refValue) throws IOException {
if (items != null && !items.isEmpty()) {
gen.writeArrayFieldStart(fieldName);
for (T item : items) {
prov.defaultSerializeValue(item, gen);
}
gen.writeEndArray();
} else if (refValue != null) {
gen.writeStringField(fieldName, refValue);
}
}

@Override
public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider provider)
throws IOException {

gen.writeStartObject();

// --- ID / key / basic fields ---
if (workflow.getId() != null && !workflow.getId().isEmpty()) {
gen.writeStringField("id", workflow.getId());
} else {
gen.writeStringField("id", generateUniqueId());
}
if (workflow.getKey() != null) {
gen.writeStringField("key", workflow.getKey());
}
gen.writeStringField("name", workflow.getName());
if (workflow.getDescription() != null && !workflow.getDescription().isEmpty()) {
gen.writeStringField("description", workflow.getDescription());
}
if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) {
gen.writeStringField("version", workflow.getVersion());
}
if (workflow.getAnnotations() != null && !workflow.getAnnotations().isEmpty()) {
gen.writeObjectField("annotations", workflow.getAnnotations());
}
if (workflow.getDataInputSchema() != null) {
if (workflow.getDataInputSchema().getSchema() != null
&& !workflow.getDataInputSchema().getSchema().isEmpty()
&& workflow.getDataInputSchema().isFailOnValidationErrors()) {
gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getSchema());
} else if (workflow.getDataInputSchema().getSchema() != null
&& !workflow.getDataInputSchema().getSchema().isEmpty()
&& !workflow.getDataInputSchema().isFailOnValidationErrors()) {
gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema());
}
}
if (workflow.getStart() != null) {
gen.writeObjectField("start", workflow.getStart());
}
if (workflow.getSpecVersion() != null && !workflow.getSpecVersion().isEmpty()) {
gen.writeStringField("specVersion", workflow.getSpecVersion());
}
if (workflow.getExpressionLang() != null && !workflow.getExpressionLang().isEmpty()) {
gen.writeStringField("expressionLang", workflow.getExpressionLang());
}
if (workflow.isKeepActive()) {
gen.writeBooleanField("keepActive", workflow.isKeepActive());
}
if (workflow.isAutoRetries()) {
gen.writeBooleanField("autoRetries", workflow.isAutoRetries());
}
if (workflow.getMetadata() != null && !workflow.getMetadata().isEmpty()) {
gen.writeObjectField("metadata", workflow.getMetadata());
}

// --- Collections or references ---
if (workflow.getEvents() != null) {
writeListOrRef(gen, provider,
"events",
workflow.getEvents().getEventDefs(),
workflow.getEvents().getRefValue());
}
if (workflow.getFunctions() != null) {
writeListOrRef(gen, provider,
"functions",
workflow.getFunctions().getFunctionDefs(),
workflow.getFunctions().getRefValue());
}
if (workflow.getRetries() != null) {
writeListOrRef(gen, provider,
"retries",
workflow.getRetries().getRetryDefs(),
workflow.getRetries().getRefValue());
}
if (workflow.getErrors() != null) {
writeListOrRef(gen, provider,
"errors",
workflow.getErrors().getErrorDefs(),
workflow.getErrors().getRefValue());
}
if (workflow.getSecrets() != null) {
writeListOrRef(gen, provider,
"secrets",
workflow.getSecrets().getSecretDefs(),
workflow.getSecrets().getRefValue());
}

// --- Always-array fields ---
if (workflow.getStates() != null && !workflow.getStates().isEmpty()) {
gen.writeArrayFieldStart("states");
for (State state : workflow.getStates()) {
gen.writeObject(state);
}
gen.writeEndArray();
}
if (workflow.getExtensions() != null && !workflow.getExtensions().isEmpty()) {
gen.writeArrayFieldStart("extensions");
for (Extension ext : workflow.getExtensions()) {
gen.writeObject(ext);
}
gen.writeEndArray();
}

gen.writeEndObject();
}
return new String(hexChars);
}
}
Loading