Skip to content

Commit

Permalink
fix(dynamodb-enhanced): Handle unicode control characters in toJson f…
Browse files Browse the repository at this point in the history
…unction (#5055)

The JsonParseException was occurring due to the toJson function
of EnhancedDocument not handling unicode control characters properly.
This fix ensures that unicode control characters are properly escaped
using backslashes to be included in string values, preventing the
Illegal unquoted character exception
  • Loading branch information
joviegas authored Apr 1, 2024
1 parent e0dcc89 commit 81ebe57
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "Amazon DynamoDB Enhanced Client",
"contributor": "",
"description": "Fixed toJson function in EnhancedDocument API to properly handle unicode control characters by escaping them with backslashes, preventing JsonParseException."
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public static String addEscapeCharacters(String input) {
output.append("\\\""); // double-quote character
break;
default:
output.append(ch);
if (Character.isISOControl(ch)) {
output.append(String.format("\\u%04X", (int) ch));
} else {
output.append(ch);
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private static Stream<Arguments> escapeDocumentStrings() {
, Arguments.of("\n", "{\"key\":\"\\n\"}")
, Arguments.of("\r", "{\"key\":\"\\r\"}")
, Arguments.of("\f", "{\"key\":\"\\f\"}")
, Arguments.of("\u001a \u001A", "{\"key\":\"\\u001A \\u001A\"}")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ private void initializeTestData() {

.build());

testDataList.add(dataBuilder().scenario("unicodeString")
.ddbItemMap(map().withKeyValue("stringKey", AttributeValue.fromS("\u001a \u000e")).get())
.enhancedDocument(
((DefaultEnhancedDocument.DefaultBuilder)
DefaultEnhancedDocument.builder()).putObject("stringKey", "\u001a \u000e")
.addAttributeConverterProvider(defaultProvider()).build())
.attributeConverterProvider(defaultProvider())
.json("{\"stringKey\":\"\\u001A \\u000E\"}")

.build());

testDataList.add(dataBuilder().scenario("record")

.ddbItemMap(map().withKeyValue("uniqueId", AttributeValue.fromS("id-value"))
Expand Down

0 comments on commit 81ebe57

Please sign in to comment.