Skip to content

Fix for Issue #4156 : Single quotes in toJson conversions for EnhancedDocuments are no longer being escaped. #4277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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