[Bug Fix] : In case the table being renamed has mongo escaped fields, rename the table name in set keeping a track of these widgets as well (#4195)

This commit is contained in:
Trisha Anand 2021-04-28 17:36:30 +05:30 committed by GitHub
parent 7f7f6f666b
commit 73c2b09dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -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<String> 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);

View File

@ -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<String, Object>();
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<LayoutDTO> widgetRenameMono = layoutActionService.refactorWidgetName(refactorNameDTO).cache();
Mono<PageDTO> 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();
}
}