Skip to content

Commit 81ebe57

Browse files
authored
fix(dynamodb-enhanced): Handle unicode control characters in toJson function (#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
1 parent e0dcc89 commit 81ebe57

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Amazon DynamoDB Enhanced Client",
4+
"contributor": "",
5+
"description": "Fixed toJson function in EnhancedDocument API to properly handle unicode control characters by escaping them with backslashes, preventing JsonParseException."
6+
}

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/document/JsonStringFormatHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ public static String addEscapeCharacters(String input) {
7575
output.append("\\\""); // double-quote character
7676
break;
7777
default:
78-
output.append(ch);
78+
if (Character.isISOControl(ch)) {
79+
output.append(String.format("\\u%04X", (int) ch));
80+
} else {
81+
output.append(ch);
82+
}
7983
break;
8084
}
8185
}

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/document/EnhancedDocumentTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private static Stream<Arguments> escapeDocumentStrings() {
6464
, Arguments.of("\n", "{\"key\":\"\\n\"}")
6565
, Arguments.of("\r", "{\"key\":\"\\r\"}")
6666
, Arguments.of("\f", "{\"key\":\"\\f\"}")
67+
, Arguments.of("\u001a \u001A", "{\"key\":\"\\u001A \\u001A\"}")
6768
);
6869
}
6970

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/document/EnhancedDocumentTestData.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ private void initializeTestData() {
104104

105105
.build());
106106

107+
testDataList.add(dataBuilder().scenario("unicodeString")
108+
.ddbItemMap(map().withKeyValue("stringKey", AttributeValue.fromS("\u001a \u000e")).get())
109+
.enhancedDocument(
110+
((DefaultEnhancedDocument.DefaultBuilder)
111+
DefaultEnhancedDocument.builder()).putObject("stringKey", "\u001a \u000e")
112+
.addAttributeConverterProvider(defaultProvider()).build())
113+
.attributeConverterProvider(defaultProvider())
114+
.json("{\"stringKey\":\"\\u001A \\u000E\"}")
115+
116+
.build());
117+
107118
testDataList.add(dataBuilder().scenario("record")
108119

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

0 commit comments

Comments
 (0)