Skip to content

Commit

Permalink
Fix for Issue [#4156](#4156) : Single quotes in toJson conversions fo…
Browse files Browse the repository at this point in the history
…r EnhancedDocuments are no longer being escaped.
  • Loading branch information
joviegas committed Aug 9, 2023
1 parent 7697807 commit 47e7376
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS DynamoDB Enhanced Client",
"contributor": "",
"description": "Fix for Issue [#4156](https://github.com/aws/aws-sdk-java-v2/issues/4156) : Single quotes in toJson conversions for EnhancedDocuments are no longer being escaped."
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public static String addEscapeCharacters(String input) {
case '\"':
output.append("\\\""); // double-quote character
break;
case '\'':
output.append("\\'"); // single-quote character
break;
default:
output.append(ch);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.awssdk.enhanced.dynamodb.document.EnhancedDocumentTestData.testDataInstance;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
Expand Down Expand Up @@ -55,6 +57,8 @@ private static Stream<Arguments> escapeDocumentStrings() {
return Stream.of(
Arguments.of(String.valueOf(c), "{\"key\":\"\\n\"}")
, Arguments.of("", "{\"key\":\"\"}")
, Arguments.of("\"", "{\"key\":\"\\\"\"}")
, Arguments.of("\\", "{\"key\":\"\\\\\"}")
, Arguments.of(" ", "{\"key\":\" \"}")
, Arguments.of("\t", "{\"key\":\"\\t\"}")
, Arguments.of("\n", "{\"key\":\"\\n\"}")
Expand All @@ -63,6 +67,13 @@ private static Stream<Arguments> escapeDocumentStrings() {
);
}

private static Stream<Arguments> unEscapeDocumentStrings() {
return Stream.of(
Arguments.of("'", "{\"key\":\"'\"}"),
Arguments.of("'single quote'", "{\"key\":\"'single quote'\"}")
);
}

@Test
void enhancedDocumentGetters() {

Expand Down Expand Up @@ -337,13 +348,27 @@ void invalidKeyNames(String escapingString) {

@ParameterizedTest
@MethodSource("escapeDocumentStrings")
void escapingTheValues(String escapingString, String expectedJson) {
void escapingTheValues(String escapingString, String expectedJson) throws JsonProcessingException {

EnhancedDocument document = EnhancedDocument.builder()
.attributeConverterProviders(defaultProvider())
.putString("key", escapingString)
.build();
assertThat(document.toJson()).isEqualTo(expectedJson);
assertThat(new ObjectMapper().readTree(document.toJson())).isNotNull();
}

@ParameterizedTest
@MethodSource("unEscapeDocumentStrings")
void unEscapingTheValues(String escapingString, String expectedJson) throws JsonProcessingException {

EnhancedDocument document = EnhancedDocument.builder()
.attributeConverterProviders(defaultProvider())
.putString("key", escapingString)
.build();
assertThat(document.toJson()).isEqualTo(expectedJson);
assertThat(new ObjectMapper().readTree(document.toJson())).isNotNull();

}

@Test
Expand Down

0 comments on commit 47e7376

Please sign in to comment.