This is an automated email from the ASF dual-hosted git repository.
mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push:
new bb382aa0be Fix the NullPointerException when a grid or form extends a
missing parent in the same file
bb382aa0be is described below
commit bb382aa0bec96ea79a25ff87b2f757c43f2e90a0
Author: Mridul Pathak <[email protected]>
AuthorDate: Wed Oct 28 16:24:33 2026 +0530
Fix the NullPointerException when a grid or form extends a missing parent
in the same file
When a grid or form extends a name that is neither a grid nor a form in the
same file, the fallback lookup of the other widget type called orElseGet with a
null supplier and threw a NullPointerException, so the widget failed to load
instead of logging that the parent was not found and continuing without one.
Use orElse(null) in ModelGrid and ModelSingleForm, and add unit tests for both.
---
.../java/org/apache/ofbiz/widget/model/ModelGrid.java | 2 +-
.../apache/ofbiz/widget/model/ModelSingleForm.java | 2 +-
.../org/apache/ofbiz/widget/model/ModelFormTest.java | 19 +++++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
diff --git
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelGrid.java
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelGrid.java
index 053360fd70..11fac6eb91 100644
---
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelGrid.java
+++
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelGrid.java
@@ -104,7 +104,7 @@ public class ModelGrid extends ModelForm {
gridElements = UtilXml.childElementList(rootElement,
"form");
Element parentElement = gridElements.stream()
.filter(form ->
form.getAttribute("name").equals(parentGrid))
- .findFirst().orElseGet(null);
+ .findFirst().orElse(null);
if (UtilValidate.isNotEmpty(parentElement)) {
parentModel =
FormFactory.createModelForm(parentElement, entityModelReader, visualTheme,
dispatchContext, parentResource, parentGrid);
diff --git
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelSingleForm.java
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelSingleForm.java
index 9f040e3fd3..e53d15cf8c 100644
---
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelSingleForm.java
+++
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelSingleForm.java
@@ -106,7 +106,7 @@ public class ModelSingleForm extends ModelForm {
formElements = UtilXml.childElementList(rootElement,
"grid");
Element parentElement = formElements.stream()
.filter(grid ->
grid.getAttribute("name").equals(parentForm))
- .findFirst().orElseGet(null);
+ .findFirst().orElse(null);
if (UtilValidate.isNotEmpty(parentElement)) {
parent = GridFactory.createModelGrid(parentElement,
entityModelReader, visualTheme,
dispatchContext, parentResource, parentForm);
diff --git
a/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
b/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
index d57299a4c9..a7e327cae8 100644
---
a/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
+++
b/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
@@ -26,6 +26,7 @@ import java.util.Map;
import org.apache.ofbiz.base.conversion.ConversionException;
import org.apache.ofbiz.base.conversion.JSONConverters;
import org.apache.ofbiz.base.util.UtilMisc;
+import org.apache.ofbiz.base.util.UtilXml;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
@@ -34,6 +35,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
+import org.w3c.dom.Element;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@@ -90,6 +92,23 @@ public final class ModelFormTest {
Assertions.assertEquals(parameter.getValue().toString(), "[1, 2]",
"Parameters value isn't the same");
}
+ @Test
+ public void testGridExtendingMissingParentInSameFileLoadsWithoutParent()
throws Exception {
+ ModelGrid grid = new ModelGrid(widgetExtendingMissingParent("grid"),
"test", null, null, null);
+ Assertions.assertNull(grid.getParentModelForm(), "Grid should have no
parent");
+ }
+
+ @Test
+ public void testFormExtendingMissingParentInSameFileLoadsWithoutParent()
throws Exception {
+ ModelSingleForm form = new
ModelSingleForm(widgetExtendingMissingParent("form"), "test", null, null, null);
+ Assertions.assertNull(form.getParentModelForm(), "Form should have no
parent");
+ }
+
+ private static Element widgetExtendingMissingParent(String tagName) throws
Exception {
+ String xml = "<forms><" + tagName + " name=\"Child\"
extends=\"Missing\"/></forms>";
+ return UtilXml.firstChildElement(UtilXml.readXmlDocument(xml, false,
true).getDocumentElement(), tagName);
+ }
+
private static CommonWidgetModels.Parameter
getParameterOnly(ModelForm.UpdateArea updateArea) {
Assertions.assertNotNull(updateArea.getParameterList());
Assertions.assertEquals(updateArea.getParameterList().size(), 1,
"Parameter size should be one");