-
Notifications
You must be signed in to change notification settings - Fork 50
Add number type conversion validation to ensure proper casting between numeric types #1205
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,8 @@ public abstract class AbstractWorkflowModel implements WorkflowModel { | |||||||||||||||||
|
|
||||||||||||||||||
| protected abstract <T> Optional<T> convert(Class<T> clazz); | ||||||||||||||||||
|
|
||||||||||||||||||
| protected abstract <T> Optional<T> asNumber(Class<?> targetNumberClass); | ||||||||||||||||||
|
||||||||||||||||||
| protected abstract <T> Optional<T> asNumber(Class<?> targetNumberClass); | |
| @SuppressWarnings("unchecked") | |
| protected <T> Optional<T> asNumber(Class<?> targetNumberClass) { | |
| if (!Number.class.isAssignableFrom(targetNumberClass)) { | |
| return Optional.empty(); | |
| } | |
| return (Optional<T>) convert((Class<T>) targetNumberClass); | |
| } |
Copilot
AI
Mar 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new numeric path uses Class<?> + unchecked casts in implementations. To improve type safety and remove the need for @SuppressWarnings(\"unchecked\"), consider changing the hook to a bounded generic signature such as protected abstract <N extends Number> Optional<N> asNumber(Class<N> targetNumberClass); and calling it with clazz.asSubclass(Number.class) (or equivalent) in this branch.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,29 @@ public Optional<Number> asNumber() { | |
| return node.isNumber() ? Optional.of(node.asLong()) : Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| protected <T> Optional<T> asNumber(Class<?> targetNumberClass) { | ||
| if (!node.isNumber()) { | ||
| return Optional.empty(); | ||
| } | ||
| if (targetNumberClass == Integer.class) { | ||
| return (Optional<T>) Optional.of(node.asInt()); | ||
| } else if (targetNumberClass == Long.class) { | ||
| return (Optional<T>) Optional.of(node.asLong()); | ||
| } else if (targetNumberClass == Double.class) { | ||
| return (Optional<T>) Optional.of(node.asDouble()); | ||
| } else if (targetNumberClass == Float.class) { | ||
| return (Optional<T>) Optional.of((float) node.asDouble()); | ||
| } else if (targetNumberClass == Short.class) { | ||
| return (Optional<T>) Optional.of((short) node.asInt()); | ||
| } else if (targetNumberClass == Byte.class) { | ||
| return (Optional<T>) Optional.of((byte) node.asInt()); | ||
| } else { | ||
| return (Optional<T>) Optional.of(node.numberValue()); | ||
| } | ||
| } | ||
|
Comment on lines
+72
to
+93
|
||
|
|
||
| @Override | ||
| public String toString() { | ||
| return node.toPrettyString(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,179 @@ | ||||
| /* | ||||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||||
| * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | ||||
| * You may obtain a copy of the License at | ||||
| * | ||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||
| * | ||||
| * Unless required by applicable law or agreed to in writing, software | ||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | ||||
| * limitations under the License. | ||||
| */ | ||||
| package io.serverlessworkflow.impl.test; | ||||
|
|
||||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function; | ||||
|
|
||||
| import io.serverlessworkflow.api.types.Workflow; | ||||
| import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; | ||||
| import io.serverlessworkflow.impl.WorkflowApplication; | ||||
| import io.serverlessworkflow.impl.WorkflowModel; | ||||
| import java.util.function.Function; | ||||
| import org.junit.jupiter.api.Assertions; | ||||
| import org.junit.jupiter.api.Test; | ||||
|
|
||||
| public class WorkflowNumberConversionTest { | ||||
|
|
||||
| @Test | ||||
| void incompatible_test() { | ||||
| Workflow workflow = | ||||
| FuncWorkflowBuilder.workflow("numbers") | ||||
| .tasks( | ||||
| function( | ||||
| "scoreProposal", | ||||
| (Proposal input) -> { | ||||
| Integer score = calculateScore(input.abstractText()); | ||||
| System.out.println("Score calculated having the result as: " + score); | ||||
mcruzdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| System.out.println("Score calculated having the result as: " + score); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as the Jackson implementation: for targets like
BigDecimal.class/BigInteger.class, theelsebranch returns the originalnumwithout ensuring it's an instance oftargetNumberClass, which can surface as aClassCastExceptionformodel.as(BigDecimal.class). Consider explicitly converting for common types (e.g.,BigDecimal,BigInteger) and/or returning empty when!targetNumberClass.isInstance(convertedValue).