From ab6fa7de82e06e13c9498595c3ad02a4dc1d9a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=C3=ADel=20P=2E=20Purkh=C3=BAs?= Date: Thu, 29 Aug 2024 09:05:57 +0000 Subject: [PATCH 1/2] Allow empty strings ## What? DynamoDB has supported empty strings for non-key attributes since mid 2020: https://aws.amazon.com/about-aws/whats-new/2020/05/amazon-dynamodb-now-supports-empty-values-for-non-key-string-and-binary-attributes-in-dynamodb-tables/ --- src/FSharp.AWS.DynamoDB/Picklers/PrimitivePicklers.fs | 2 -- tests/FSharp.AWS.DynamoDB.Tests/RecordGenerationTests.fs | 4 +--- tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs | 4 ++++ 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/FSharp.AWS.DynamoDB/Picklers/PrimitivePicklers.fs b/src/FSharp.AWS.DynamoDB/Picklers/PrimitivePicklers.fs index f2e2ba5..277feb4 100644 --- a/src/FSharp.AWS.DynamoDB/Picklers/PrimitivePicklers.fs +++ b/src/FSharp.AWS.DynamoDB/Picklers/PrimitivePicklers.fs @@ -36,8 +36,6 @@ type StringPickler() = override _.Pickle s = if isNull s then AttributeValue(NULL = true) - elif s = "" then - invalidOp "empty strings not supported by DynamoDB." else AttributeValue(s) |> Some diff --git a/tests/FSharp.AWS.DynamoDB.Tests/RecordGenerationTests.fs b/tests/FSharp.AWS.DynamoDB.Tests/RecordGenerationTests.fs index 7d4c1c0..2d48b67 100644 --- a/tests/FSharp.AWS.DynamoDB.Tests/RecordGenerationTests.fs +++ b/tests/FSharp.AWS.DynamoDB.Tests/RecordGenerationTests.fs @@ -30,10 +30,8 @@ module ``Record Generation Tests`` = test <@ r' = r @> with // account for random inputs not supported by the library - | :? InvalidOperationException as e when e.Message = "empty strings not supported by DynamoDB." -> () | :? ArgumentException as e when - e.Message.Contains "unsupported key name" && e.Message.Contains "should be 1 to 64k long (as utf8)" - -> + e.Message.Contains "unsupported key name" && e.Message.Contains "should be 1 to 64k long (as utf8)" -> () Check.One(config, roundTrip) diff --git a/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs b/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs index 1eadef9..0e153c9 100644 --- a/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs +++ b/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs @@ -18,6 +18,8 @@ module SimpleTableTypes = [] RangeKey: string + EmptyString: string + Value: int64 Tuple: int64 * int64 @@ -46,6 +48,7 @@ type ``Simple Table Operation Tests``(fixture: TableFixture) = let mkItem () = { HashKey = guid () RangeKey = guid () + EmptyString = "" Value = rand () Tuple = rand (), rand () Map = seq { for _ in 0L .. rand () % 5L -> "K" + guid (), rand () } |> Map.ofSeq @@ -139,6 +142,7 @@ type ``TransactWriteItems tests``(fixture: TableFixture) = let mkItem () = { HashKey = guid () RangeKey = guid () + EmptyString = "" Value = rand () Tuple = rand (), rand () Map = seq { for _ in 0L .. rand () % 5L -> "K" + guid (), rand () } |> Map.ofSeq From 688212c5c32c918ce161a2a441e7b660faca0d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=C3=ADel=20P=2E=20Purkh=C3=BAs?= Date: Thu, 5 Sep 2024 15:15:33 +0000 Subject: [PATCH 2/2] Add test for empty key values --- .../SimpleTableOperationTests.fs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs b/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs index 0e153c9..3a7e1c6 100644 --- a/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs +++ b/tests/FSharp.AWS.DynamoDB.Tests/SimpleTableOperationTests.fs @@ -134,6 +134,23 @@ type ``Simple Table Operation Tests``(fixture: TableFixture) = test <@ None = deletedItem @> test <@ not (table.ContainsKey key) @> + [] + [] + [] + [] + let ``Operations with empty key values should fail with a DynamoDB client error`` (hashKey, rangeKey, expectedErrorMsg) = + let value = { mkItem () with HashKey = hashKey; RangeKey = rangeKey } + try + table.PutItem value |> ignore + with :? Amazon.DynamoDBv2.AmazonDynamoDBException as ex -> + test <@ ex.Message = expectedErrorMsg @> + interface IClassFixture type ``TransactWriteItems tests``(fixture: TableFixture) =