fix(dynamodb): check the Scan segment pairing after the Select checks (#5857)

* fix(dynamodb): check the Scan segment pairing after the Select checks

* fix(dynamodb): check the Scan Select conflicts before the segment pairing
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-16 23:01:25 -04:00
1 parent 2a012cf9a4
commit 5b12c620aa
4 files changed
+72 -29

No files matched your search

@@ -52,15 +52,19 @@ final class DynamoDbAccessPathValidator {
return partitionKeyValuePlaceholder;
}
static void validateSelection(TableDefinition table, DynamoDbAccessPath accessPath,
String select, String projectionExpression,
JsonNode attributesToGet, JsonNode expressionAttributeNames) {
static void validateSelection(String select, String indexName,
String projectionExpression, JsonNode attributesToGet) {
rejectWithSelect("ProjectionExpression", projectionExpression != null, select);
rejectWithSelect("AttributesToGet", attributesToGet != null, select);
if ("ALL_PROJECTED_ATTRIBUTES".equals(select) && !accessPath.isIndex()) {
if ("ALL_PROJECTED_ATTRIBUTES".equals(select) && indexName == null) {
throw validationException("1 validation error detected: "
+ "ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName");
}
}
static void validateGsiProjection(TableDefinition table, DynamoDbAccessPath accessPath,
String select, String projectionExpression,
JsonNode attributesToGet, JsonNode expressionAttributeNames) {
if (!accessPath.isGlobalSecondaryIndex() || "ALL".equals(accessPath.projectionType())) {
return;
}
@@ -1139,7 +1139,8 @@ public class NativeDynamoDbJsonHandler {
DynamoDbAccessPath queryAccessPath = DynamoDbAccessPath.resolve(queryTable, indexName);
DynamoDbAccessPathValidator.validateQuery(queryTable, queryAccessPath, keyConditions,
keyConditionExpr, filterExpr, queryFilter, exprAttrNames, exprAttrValues);
DynamoDbAccessPathValidator.validateSelection(queryTable, queryAccessPath, select,
DynamoDbAccessPathValidator.validateSelection(select, indexName, projectionExpression, attributesToGet);
DynamoDbAccessPathValidator.validateGsiProjection(queryTable, queryAccessPath, select,
projectionExpression, attributesToGet, exprAttrNames);
rejectConsistentReadOnGsi(request, queryAccessPath);
@@ -1257,19 +1258,6 @@ public class NativeDynamoDbJsonHandler {
List.of("AttributesToGet", "ScanFilter", "ConditionalOperator"),
List.of("ProjectionExpression", "FilterExpression"));
if (segment != null && totalSegments == null) {
throw new AwsException("ValidationException",
"The TotalSegments parameter is required but was not present in the request when Segment parameter is present", 400);
}
if (totalSegments != null && segment == null) {
throw new AwsException("ValidationException",
"The Segment parameter is required but was not present in the request when parameter TotalSegments is present", 400);
}
if (segment != null && totalSegments != null && segment >= totalSegments) {
throw new AwsException("ValidationException",
"The Segment parameter is zero-based and must be less than parameter TotalSegments", 400);
}
DynamoDbExpressionSize.checkReadWithSize(filterExpr, "FilterExpression");
DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression");
DynamoDbAttributeValueValidator.requireNestingWithinLimit(exprAttrValues);
@@ -1295,10 +1283,24 @@ public class NativeDynamoDbJsonHandler {
throw new AwsException("ValidationException",
"1 validation error detected: " + SELECT_NEEDS_PROJECTION, 400);
}
DynamoDbAccessPathValidator.validateSelection(select, indexNameScan,
projectionExpressionScan, attributesToGetScan);
if (segment != null && totalSegments == null) {
throw new AwsException("ValidationException",
"The TotalSegments parameter is required but was not present in the request when Segment parameter is present", 400);
}
if (totalSegments != null && segment == null) {
throw new AwsException("ValidationException",
"The Segment parameter is required but was not present in the request when parameter TotalSegments is present", 400);
}
if (segment != null && totalSegments != null && segment >= totalSegments) {
throw new AwsException("ValidationException",
"The Segment parameter is zero-based and must be less than parameter TotalSegments", 400);
}
TableDefinition scanTable = dynamoDbService.requireTableForItemCall(tableName, region);
DynamoDbAccessPath scanAccessPath = DynamoDbAccessPath.resolve(scanTable, indexNameScan);
DynamoDbAccessPathValidator.validateSelection(scanTable, scanAccessPath, select,
DynamoDbAccessPathValidator.validateGsiProjection(scanTable, scanAccessPath, select,
projectionExpressionScan, attributesToGetScan, exprAttrNames);
rejectConsistentReadOnGsi(request, scanAccessPath);
@@ -327,27 +327,27 @@ class DynamoDbAccessPathValidatorTest {
ObjectNode names = mapper.createObjectNode();
names.put("#summary", "summary");
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateSelection(
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateGsiProjection(
table, gsiPath, "SPECIFIC_ATTRIBUTES", "pk, status, #summary",
null, names));
AwsException projectionError = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateSelection(
() -> DynamoDbAccessPathValidator.validateGsiProjection(
table, gsiPath, "SPECIFIC_ATTRIBUTES", "zeta, details",
null, null));
assertEquals("One or more parameter values were invalid: Global secondary index "
+ "status-index does not project details, zeta", projectionError.getMessage());
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateSelection(
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateGsiProjection(
table, gsiPath, "ALL_ATTRIBUTES", null, null, null));
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateSelection(
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateGsiProjection(
table, lsiPath, "ALL_ATTRIBUTES", null, null, null));
}
@Test
void rejectsTableAllProjectedAttributesAndInvalidSelectProjectionCombination() {
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "ALL_PROJECTED_ATTRIBUTES", null, null, null));
"ALL_PROJECTED_ATTRIBUTES", null, null, null));
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "COUNT", "pk", null, null));
"COUNT", null, "pk", null));
}
@Test
@@ -391,14 +391,14 @@ class DynamoDbAccessPathValidatorTest {
void namesTheSelectValueWhenRejectingAProjectionExpression() {
AwsException allAttributes = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "ALL_ATTRIBUTES", "sk", null, null));
"ALL_ATTRIBUTES", null, "sk", null));
assertEquals("1 validation error detected: "
+ "Cannot specify the ProjectionExpression when choosing to get ALL_ATTRIBUTES",
allAttributes.getMessage());
AwsException count = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "COUNT", "sk", null, null));
"COUNT", null, "sk", null));
assertEquals("1 validation error detected: "
+ "Cannot specify the ProjectionExpression when choosing to get only the Count",
count.getMessage());
@@ -408,14 +408,14 @@ class DynamoDbAccessPathValidatorTest {
void reportsTheProjectionExpressionConflictBeforeTheMissingIndex() {
AwsException both = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "ALL_PROJECTED_ATTRIBUTES", "sk", null, null));
"ALL_PROJECTED_ATTRIBUTES", null, "sk", null));
assertEquals("1 validation error detected: "
+ "Cannot specify the ProjectionExpression when choosing to get ALL_PROJECTED_ATTRIBUTES",
both.getMessage());
AwsException indexOnly = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateSelection(
table, tablePath, "ALL_PROJECTED_ATTRIBUTES", null, null, null));
"ALL_PROJECTED_ATTRIBUTES", null, null, null));
assertEquals("1 validation error detected: "
+ "ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName",
indexOnly.getMessage());
@@ -1282,6 +1282,43 @@ class NativeDynamoDbJsonHandlerTest {
+ "Member must have value greater than or equal to 1", ex.getMessage());
}
@Test
void scanReportsTheMissingProjectionBeforeTheSegmentPairing() {
AwsException ex = expectValidationException("Scan", json("""
{"TableName": "Users", "Select": "SPECIFIC_ATTRIBUTES", "Segment": 5, "TotalSegments": 5}
"""));
assertEquals("1 validation error detected: Must specify the AttributesToGet or ProjectionExpression "
+ "when choosing to get SPECIFIC_ATTRIBUTES", ex.getMessage());
}
@Test
void scanReportsAnUnknownSelectBeforeAMissingTotalSegments() {
AwsException ex = expectValidationException("Scan", json("""
{"TableName": "Users", "Select": "BOGUS", "Segment": 1}
"""));
assertEquals("1 validation error detected: Value 'BOGUS' at 'select' failed to satisfy constraint: "
+ "Member must satisfy enum value set: "
+ "[SPECIFIC_ATTRIBUTES, COUNT, ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES]", ex.getMessage());
}
@Test
void scanReportsACountProjectionBeforeTheSegmentPairingOnAMissingTable() {
AwsException ex = expectValidationException("Scan", json("""
{"TableName": "Missing", "Select": "COUNT", "ProjectionExpression": "a", "Segment": 1}
"""));
assertEquals("1 validation error detected: "
+ "Cannot specify the ProjectionExpression when choosing to get only the Count", ex.getMessage());
}
@Test
void scanReportsAllProjectedWithoutAnIndexBeforeAMissingSegment() {
AwsException ex = expectValidationException("Scan", json("""
{"TableName": "Missing", "Select": "ALL_PROJECTED_ATTRIBUTES", "TotalSegments": 2}
"""));
assertEquals("1 validation error detected: "
+ "ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName", ex.getMessage());
}
@Test
void scanAcceptsSegmentsAtTheBounds() throws Exception {
createUsersTable("eu-west-1");