Skip to content

Commit 3282664

Browse files
authored
RSDEV-488: fix possible NPE when populating response to public API /folders endpoints (#249) (#256)
1 parent 9ce6f90 commit 3282664

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/main/java/com/researchspace/api/v1/model/RecordTreeItemInfo.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.researchspace.core.util.jsonserialisers.ISO8601DateTimeSerialiser;
1010
import com.researchspace.model.User;
1111
import com.researchspace.model.record.BaseRecord;
12+
import com.researchspace.model.record.Folder;
13+
import java.util.Optional;
1214
import lombok.Data;
1315
import lombok.EqualsAndHashCode;
1416
import lombok.NoArgsConstructor;
@@ -49,13 +51,16 @@ public class RecordTreeItemInfo extends IdentifiableNameableApiObject {
4951
@JsonProperty("type")
5052
private ApiRecordType type = null;
5153

52-
public RecordTreeItemInfo(BaseRecord record, User authorisedSubject) {
54+
public RecordTreeItemInfo(BaseRecord record, User user) {
5355
super(record.getId(), record.getGlobalIdentifier(), record.getName());
5456
setCreatedMillis(record.getCreationDateMillis());
5557
setLastModifiedMillis(record.getModificationDateMillis());
5658
// set parent folder if user is owner of document else null
57-
if (authorisedSubject.equals(record.getOwner()) && record.hasParents()) {
58-
setParentFolderId(record.getOwnerParent().get().getId());
59+
if (user.equals(record.getOwner()) && record.hasParents()) {
60+
Optional<Folder> parentForCurrentUser = record.getOwnerOrSharedParentForUser(user);
61+
if (parentForCurrentUser.isPresent()) {
62+
setParentFolderId(parentForCurrentUser.get().getId());
63+
}
5964
}
6065
setOwner(new ApiUser(record.getOwner()));
6166
if (record.isNotebook()) {

src/main/java/com/researchspace/webapp/controller/GalleryController.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,7 @@ public AjaxReturnObject<List<RecordInformation>> getDocumentsLinkedToAttachment(
819819

820820
/**
821821
* Gets record information for a list of IDs (and revision numbers) of EcatMediaFiles
822-
* If info cannot be retrieved, value of map will be null and there will be an error
823-
*
822+
*
824823
* @param ids
825824
* @param revisions
826825
* @return a map with keys in "$id-$revision" format

src/test/java/com/researchspace/api/v1/controller/FolderApiControllerMVCIT.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public void traverseFolderAndSharedFolder() throws Exception {
262262
result = deleteByIdFailsWith4xxStatus(u1, apiKey, info.getId());
263263
}
264264

265-
// ser shouldn't be able to create content inside these folders either.
265+
// user shouldn't be able to create content inside these folders either.
266266
ApiFolder folderPost = new ApiFolder();
267267
folderPost.setName("TestFolder");
268268
folderPost.setParentFolderId(sharedId);
@@ -294,6 +294,30 @@ public void traverseFolderAndSharedFolder() throws Exception {
294294
deleteByIdFailsWith4xxStatus(group.getPi(), piApiKey, getRootFolderForUser(u1).getId());
295295
}
296296

297+
@Test
298+
public void listOwnSharedFolderInNonOwnedFolder_RSDEV_488() throws Exception {
299+
TestGroup group = createTestGroup(2);
300+
User u1 = group.u1();
301+
logoutAndLoginAs(u1);
302+
String apiKey = createNewApiKeyForUser(u1);
303+
304+
Long groupSharedFolderId = group.getGroup().getCommunalGroupFolderId();
305+
ApiFolder folderPost = new ApiFolder();
306+
folderPost.setName("u1 test folder in group's shared folder");
307+
folderPost.setParentFolderId(groupSharedFolderId);
308+
MvcResult result = this.mockMvc.perform(folderCreate(u1, apiKey, folderPost)).andReturn();
309+
ApiFolder created = getFromJsonResponseBody(result, ApiFolder.class);
310+
assertNotNull(created.getId());
311+
312+
// listing of top-level folder works fine for u1 (RSDEV-488)
313+
ApiRecordTreeItemListing sharedFolderListing =
314+
performFolderListingById(u1, apiKey, groupSharedFolderId);
315+
assertEquals(1, sharedFolderListing.getTotalHits().intValue());
316+
RecordTreeItemInfo retrievedFolder = sharedFolderListing.getRecords().get(0);
317+
assertEquals(u1.getUsername(), retrievedFolder.getOwner().getUsername());
318+
assertEquals(groupSharedFolderId, retrievedFolder.getParentFolderId());
319+
}
320+
297321
private long getIdFromNameForListing(String name, ApiRecordTreeItemListing listing) {
298322
return listing.getRecords().stream()
299323
.filter(item -> item.getName().equals(name))

0 commit comments

Comments
 (0)