fix(dynamodb): cap GSI multi-attribute key parts at 4 attributes (#2475)

Closes #2460

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-08-23 16:52:32 -04:00
1 parent 86e1c016cd
commit 13f68670d7
2 files changed
+128

No files matched your search

@@ -146,6 +146,113 @@ class DynamoDbServiceTest {
assertEquals(400, ex.getHttpStatus());
}
@Test
void createTableRejectsGsiWithMoreThanFourSortKeyAttributes() {
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
"GSI1",
List.of(
new KeySchemaElement("PK", "HASH"),
new KeySchemaElement("A", "RANGE"),
new KeySchemaElement("B", "RANGE"),
new KeySchemaElement("C", "RANGE"),
new KeySchemaElement("D", "RANGE"),
new KeySchemaElement("E", "RANGE")),
null, "ALL", null);
AwsException ex = assertThrows(AwsException.class, () -> service.createTable("gsi-too-many-sort-keys",
List.of(new KeySchemaElement("PK", "HASH")),
List.of(
new AttributeDefinition("PK", "S"),
new AttributeDefinition("A", "S"),
new AttributeDefinition("B", "S"),
new AttributeDefinition("C", "S"),
new AttributeDefinition("D", "S"),
new AttributeDefinition("E", "S")),
5L, 5L, List.of(gsi), "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void createTableRejectsGsiWithMoreThanFourPartitionKeyAttributes() {
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
"GSI1",
List.of(
new KeySchemaElement("A", "HASH"),
new KeySchemaElement("B", "HASH"),
new KeySchemaElement("C", "HASH"),
new KeySchemaElement("D", "HASH"),
new KeySchemaElement("E", "HASH")),
null, "ALL", null);
AwsException ex = assertThrows(AwsException.class, () -> service.createTable("gsi-too-many-hash-keys",
List.of(new KeySchemaElement("PK", "HASH")),
List.of(
new AttributeDefinition("PK", "S"),
new AttributeDefinition("A", "S"),
new AttributeDefinition("B", "S"),
new AttributeDefinition("C", "S"),
new AttributeDefinition("D", "S"),
new AttributeDefinition("E", "S")),
5L, 5L, List.of(gsi), "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void updateTableRejectsGsiCreateWithMoreThanFourSortKeyAttributes() {
createUsersTable("eu-west-1");
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
"GSI1",
List.of(
new KeySchemaElement("userId", "HASH"),
new KeySchemaElement("A", "RANGE"),
new KeySchemaElement("B", "RANGE"),
new KeySchemaElement("C", "RANGE"),
new KeySchemaElement("D", "RANGE"),
new KeySchemaElement("E", "RANGE")),
null, "ALL", null);
List<AttributeDefinition> newAttrs = List.of(
new AttributeDefinition("A", "S"), new AttributeDefinition("B", "S"),
new AttributeDefinition("C", "S"), new AttributeDefinition("D", "S"),
new AttributeDefinition("E", "S"));
AwsException ex = assertThrows(AwsException.class, () -> service.updateTable(
"Users", null, null, List.of(gsi), List.of(), newAttrs, "eu-west-1"));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void updateTableRejectsGsiArityWithoutApplyingThroughputChange() {
String region = "eu-west-1";
createUsersTable(region);
GlobalSecondaryIndex invalidGsi = new GlobalSecondaryIndex(
"GSI1",
List.of(
new KeySchemaElement("A", "HASH"),
new KeySchemaElement("B", "HASH"),
new KeySchemaElement("C", "HASH"),
new KeySchemaElement("D", "HASH"),
new KeySchemaElement("E", "HASH")),
null, "ALL", null);
List<AttributeDefinition> newAttrs = List.of(
new AttributeDefinition("A", "S"), new AttributeDefinition("B", "S"),
new AttributeDefinition("C", "S"), new AttributeDefinition("D", "S"),
new AttributeDefinition("E", "S"));
assertThrows(AwsException.class, () -> service.updateTable(
"Users", 10L, 10L, List.of(invalidGsi), List.of(), newAttrs, region));
TableDefinition after = service.describeTable("Users", region);
assertEquals(5L, after.getProvisionedThroughput().getReadCapacityUnits());
assertEquals(5L, after.getProvisionedThroughput().getWriteCapacityUnits());
assertTrue(after.getGlobalSecondaryIndexes().isEmpty());
}
@Test
void describeTable() {
String region = "eu-west-1";