fix: Allow serializing child widget if it is a substring of the conta… (#35311)

This commit is contained in:
Nidhi 2024-08-01 23:09:34 +05:30 committed by GitHub
parent e8cb460b77
commit e168219750
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 14 deletions

View File

@ -257,17 +257,10 @@ public class FileUtilsCEImpl implements FileInterface {
Map<String, JSONObject> 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());

View File

@ -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<String, List<String>> calculateParentDirectories(List<String> 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;
}
}

View File

@ -19,7 +19,7 @@ import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class DSLTransformHelperTest {
public class DSLTransformerHelperTest {
private Map<String, JSONObject> jsonMap;
private Map<String, List<String>> 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/");
}
}