Skip to content

Bytes in a toJson of EnhancedDocument should be represented in a decimal encoded format for their binary representation. #3860

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 5 commits into from
Mar 28, 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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-7ab9c30.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "A binary attribute value must be encoded in base64 format for its String representation in EnhancedDocument.toJson() API"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,17 +128,17 @@ public Map<String, AttributeValue> itemToMap(EnhancedDocument item, Collection<S
if (item.toMap() == null) {
return null;
}

List<AttributeConverterProvider> providers = mergeAttributeConverterProviders(item);
return item.toBuilder().attributeConverterProviders(providers).build().toMap().entrySet()
.stream()
.filter(entry -> attributes.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(left, right) -> left, LinkedHashMap::new));
}

@Override
public AttributeValue attributeValue(EnhancedDocument item, String attributeName) {
if (item == null || item.toMap() == null) {
if (item == null) {
return null;
}
List<AttributeConverterProvider> providers = mergeAttributeConverterProviders(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import software.amazon.awssdk.protocols.jsoncore.internal.ObjectJsonNode;
import software.amazon.awssdk.protocols.jsoncore.internal.StringJsonNode;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.utils.BinaryUtils;

/**
* An Internal converter between JsonNode and {@link AttributeValue}.
Expand Down Expand Up @@ -119,7 +120,7 @@ public JsonNode convertBytes(SdkBytes value) {
if (value == null) {
return null;
}
return new StringJsonNode(value.asUtf8String());
return new StringJsonNode(BinaryUtils.toBase64(value.asByteArray()));
}

@Override
Expand Down Expand Up @@ -151,8 +152,8 @@ public JsonNode convertSetOfBytes(List<SdkBytes> value) {
if (value == null) {
return null;
}
return new ArrayJsonNode(value.stream().map(sdkByte ->
new StringJsonNode(sdkByte.asUtf8String())
return new ArrayJsonNode(value.stream().map(
sdkByte -> new StringJsonNode(BinaryUtils.toBase64(sdkByte.asByteArray()))
).collect(Collectors.toList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ public Map<String, AttributeValue> toMap() {

private Map<String, AttributeValue> initializeAttributeValueMap() {
Map<String, AttributeValue> result = new LinkedHashMap<>(this.nonAttributeValueMap.size());
Validate.notEmpty(this.attributeConverterProviders, "attributeConverterProviders");
this.nonAttributeValueMap.forEach((k, v) -> {
if (v == null) {
result.put(k, NULL_ATTRIBUTE_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void jsonAttributeConverterWithSdkBytes() {

assertThat(transformFrom(converter, sdkByteJsonNode).s()).isEqualTo(SdkBytes.fromUtf8String("a").asUtf8String());
assertThat(transformFrom(converter, sdkByteJsonNode).b()).isNull();
assertThat(transformTo(converter, AttributeValue.fromB(SdkBytes.fromUtf8String("a")))).isEqualTo(sdkByteJsonNode);
assertThat(transformTo(converter, AttributeValue.fromB(SdkBytes.fromUtf8String("a")))).isEqualTo(new StringJsonNode("YQ=="));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ void enhancedDocWithNestedListAndMaps() {
.build();

assertThat(simpleDoc.toJson()).isEqualTo("{\"HashKey\":\"abcdefg123\",\"nullKey\":null,\"numberKey\":2.0,"
+ "\"sdkByte\":\"a\",\"booleanKey\":true,\"jsonKey\":{\"1\":[\"a\",\"b\","
+ "\"sdkByte\":\"YQ==\",\"booleanKey\":true,\"jsonKey\":{\"1\":[\"a\",\"b\","
+ "\"c\"],\"2\":1},\"stingSet\":[\"a\",\"b\",\"c\"],\"numberSet\":[1,2,3,4],"
+ "\"sdkByteSet\":[\"a\"]}");
+ "\"sdkByteSet\":[\"YQ==\"]}");


assertThat(simpleDoc.isPresent("HashKey")).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void initializeTestData() {
.putStringSet("stringSet", Stream.of("a", "b", "c").collect(Collectors.toSet()))
.build())
.json("{\"stringKey\":\"stringValue\",\"numberKey\":10,\"boolKey\":true,\"nullKey\":null,"
+ "\"numberSet\":[1,2,3],\"sdkBytesSet\":[\"a\",\"b\",\"c\"],\"stringSet\":[\"a\","
+ "\"numberSet\":[1,2,3],\"sdkBytesSet\":[\"YQ==\",\"Yg==\",\"Yw==\"],\"stringSet\":[\"a\","
+ "\"b\",\"c\"]}")
.attributeConverterProvider(defaultProvider())
.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.functionaltests.document;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider.defaultProvider;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType;
import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.document.EnhancedDocument;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.LocalDynamoDbSyncTestBase;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;

public class ComplexInputItemsTests extends LocalDynamoDbSyncTestBase {
private final String tableName = getConcreteTableName("table-name");
private DynamoDbEnhancedClient enhancedClient;
private DynamoDbClient lowLevelClient;
private DynamoDbTable<EnhancedDocument> docMappedtable;

@Before
public void setUp() {
lowLevelClient = getDynamoDbClient();
enhancedClient = DynamoDbEnhancedClient.builder()
.dynamoDbClient(lowLevelClient)
.build();
docMappedtable = enhancedClient.table(tableName,
TableSchema.documentSchemaBuilder()
.addIndexPartitionKey(TableMetadata.primaryIndexName(),
"id",
AttributeValueType.S)
.addIndexSortKey(TableMetadata.primaryIndexName(), "sort",
AttributeValueType.N)
.attributeConverterProviders(defaultProvider())
.build());
docMappedtable.createTable();
}

@After
public void deleteTable() {
getDynamoDbClient().deleteTable(DeleteTableRequest.builder()
.tableName(tableName)
.build());
}

@Test
public void getAndPutDocumentWithNoAttributeConverters() {

docMappedtable.putItem(EnhancedDocument.builder()
.putString("id", "one")
.putNumber("sort", 1)
.putString("element", "noAttributeConverter")
.build());

EnhancedDocument noConverterInGetItem = docMappedtable.getItem(EnhancedDocument.builder()
.putString("id", "one")
.putNumber("sort", 1)
.build());
assertThat(noConverterInGetItem.toJson(), is("{\"id\":\"one\",\"sort\":1,\"element\":\"noAttributeConverter\"}"));

}


@Test
public void bytesInAlTypes() {

Map<String, SdkBytes> bytesMap = new LinkedHashMap<>();
bytesMap.put("key1", SdkBytes.fromByteArray("1".getBytes(StandardCharsets.UTF_8)));
bytesMap.put("key2", SdkBytes.fromByteArray("2".getBytes(StandardCharsets.UTF_8)));
EnhancedDocument enhancedDocument = EnhancedDocument.builder()
.putString("id", "allTypesBytes")
.putNumber("sort", 2)
.put("put", SdkBytes.fromUtf8String("1"), SdkBytes.class)
.putBytes("putBytes",
SdkBytes.fromByteBuffer(ByteBuffer.wrap("1".getBytes(StandardCharsets.UTF_8))))
.putBytesSet("putBytesSet",
Stream.of(SdkBytes.fromUtf8String("1"),
SdkBytes.fromUtf8String("2")).collect(Collectors.toCollection(LinkedHashSet::new)))
.putList("putBytesList",
Stream.of(SdkBytes.fromUtf8String("1"),
SdkBytes.fromUtf8String("2")).collect(Collectors.toList()),
EnhancedType.of(SdkBytes.class))
.putMap("bytesMap", bytesMap, EnhancedType.of(String.class),
EnhancedType.of(SdkBytes.class))
.build();

docMappedtable.putItem(enhancedDocument);


EnhancedDocument retrievedItem = docMappedtable.getItem(EnhancedDocument.builder()
.putString("id", "allTypesBytes")
.putNumber("sort", 2)
.build());

assertThat(retrievedItem.toJson(), is("{\"putBytesSet\":[\"MQ==\",\"Mg==\"],\"putBytes\":\"MQ==\",\"putBytesList\":[\"MQ==\",\"Mg==\"],\"id\":\"allTypesBytes\",\"sort\":2,\"bytesMap\":{\"key1\":\"MQ==\",\"key2\":\"Mg==\"},\"put\":\"MQ==\"}"
));
}
}