fix(s3): return CommonPrefixes in ListObjects when delimiter is specified (#133)

* fix(s3): return CommonPrefixes in ListObjects when delimiter is specified

ListObjects with a delimiter silently dropped nested keys instead of
grouping them into CommonPrefixes entries. Refactor the delimiter logic
to properly separate direct objects from common prefixes and include
them in the XML response, matching real S3 ListObjectsV2 behavior.

docs(s3): add reference link for using prefixes in ListObjects

* refactor(s3): following copilot review, sort common prefixes and all objects in ListObjects response. Update integration tests to reflect changes in object ordering.

* feat(s3): implement maxKeys limit for ListObjects with delimiter as suggested by copilot in the scope of CommonPrefix fix. Added a test to cover it

* fix(s3): enhance delimiter handling in ListObjects to correctly separate direct objects and common prefixes. Update logic to maintain maxKeys behavior and ensure proper sorting of results, aligning with S3 ListObjectsV2 specifications.

* fix(s3): update ListObjectsResult to include isTruncated flag and adjust XML response in S3Controller to reflect this change. Enhance tests to verify truncation behavior aligns with S3 ListObjectsV2 specifications.

* fix(s3): ensure Prefix element in ListBucketResult is not null by providing an empty string fallback when prefix is null following test results in floci-compatibility-tests
This commit is contained in:
Beytullah Gunduz authored and GitHub committed 2026-04-02 18:34:39 -05:00
1 parent 4529823557
commit 845ac85363
4 files changed
+121 -16

No files matched your search

@@ -161,6 +161,23 @@ class S3IntegrationTest {
@Test
@Order(12)
void listObjectsWithDelimiterReturnsCommonPrefixes() {
given()
.queryParam("delimiter", "/")
.queryParam("list-type", "2")
.when()
.get("/test-bucket")
.then()
.statusCode(200)
.body(containsString("<CommonPrefixes>"))
.body(containsString("<Prefix>data/</Prefix>"))
.body(containsString("<Key>greeting.txt</Key>"))
.body(containsString("<KeyCount>2</KeyCount>"))
.body(containsString("<IsTruncated>false</IsTruncated>"));
}
@Test
@Order(13)
void copyObject() {
given()
.header("x-amz-copy-source", "/test-bucket/greeting.txt")
@@ -186,7 +203,7 @@ class S3IntegrationTest {
}
@Test
@Order(13)
@Order(14)
void deleteObject() {
given()
.when()
@@ -203,7 +220,7 @@ class S3IntegrationTest {
}
@Test
@Order(14)
@Order(15)
void deleteNonEmptyBucketFails() {
given()
.when()
@@ -177,6 +177,43 @@ class S3ServiceTest {
assertEquals(2, docs.size());
}
@Test
void listObjectsWithDelimiterReturnsCommonPrefixes() {
s3Service.createBucket("test-bucket", "us-east-1");
s3Service.putObject("test-bucket", "docs/a.txt", "a".getBytes(), null, null);
s3Service.putObject("test-bucket", "docs/sub/deep.txt", "d".getBytes(), null, null);
s3Service.putObject("test-bucket", "images/pic.jpg", "img".getBytes(), null, null);
s3Service.putObject("test-bucket", "root.txt", "r".getBytes(), null, null);
S3Service.ListObjectsResult result = s3Service.listObjectsWithPrefixes("test-bucket", null, "/", 1000);
List<String> rootKeys = result.objects().stream().map(S3Object::getKey).toList();
assertEquals(List.of("root.txt"), rootKeys);
assertEquals(List.of("docs/", "images/"), result.commonPrefixes());
assertFalse(result.isTruncated());
S3Service.ListObjectsResult docsResult = s3Service.listObjectsWithPrefixes("test-bucket", "docs/", "/", 1000);
List<String> docKeys = docsResult.objects().stream().map(S3Object::getKey).toList();
assertEquals(List.of("docs/a.txt"), docKeys);
assertEquals(List.of("docs/sub/"), docsResult.commonPrefixes());
assertFalse(docsResult.isTruncated());
}
@Test
void listObjectsWithDelimiterRespectsMaxKeysAcrossObjectsAndPrefixes() {
s3Service.createBucket("test-bucket", "us-east-1");
s3Service.putObject("test-bucket", "a.txt", "a".getBytes(), null, null);
s3Service.putObject("test-bucket", "b.txt", "b".getBytes(), null, null);
s3Service.putObject("test-bucket", "dir1/file.txt", "f1".getBytes(), null, null);
s3Service.putObject("test-bucket", "dir2/file.txt", "f2".getBytes(), null, null);
s3Service.putObject("test-bucket", "dir3/file.txt", "f3".getBytes(), null, null);
S3Service.ListObjectsResult result = s3Service.listObjectsWithPrefixes("test-bucket", null, "/", 3);
int totalReturned = result.objects().size() + result.commonPrefixes().size();
assertEquals(3, totalReturned, "combined objects + commonPrefixes must not exceed maxKeys");
assertTrue(result.isTruncated(), "result should be truncated when maxKeys < total entries");
}
@Test
void listObjectsInNonExistentBucketThrows() {
assertThrows(AwsException.class, () ->