This is an automated email from the ASF dual-hosted git repository.

yamer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git


The following commit(s) were added to refs/heads/main by this push:
     new bf68c4c76b [kie-issues#1831] Return coerced null from decision with 
knowledge requirements (#6408)
bf68c4c76b is described below

commit bf68c4c76b67cb3459f0a2e30a700dc4be834dae
Author: Yeser Amer <[email protected]>
AuthorDate: Thu Jul 31 18:05:48 2025 +0200

    [kie-issues#1831] Return coerced null from decision with knowledge 
requirements (#6408)
    
    * Fixing decision_bkm_002 and invoke_001 tests
    
    * Code smells fixed.
    
    * Reverted code smells
    
    * invoke_005 test case fixed
    
    * Change Request
---
 .../core/ast/DMNFunctionDefinitionEvaluator.java   |  4 +--
 .../java/org/kie/dmn/core/impl/DMNRuntimeImpl.java | 36 ++++++++++++++++------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git 
a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/ast/DMNFunctionDefinitionEvaluator.java
 
b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/ast/DMNFunctionDefinitionEvaluator.java
index 4888dfe21d..3c31816961 100644
--- 
a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/ast/DMNFunctionDefinitionEvaluator.java
+++ 
b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/ast/DMNFunctionDefinitionEvaluator.java
@@ -163,9 +163,8 @@ public class DMNFunctionDefinitionEvaluator
                         if ((!performRuntimeTypeCheck) || 
parameters.get(i).type.isAssignableValue(coercedObject)) {
                             ctx.setValue(paramName, coercedObject);
                         } else {
-                            ctx.setValue(paramName, null);
                             MsgUtil.reportMessage(logger,
-                                                  DMNMessage.Severity.WARN,
+                                                  DMNMessage.Severity.ERROR,
                                                   functionDefinition,
                                                   resultContext,
                                                   null,
@@ -174,6 +173,7 @@ public class DMNFunctionDefinitionEvaluator
                                                   paramName,
                                                   parameters.get(i).type,
                                                   params[i]);
+                            return null;
                         }
                     }
                     resultContext.setContext( dmnContext );
diff --git 
a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java 
b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java
index a22c7eb065..ee53fab7c5 100644
--- 
a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java
+++ 
b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java
@@ -447,19 +447,35 @@ public class DMNRuntimeImpl
     }
 
     public static Object coerceUsingType(Object value, DMNType type, boolean 
typeCheck, BiConsumer<Object, DMNType> nullCallback) {
-        Object result = value;
+        if (typeCheck) {
+            if (type.isAssignableValue(value)) {
+                return coerceSingleItemCollectionToValue(value, type);
+            } else {
+                nullCallback.accept(value, type);
+                return null;
+            }
+        } else {
+            return coerceSingleItemCollectionToValue(value, type);
+        }
+    }
+
+    /**
+     * Checks a type and if it is not a collection type, checks if the 
specified value is a collection
+     * that contains only a single value and if yes, coerces the collection to 
the single item itself.
+     * E.g. [1] becomes 1. Basically it unwraps the single item from a 
collection, if it is required.
+     *
+     * @param value Value that is checked and potentially coerced to a single 
item.
+     * @param type Required type. Based on this type, it is determined, if the 
coercion happens.
+     *             If the requirement is for a non-collection type and the 
value is a single item collection,
+     *             the coercion happens.
+     * @return If all requirements are met, returns coerced value. Otherwise 
returns the original value.
+     */
+    private static Object coerceSingleItemCollectionToValue(Object value, 
DMNType type) {
         if (!type.isCollection() && value instanceof Collection && 
((Collection<?>) value).size() == 1) {
             // as per Decision evaluation result.
-            result = ((Collection<?>) value).toArray()[0];
-        }
-        if (!typeCheck) {
-            return result;
-        }
-        if (type.isAssignableValue(result)) {
-            return result;
+            return ((Collection<?>) value).toArray()[0];
         } else {
-            nullCallback.accept(value, type);
-            return null;
+            return value;
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to