diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutActionServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutActionServiceImpl.java index cc130c0611..453602328b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutActionServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutActionServiceImpl.java @@ -223,6 +223,13 @@ public class LayoutActionServiceImpl implements LayoutActionService { } catch (ParseException e) { log.debug("Exception caught during DSL conversion from string to Json object. ", e); } + // DSL has removed all the old names and replaced it with new name. If the change of name + // was one of the mongoEscaped widgets, then update the names in the set as well + Set mongoEscapedWidgetNames = layout.getMongoEscapedWidgetNames(); + if (mongoEscapedWidgetNames.contains(oldName)) { + mongoEscapedWidgetNames.remove(oldName); + mongoEscapedWidgetNames.add(newName); + } page.setLayouts(layouts); // Since the page has most probably changed, save the page and return. return newPageService.saveUnpublishedPage(page); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java index 805d733a1f..0864b1c957 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/LayoutActionServiceTest.java @@ -17,6 +17,7 @@ import com.appsmith.server.dtos.LayoutActionUpdateDTO; import com.appsmith.server.dtos.LayoutDTO; import com.appsmith.server.dtos.PageDTO; import com.appsmith.server.dtos.RefactorActionNameDTO; +import com.appsmith.server.dtos.RefactorNameDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.helpers.MockPluginExecutor; @@ -616,4 +617,50 @@ public class LayoutActionServiceTest { throwable.getMessage().equals(AppsmithError.DUPLICATE_KEY_USER_ERROR.getMessage(name, FieldName.NAME))) .verify(); } + + @Test + @WithUserDetails(value = "api_user") + public void tableWidgetKeyEscapeRefactorName() { + Mockito.when(pluginExecutorHelper.getPluginExecutor(Mockito.any())).thenReturn(Mono.just(new MockPluginExecutor())); + + JSONObject dsl = new JSONObject(); + dsl.put("widgetName", "Table1"); + dsl.put("type", "TABLE_WIDGET"); + Map primaryColumns = new HashMap(); + JSONObject jsonObject = new JSONObject(Map.of("key", "value")); + primaryColumns.put("_id", jsonObject); + primaryColumns.put("_class", jsonObject); + dsl.put("primaryColumns", primaryColumns); + Layout layout = testPage.getLayouts().get(0); + layout.setDsl(dsl); + + layoutActionService.updateLayout(testPage.getId(), layout.getId(), layout).block(); + + RefactorNameDTO refactorNameDTO = new RefactorNameDTO(); + refactorNameDTO.setPageId(testPage.getId()); + refactorNameDTO.setLayoutId(layout.getId()); + refactorNameDTO.setOldName("Table1"); + refactorNameDTO.setNewName("NewNameTable1"); + + Mono widgetRenameMono = layoutActionService.refactorWidgetName(refactorNameDTO).cache(); + + Mono pageFromRepoMono = widgetRenameMono.then(newPageService.findPageById(testPage.getId(), READ_PAGES, false)); + + StepVerifier + .create(Mono.zip(widgetRenameMono, pageFromRepoMono)) + .assertNext(tuple -> { + LayoutDTO updatedLayout = tuple.getT1(); + PageDTO pageFromRepo = tuple.getT2(); + + String widgetName = (String) updatedLayout.getDsl().get("widgetName"); + assertThat(widgetName).isEqualTo("NewNameTable1"); + + Map primaryColumns1 = (Map) updatedLayout.getDsl().get("primaryColumns"); + assertThat(primaryColumns1.keySet()).containsAll(Set.of(FieldName.MONGO_UNESCAPED_ID, FieldName.MONGO_UNESCAPED_CLASS)); + + Map primaryColumns2 = (Map) pageFromRepo.getLayouts().get(0).getDsl().get("primaryColumns"); + assertThat(primaryColumns2.keySet()).containsAll(Set.of(FieldName.MONGO_ESCAPE_ID, FieldName.MONGO_ESCAPE_CLASS)); + }) + .verifyComplete(); + } }