From e168219750f33e1c4165022413e2aaabc26e7f2b Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 1 Aug 2024 23:09:34 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20Allow=20serializing=20child=20widget=20i?= =?UTF-8?q?f=20it=20is=20a=20substring=20of=20the=20conta=E2=80=A6=20(#353?= =?UTF-8?q?11)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../appsmith/git/files/FileUtilsCEImpl.java | 13 +++------- .../git/helpers/DSLTransformerHelper.java | 24 ++++++++++++++++--- ...est.java => DSLTransformerHelperTest.java} | 11 ++++++++- 3 files changed, 34 insertions(+), 14 deletions(-) rename app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/{DSLTransformHelperTest.java => DSLTransformerHelperTest.java} (96%) diff --git a/app/server/appsmith-git/src/main/java/com/appsmith/git/files/FileUtilsCEImpl.java b/app/server/appsmith-git/src/main/java/com/appsmith/git/files/FileUtilsCEImpl.java index fafe489f32..bc57788137 100644 --- a/app/server/appsmith-git/src/main/java/com/appsmith/git/files/FileUtilsCEImpl.java +++ b/app/server/appsmith-git/src/main/java/com/appsmith/git/files/FileUtilsCEImpl.java @@ -257,17 +257,10 @@ public class FileUtilsCEImpl implements FileInterface { Map result = DSLTransformerHelper.flatten( new JSONObject(applicationGitReference.getPageDsl().get(pageName))); result.forEach((key, jsonObject) -> { - // get path with splitting the name via key String widgetName = key.substring(key.lastIndexOf(CommonConstants.DELIMITER_POINT) + 1); - String childPath = key.replace(CommonConstants.MAIN_CONTAINER, CommonConstants.EMPTY_STRING) - .replace(CommonConstants.DELIMITER_POINT, CommonConstants.DELIMITER_PATH); - // Replace the canvas Widget as a child and add it to the same level as parent - childPath = childPath.replaceAll(CANVAS_WIDGET, CommonConstants.EMPTY_STRING); - if (!DSLTransformerHelper.hasChildren(jsonObject) - && !DSLTransformerHelper.isTabsWidget(jsonObject)) { - // Save the widget as a directory or Save the widget as a file - childPath = childPath.replace(widgetName, CommonConstants.EMPTY_STRING); - } + + String childPath = DSLTransformerHelper.getPathToWidgetFile(key, jsonObject, widgetName); + Path path = Paths.get( String.valueOf(pageSpecificDirectory.resolve(CommonConstants.WIDGETS)), childPath); validWidgetToParentMap.put(widgetName, path.toFile().toString()); diff --git a/app/server/appsmith-git/src/main/java/com/appsmith/git/helpers/DSLTransformerHelper.java b/app/server/appsmith-git/src/main/java/com/appsmith/git/helpers/DSLTransformerHelper.java index a989e60039..9d444b857d 100644 --- a/app/server/appsmith-git/src/main/java/com/appsmith/git/helpers/DSLTransformerHelper.java +++ b/app/server/appsmith-git/src/main/java/com/appsmith/git/helpers/DSLTransformerHelper.java @@ -15,6 +15,8 @@ import java.util.Map; import java.util.TreeMap; import java.util.stream.Collectors; +import static com.appsmith.git.constants.CommonConstants.CANVAS_WIDGET; + @Component @RequiredArgsConstructor @Slf4j @@ -60,7 +62,7 @@ public class DSLTransformerHelper { if (children.length() != 0) { for (int i = 0; i < children.length(); i++) { JSONObject child = children.getJSONObject(i); - if (!CommonConstants.CANVAS_WIDGET.equals(child.optString(CommonConstants.WIDGET_TYPE))) { + if (!CANVAS_WIDGET.equals(child.optString(CommonConstants.WIDGET_TYPE))) { jsonObject.remove(CommonConstants.CHILDREN); } else { JSONObject childCopy = new JSONObject(child.toString()); @@ -94,7 +96,7 @@ public class DSLTransformerHelper { } public static boolean isCanvasWidget(JSONObject jsonObject) { - return jsonObject.optString(CommonConstants.WIDGET_TYPE).startsWith(CommonConstants.CANVAS_WIDGET); + return jsonObject.optString(CommonConstants.WIDGET_TYPE).startsWith(CANVAS_WIDGET); } public static Map> calculateParentDirectories(List paths) { @@ -189,7 +191,7 @@ public class DSLTransformerHelper { // Is the children CANVAS_WIDGET if (children.length() == 1) { JSONObject childObject = children.getJSONObject(0); - if (CommonConstants.CANVAS_WIDGET.equals(childObject.optString(CommonConstants.WIDGET_TYPE))) { + if (CANVAS_WIDGET.equals(childObject.optString(CommonConstants.WIDGET_TYPE))) { childObject.put(CommonConstants.CHILDREN, childWidgets); } } else if (children.length() > 1) { // Tabs Widget children mapping case @@ -224,4 +226,20 @@ public class DSLTransformerHelper { } return widgetIdWidgetNameMapping; } + + public static String getPathToWidgetFile(String key, JSONObject jsonObject, String widgetName) { + // get path with splitting the name via key + String childPath = key.replace(CommonConstants.MAIN_CONTAINER, CommonConstants.EMPTY_STRING) + .replace(CommonConstants.DELIMITER_POINT, CommonConstants.DELIMITER_PATH); + // Replace the canvas Widget as a child and add it to the same level as parent + childPath = childPath.replaceAll(CANVAS_WIDGET, CommonConstants.EMPTY_STRING); + if (!DSLTransformerHelper.hasChildren(jsonObject) && !DSLTransformerHelper.isTabsWidget(jsonObject)) { + // Save the widget as a directory or Save the widget as a file + // Only consider widgetName at the end of the childPath to reset + // For example, "foobar/bar" should convert into "foobar/" + childPath = childPath.replaceAll(widgetName + "$", CommonConstants.EMPTY_STRING); + } + + return childPath; + } } diff --git a/app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformHelperTest.java b/app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformerHelperTest.java similarity index 96% rename from app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformHelperTest.java rename to app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformerHelperTest.java index c3823ad91b..612cf17870 100644 --- a/app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformHelperTest.java +++ b/app/server/appsmith-git/src/test/java/com/appsmith/git/helpers/DSLTransformerHelperTest.java @@ -19,7 +19,7 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; -public class DSLTransformHelperTest { +public class DSLTransformerHelperTest { private Map jsonMap; private Map> pathMapping; @@ -296,4 +296,13 @@ public class DSLTransformHelperTest { } } } + + @Test + void testGetPathToWidgetFile_whenChildWidgetIsSubstringOfParent_returnsParentWidgetPath() { + + JSONObject jsonObject = new JSONObject(Map.of("widgetName", "bar")); + String pathToWidgetFile = DSLTransformerHelper.getPathToWidgetFile("foobar.bar", jsonObject, "bar"); + + org.assertj.core.api.Assertions.assertThat(pathToWidgetFile).isEqualTo("foobar/"); + } }