From 552e71e76f19bc31b7ee40fd084441a10d6294ca Mon Sep 17 00:00:00 2001 From: bkindersleytrulioo <33294630+bkindersleytrulioo@users.noreply.github.com> Date: Wed, 8 Apr 2026 21:05:52 -0700 Subject: [PATCH] fix: DynamoDB DescribeTable returns Projection.NonKeyAttributes (#300) * support nonkeyattributes * attempt integration tests * fix tests --- .../CloudFormationResourceProvisioner.java | 9 ++- .../dynamodb/DynamoDbJsonHandler.java | 25 +++++++- .../dynamodb/model/GlobalSecondaryIndex.java | 14 ++++- .../dynamodb/DynamoDbIntegrationTest.java | 61 +++++++++++++++++-- 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index f65523da14..1a61f6537c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -239,10 +239,17 @@ public class CloudFormationResourceProvisioner { } String projectionType = "ALL"; JsonNode projection = gsiNode.get("Projection"); + List nonKeyAttributes = new ArrayList<>(); if (projection != null && projection.has("ProjectionType")) { projectionType = engine.resolve(projection.get("ProjectionType")); + JsonNode nonKeyAttrArray = projection.path("NonKeyAttributes"); + if (!nonKeyAttrArray.isMissingNode() && nonKeyAttrArray.isArray()){ + for (JsonNode nonKeyAttr : nonKeyAttrArray){ + nonKeyAttributes.add(nonKeyAttr.asText()); + } + } } - gsis.add(new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType)); + gsis.add(new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType, nonKeyAttributes)); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index 96f859c414..c2b4d550c9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -95,7 +95,14 @@ public class DynamoDbJsonHandler { ks.path("AttributeName").asText(), ks.path("KeyType").asText()))); String projectionType = gsiNode.path("Projection").path("ProjectionType").asText("ALL"); - GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType); + JsonNode nonKeyAttrArray = gsiNode.path("Projection").path("NonKeyAttributes"); + List nonKeyAttributes = new ArrayList<>(); + if (!nonKeyAttrArray.isMissingNode() && nonKeyAttrArray.isArray()){ + for (JsonNode nonKeyAttr : nonKeyAttrArray){ + nonKeyAttributes.add(nonKeyAttr.asText()); + } + } + GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType, nonKeyAttributes); JsonNode gsiPt = gsiNode.path("ProvisionedThroughput"); if (!gsiPt.isMissingNode()) { gsi.getProvisionedThroughput().setReadCapacityUnits(gsiPt.path("ReadCapacityUnits").asLong(0)); @@ -416,7 +423,14 @@ public class DynamoDbJsonHandler { ks.path("AttributeName").asText(), ks.path("KeyType").asText()))); String projectionType = createNode.path("Projection").path("ProjectionType").asText("ALL"); - GlobalSecondaryIndex newGsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType); + JsonNode nonKeyAttrArray = createNode.path("Projection").path("NonKeyAttributes"); + List nonKeyAttributes = new ArrayList<>(); + if (!nonKeyAttrArray.isMissingNode() && nonKeyAttrArray.isArray()){ + for (JsonNode nonKeyAttr : nonKeyAttrArray){ + nonKeyAttributes.add(nonKeyAttr.asText()); + } + } + GlobalSecondaryIndex newGsi = new GlobalSecondaryIndex(indexName, gsiKeySchema, null, projectionType, nonKeyAttributes); JsonNode newGsiPt = createNode.path("ProvisionedThroughput"); if (!newGsiPt.isMissingNode()) { newGsi.getProvisionedThroughput().setReadCapacityUnits(newGsiPt.path("ReadCapacityUnits").asLong(0)); @@ -665,6 +679,13 @@ public class DynamoDbJsonHandler { ObjectNode projection = objectMapper.createObjectNode(); projection.put("ProjectionType", gsi.getProjectionType() != null ? gsi.getProjectionType() : "ALL"); + if ("INCLUDE".equals(gsi.getProjectionType())){ + ArrayNode nonKeyAttributes = objectMapper.createArrayNode(); + for (var attr : gsi.getNonKeyAttributes()){ + nonKeyAttributes.add(attr); + } + projection.put("NonKeyAttributes", nonKeyAttributes); + } gsiNode.set("Projection", projection); ObjectNode gsiPt = objectMapper.createObjectNode(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/GlobalSecondaryIndex.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/GlobalSecondaryIndex.java index c57692055d..0a57b57859 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/GlobalSecondaryIndex.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/model/GlobalSecondaryIndex.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.quarkus.runtime.annotations.RegisterForReflection; import java.util.List; +import java.util.ArrayList; @RegisterForReflection @JsonIgnoreProperties(ignoreUnknown = true) @@ -16,17 +17,25 @@ public class GlobalSecondaryIndex { private ProvisionedThroughput provisionedThroughput; private long itemCount; private long indexSizeBytes; + private List nonKeyAttributes; public GlobalSecondaryIndex() { this.provisionedThroughput = new ProvisionedThroughput(0, 0); + this.nonKeyAttributes = new ArrayList(); } public GlobalSecondaryIndex(String indexName, List keySchema, - String indexArn, String projectionType) { + String indexArn, String projectionType, List nonKeyAttributes) { this.indexName = indexName; this.keySchema = keySchema; this.indexArn = indexArn; this.projectionType = projectionType != null ? projectionType : "ALL"; + if ("INCLUDE".equals(this.projectionType) && nonKeyAttributes != null){ + this.nonKeyAttributes = nonKeyAttributes; + } + else { + this.nonKeyAttributes = new ArrayList(); + } this.provisionedThroughput = new ProvisionedThroughput(0, 0); } @@ -42,6 +51,9 @@ public class GlobalSecondaryIndex { public String getProjectionType() { return projectionType; } public void setProjectionType(String projectionType) { this.projectionType = projectionType; } + public List getNonKeyAttributes() { return nonKeyAttributes; } + public void setNonKeyAttributes(List nonKeyAttributes) { this.nonKeyAttributes = nonKeyAttributes; } + public ProvisionedThroughput getProvisionedThroughput() { return provisionedThroughput; } public void setProvisionedThroughput(ProvisionedThroughput provisionedThroughput) { this.provisionedThroughput = provisionedThroughput; } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java index cf783caa8e..dc067550ea 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java @@ -602,6 +602,7 @@ class DynamoDbIntegrationTest { .body("Table.GlobalSecondaryIndexes.size()", equalTo(1)) .body("Table.GlobalSecondaryIndexes[0].IndexName", equalTo("TestGsi")) .body("Table.GlobalSecondaryIndexes[0].IndexStatus", equalTo("ACTIVE")) + .body("Table.GlobalSecondaryIndexes[0].Projection.ProjectionType", equalTo("ALL")) .body("Table.GlobalSecondaryIndexes[0].IndexArn", containsString("/index/TestGsi")) .body("Table.GlobalSecondaryIndexes[0].ProvisionedThroughput", notNullValue()) .body("Table.GlobalSecondaryIndexes[0].ProvisionedThroughput.ReadCapacityUnits", equalTo(0)) @@ -649,9 +650,51 @@ class DynamoDbIntegrationTest { .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndex' }.IndexStatus", equalTo("ACTIVE")) .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndex' }.Projection.ProjectionType", equalTo("KEYS_ONLY")); } + @Test @Order(23) + void updateTableAddGsiWithIncludeProjection() { + given() + .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable") + .contentType(DYNAMODB_CONTENT_TYPE) + .body(""" + { + "TableName": "GsiTestTable", + "AttributeDefinitions": [ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "gsiPk", "AttributeType": "S"}, + {"AttributeName": "gsiSk", "AttributeType": "S"}, + {"AttributeName": "owner", "AttributeType": "S"} + ], + "GlobalSecondaryIndexUpdates": [ + { + "Create": { + "IndexName": "OwnerIndexProj", + "KeySchema": [ + {"AttributeName": "owner", "KeyType": "HASH"}, + {"AttributeName": "pk", "KeyType": "RANGE"} + ], + "Projection": {"ProjectionType": "INCLUDE", "NonKeyAttributes":["TestAttr"]} + } + } + ] + } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("TableDescription.GlobalSecondaryIndexes.size()", equalTo(3)) + .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndexProj' }.IndexStatus", equalTo("ACTIVE")) + .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndexProj' }.Projection.ProjectionType", equalTo("INCLUDE")) + .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndexProj' }.Projection.NonKeyAttributes.size()", equalTo(1)) + .body("TableDescription.GlobalSecondaryIndexes.find { it.IndexName == 'OwnerIndexProj' }.Projection.NonKeyAttributes[0]", equalTo("TestAttr")); + } + + + @Test + @Order(24) void updateTableDeleteGsi() { given() .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable") @@ -672,12 +715,13 @@ class DynamoDbIntegrationTest { .post("/") .then() .statusCode(200) - .body("TableDescription.GlobalSecondaryIndexes.size()", equalTo(1)) - .body("TableDescription.GlobalSecondaryIndexes[0].IndexName", equalTo("OwnerIndex")); + .body("TableDescription.GlobalSecondaryIndexes.size()", equalTo(2)) + .body("TableDescription.GlobalSecondaryIndexes[0].IndexName", equalTo("OwnerIndex")) + .body("TableDescription.GlobalSecondaryIndexes[1].IndexName", equalTo("OwnerIndexProj")); } @Test - @Order(24) + @Order(25) void updateTableDeleteAllGsis() { given() .header("X-Amz-Target", "DynamoDB_20120810.UpdateTable") @@ -690,6 +734,11 @@ class DynamoDbIntegrationTest { "Delete": { "IndexName": "OwnerIndex" } + }, + { + "Delete": { + "IndexName": "OwnerIndexProj" + } } ] } @@ -702,7 +751,7 @@ class DynamoDbIntegrationTest { } @Test - @Order(25) + @Order(26) void describeTableAfterAllGsisDeletion() { given() .header("X-Amz-Target", "DynamoDB_20120810.DescribeTable") @@ -720,7 +769,7 @@ class DynamoDbIntegrationTest { // --- Cleanup --- @Test - @Order(26) + @Order(27) void deleteTable() { given() .header("X-Amz-Target", "DynamoDB_20120810.DeleteTable") @@ -749,7 +798,7 @@ class DynamoDbIntegrationTest { } @Test - @Order(27) + @Order(28) void updateItemListAppend() { // Create a table for this test given()