mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): word BatchWriteItem key rejections the way AWS does (#3227)
* fix(dynamodb): word BatchWriteItem key rejections the way AWS does A key value can arrive on three surfaces, and AWS words the rejection differently on each. The boolean that told a Key argument from a PutItem item body is now an enum with a third value for BatchWriteItem. BatchWriteItem reports a schema mismatch rather than naming the types, and uses the "are not valid." form for an empty key value. PutItem and TransactWriteItems keep the "Type mismatch for key" form. An empty binary key value is now rejected on every surface. It was accepted before, where the empty string was already rejected. * fix(dynamodb): report a missing BatchWriteItem key as a schema mismatch * fix(dynamodb): use the same empty string key wording on every surface
This commit is contained in:
1 parent
67198c7064
commit
f4954dec02
3 files changed
+128
-27
No files matched your search
@@ -3016,6 +3016,92 @@ class DynamoDbServiceTest {
|
||||
assertEquals(2, result.items().size(), "compact AND with BETWEEN should work");
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchWriteItemReportsAKeySchemaMismatchTheWayAwsDoes() {
|
||||
createUsersTable("us-east-1");
|
||||
var wrongType = mapper.createObjectNode();
|
||||
wrongType.set("userId", attributeValue("N", "5"));
|
||||
|
||||
var error = assertThrows(AwsException.class, () -> service.batchWriteItem(
|
||||
Map.of("Users", List.of(putRequest(wrongType))), "us-east-1"));
|
||||
assertEquals("The provided key element does not match the schema", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchWriteItemReportsAMissingKeyAsASchemaMismatch() {
|
||||
createUsersTable("us-east-1");
|
||||
var noKey = mapper.createObjectNode();
|
||||
noKey.set("name", attributeValue("S", "x"));
|
||||
|
||||
var putError = assertThrows(AwsException.class, () -> service.batchWriteItem(
|
||||
Map.of("Users", List.of(putRequest(noKey))), "us-east-1"));
|
||||
assertEquals("The provided key element does not match the schema", putError.getMessage());
|
||||
|
||||
var deleteError = assertThrows(AwsException.class, () -> service.batchWriteItem(
|
||||
Map.of("Users", List.of(deleteRequest(noKey))), "us-east-1"));
|
||||
assertEquals("The provided key element does not match the schema", deleteError.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putItemStillNamesTheMismatchedKeyTypes() {
|
||||
createUsersTable("us-east-1");
|
||||
var wrongType = mapper.createObjectNode();
|
||||
wrongType.set("userId", attributeValue("N", "5"));
|
||||
|
||||
var error = assertThrows(AwsException.class,
|
||||
() -> service.putItem("Users", wrongType, "us-east-1"));
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for key userId "
|
||||
+ "expected: S actual: N", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everySurfaceUsesTheSameEmptyStringKeyWording() {
|
||||
createUsersTable("us-east-1");
|
||||
ObjectNode emptyKey = mapper.createObjectNode();
|
||||
emptyKey.set("userId", attributeValue("S", ""));
|
||||
String expected = "One or more parameter values are not valid. The AttributeValue for a key "
|
||||
+ "attribute cannot contain an empty string value. Key: userId";
|
||||
|
||||
AwsException batchError = assertThrows(AwsException.class, () -> service.batchWriteItem(
|
||||
Map.of("Users", List.of(putRequest(emptyKey))), "us-east-1"));
|
||||
assertEquals(expected, batchError.getMessage());
|
||||
|
||||
AwsException putError = assertThrows(AwsException.class,
|
||||
() -> service.putItem("Users", emptyKey, "us-east-1"));
|
||||
assertEquals(expected, putError.getMessage());
|
||||
|
||||
AwsException getError = assertThrows(AwsException.class,
|
||||
() -> service.getItem("Users", emptyKey, "us-east-1"));
|
||||
assertEquals(expected, getError.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnEmptyBinaryKeyValue() {
|
||||
service.createTable("Binaries",
|
||||
List.of(new KeySchemaElement("pk", "HASH")),
|
||||
List.of(new AttributeDefinition("pk", "B")),
|
||||
5L, 5L, "us-east-1");
|
||||
ObjectNode emptyBinary = mapper.createObjectNode();
|
||||
emptyBinary.set("pk", attributeValue("B", ""));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.putItem("Binaries", emptyBinary, "us-east-1"));
|
||||
assertEquals("One or more parameter values are not valid. The AttributeValue for a key "
|
||||
+ "attribute cannot contain an empty binary value. Key: pk", error.getMessage());
|
||||
}
|
||||
|
||||
private JsonNode putRequest(ObjectNode item) {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.set("PutRequest", mapper.createObjectNode().set("Item", item));
|
||||
return request;
|
||||
}
|
||||
|
||||
private JsonNode deleteRequest(ObjectNode key) {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.set("DeleteRequest", mapper.createObjectNode().set("Key", key));
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemWithNestedDottedPathSetAndRemove() {
|
||||
createOrdersTable("us-east-1");
|
||||
|
||||
Reference in new issue
Block a user