Skip to content

Return null rather than throwing exception when conversion fail#1222

Merged
fjtirado merged 1 commit intoserverlessworkflow:mainfrom
fjtirado:Return_null_rather_than_throwing_exception
Mar 10, 2026
Merged

Return null rather than throwing exception when conversion fail#1222
fjtirado merged 1 commit intoserverlessworkflow:mainfrom
fjtirado:Return_null_rather_than_throwing_exception

Conversation

@fjtirado
Copy link
Collaborator

No description provided.

Copilot AI review requested due to automatic review settings March 10, 2026 19:54
Signed-off-by: fjtirado <ftirados@redhat.com>
@fjtirado fjtirado force-pushed the Return_null_rather_than_throwing_exception branch from 19b166c to 1cfd8ec Compare March 10, 2026 19:55
@fjtirado fjtirado merged commit fac2213 into serverlessworkflow:main Mar 10, 2026
3 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an explicit null-check capability on WorkflowModel and uses it in the Java expression execution path to avoid throwing when a model represents null (notably for typed Java functions).

Changes:

  • Add WorkflowModel#isNull() as a default interface method, with a Jackson-specific override.
  • Update JavaExpressionFactory to return null for typed function arguments when the incoming WorkflowModel is null.
  • Adjust a Func DSL lambda test invocation to use the typed overload.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
impl/model/src/main/java/io/serverlessworkflow/impl/model/jackson/JacksonModel.java Overrides isNull() to use JsonNode#isNull() directly.
impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowModel.java Adds default isNull() implementation based on asJavaObject() == null.
experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/func/JavaExpressionFactory.java Routes typed function argument conversion through a new convert() helper that returns null for null models.
experimental/lambda/src/test/java/io/serverless/workflow/impl/executors/func/FuncDSLDataFlowTransformationHelpersTest.java Updates an exportAs call to the typed overload by passing an explicit argument class.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +66 to +69
.as(argClass)
.orElseThrow(
() ->
new IllegalArgumentException(
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new model.isNull() ? null : ... conversion path isn’t covered by tests. Adding a test that passes a null workflow/task output into a typed Java function and asserts the function receives null (and no exception is thrown) would prevent regressions.

Copilot uses AI. Check for mistakes.
Comment on lines 120 to +126
.exportAs(
(object, workflowContext, taskContextData) -> {
Long taskOutput = output(taskContextData, Long.class);
softly.assertThat(taskOutput).isEqualTo(15L);
return taskOutput * 2;
}))
},
Object.class))
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test now forces the typed overload by passing Object.class, but the lambda doesn’t assert what the object argument actually is. Consider using the concrete expected type (e.g., Long.class) and asserting on the object parameter so the test validates the conversion behavior this PR is changing.

Copilot uses AI. Check for mistakes.
Comment on lines 44 to 45
Object value = descriptor.asObject();
if (value instanceof Function func) {
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger is declared but never used. Either remove it to avoid dead code, or use it (e.g., log conversion failures / null conversions) so it serves a purpose.

Copilot uses AI. Check for mistakes.
Comment on lines +71 to 74
}

@Override
public int priority(ExpressionDescriptor descriptor) {
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IllegalArgumentException message concatenation is missing a space before the target type (currently produces ... to classclass ...). Consider formatting it as ... to class <type> (e.g., using argClass.getName()), and include a separator after class.

Copilot uses AI. Check for mistakes.
Comment on lines +66 to 75
.as(argClass)
.orElseThrow(
() ->
new IllegalArgumentException(
"Cannot convert model " + model.asJavaObject() + " to class" + argClass));
}

@Override
public int priority(ExpressionDescriptor descriptor) {
Object value = descriptor.asObject();
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR title indicates conversions should return null rather than throw when conversion fails, but this still throws for non-null values when model.as(argClass) is empty (and model.as(...) may also throw). If the intent is truly "return null on conversion failure", consider returning null/Optional here instead of throwing, or explicitly adjust the PR title/behavior to match.

Copilot uses AI. Check for mistakes.
fjtirado added a commit to fjtirado/sdk-java that referenced this pull request Mar 13, 2026
fjtirado added a commit to fjtirado/sdk-java that referenced this pull request Mar 13, 2026
This is a follow up of
serverlessworkflow#1222
Signed-off-by: fjtirado <ftirados@redhat.com>
fjtirado added a commit to fjtirado/sdk-java that referenced this pull request Mar 13, 2026
This is a follow up of
serverlessworkflow#1222
Signed-off-by: fjtirado <ftirados@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants