Skip to content

Commit

Permalink
Merge pull request #491 from camunda-community-hub/442-variable-names
Browse files Browse the repository at this point in the history
Added name field to variable
  • Loading branch information
jonathanlukas authored Oct 13, 2023
2 parents 36be87f + 7674131 commit 5e6746a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Variable {

String DEFAULT_NAME = "$NULL$";
String name() default DEFAULT_NAME;
String value() default DEFAULT_NAME;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Zeebe JobHandler that invokes a Spring bean
Expand Down Expand Up @@ -93,7 +94,7 @@ private List<Object> createParameters(JobClient jobClient, ActivatedJob job, Lis
} else if (ActivatedJob.class.isAssignableFrom(clazz)) {
arg = job;
} else if (param.getParameterInfo().isAnnotationPresent(Variable.class) || param.getParameterInfo().isAnnotationPresent(ZeebeVariable.class)) {
String paramName = param.getParameterName();
String paramName = getVariableName(param);
Object variableValue = job.getVariablesAsMap().get(paramName);
try {
arg = mapZeebeVariable(variableValue, param.getParameterInfo().getType());
Expand All @@ -119,6 +120,23 @@ private List<Object> createParameters(JobClient jobClient, ActivatedJob job, Lis
return args;
}

private String getVariableName(ParameterInfo param) {
if (param.getParameterInfo().isAnnotationPresent(Variable.class)) {
String nameFromAnnotation = param.getParameterInfo().getAnnotation(Variable.class).name();
if (!Objects.equals(nameFromAnnotation, Variable.DEFAULT_NAME)) {
LOG.trace("Extracting name {} from Variable.name", nameFromAnnotation);
return nameFromAnnotation;
}
String valueFromAnnotation = param.getParameterInfo().getAnnotation(Variable.class).value();
if (!Objects.equals(valueFromAnnotation, Variable.DEFAULT_NAME)) {
LOG.trace("Extracting name {} from Variable.value", valueFromAnnotation);
return valueFromAnnotation;
}
}
LOG.trace("Extracting variable name from parameter name");
return param.getParameterName();
}

public static FinalCommandStep createCompleteCommand(JobClient jobClient, ActivatedJob job, Object result) {
CompleteJobCommandStep1 completeCommand = jobClient.newCompleteCommand(job.getKey());
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public MetricsRecorder testMetricsRecorder() {
private static boolean calledTest4 = false;

private static boolean calledTest6 = false;
private static boolean calledTest7 = false;
private static String test7Var = null;
private static ComplexTypeDTO test6ComplexTypeDTO = null;
private static String test6Var2 = null;

Expand Down Expand Up @@ -219,6 +221,25 @@ void testShouldDeserializeComplexTypeZebeeVariable() {
assertEquals("stringValue", test6Var2);
}

@JobWorker(type = "test7")
public void handleTest7(@Variable(name="class")String variableWithKeywordAsName){
calledTest7 = true;
test7Var = variableWithKeywordAsName;
}

@Test
void shouldInjectVariableWithKeywordAsName(){
BpmnModelInstance bpmnModel = Bpmn.createExecutableProcess("test7").startEvent().serviceTask().zeebeJobType("test7").endEvent().done();
client.newDeployResourceCommand().addProcessModel(bpmnModel, "test7.bpmn").send().join();
ProcessInstanceEvent processInstance = startProcessInstance(client, "test7", Map.of("class", "alpha"));
waitForProcessInstanceCompleted(processInstance);
assertTrue(calledTest7);
assertNotNull(test7Var);
assertEquals("alpha",test7Var);
}



private ProcessInstanceEvent startProcessInstance(ZeebeClient client, String bpmnProcessId) {
return startProcessInstance(client, bpmnProcessId, new HashMap<>());
}
Expand Down

0 comments on commit 5e6746a

Please sign in to comment.