Skip to content

Handle null when serializing enum to string #6342

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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-17f90b1.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": "Updated codegen to add the null check and pass it in instead of throw NPE when serializing enum to string"
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ private String copyMethodBody(CodeBlock.Builder code, BuilderTransform builderTr
case NONE:
return inputVariableName;
case ENUM_TO_STRING:
code.add("$T $N = $N.toString();", String.class, outputVariableName, inputVariableName);
code.add(
"$T $N = $N == null ? null : $N.toString();",
String.class, outputVariableName, inputVariableName, inputVariableName);
return outputVariableName;
case STRING_TO_ENUM:
code.add("$1T $2N = $1T.fromValue($3N);", enumType, outputVariableName, inputVariableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static List<String> copyEnumToString(Collection<EnumType> listOfEnumsParam) {
} else {
List<String> modifiableList = new ArrayList<>(listOfEnumsParam.size());
listOfEnumsParam.forEach(entry -> {
String result = entry.toString();
String result = entry == null ? null : entry.toString();
modifiableList.add(result);
});
list = Collections.unmodifiableList(modifiableList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static List<Map<String, String>> copyEnumToString(Collection<? extends Map<EnumT
} else {
Map<String, String> modifiableMap = new LinkedHashMap<>(entry.size());
entry.forEach((key, value) -> {
String result = key.toString();
String result = key == null ? null : key.toString();
modifiableMap.put(result, value);
});
map = Collections.unmodifiableMap(modifiableMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static Map<String, String> copyEnumToString(Map<EnumType, EnumType> mapOfEnumToE
} else {
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfEnumToEnumParam.size());
mapOfEnumToEnumParam.forEach((key, value) -> {
String result = key.toString();
String result1 = value.toString();
String result = key == null ? null : key.toString();
String result1 = value == null ? null : value.toString();
modifiableMap.put(result, result1);
});
map = Collections.unmodifiableMap(modifiableMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ static Map<String, List<String>> copyEnumToString(Map<EnumType, ? extends Collec
} else {
Map<String, List<String>> modifiableMap = new LinkedHashMap<>(mapOfEnumToListOfEnumsParam.size());
mapOfEnumToListOfEnumsParam.forEach((key, value) -> {
String result = key.toString();
String result = key == null ? null : key.toString();
List<String> list;
if (value == null || value instanceof SdkAutoConstructList) {
list = DefaultSdkAutoConstructList.getInstance();
} else {
List<String> modifiableList = new ArrayList<>(value.size());
value.forEach(entry -> {
String result1 = entry.toString();
String result1 = entry == null ? null : entry.toString();
modifiableList.add(result1);
});
list = Collections.unmodifiableList(modifiableList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ static Map<String, Map<String, String>> copyEnumToString(
} else {
Map<String, Map<String, String>> modifiableMap = new LinkedHashMap<>(mapOfEnumToMapOfStringToEnumParam.size());
mapOfEnumToMapOfStringToEnumParam.forEach((key, value) -> {
String result = key.toString();
String result = key == null ? null : key.toString();
Map<String, String> map1;
if (value == null || value instanceof SdkAutoConstructMap) {
map1 = DefaultSdkAutoConstructMap.getInstance();
} else {
Map<String, String> modifiableMap1 = new LinkedHashMap<>(value.size());
value.forEach((key1, value1) -> {
String result1 = value1.toString();
String result1 = value1 == null ? null : value1.toString();
modifiableMap1.put(key1, result1);
});
map1 = Collections.unmodifiableMap(modifiableMap1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static Map<String, SimpleStruct> copyEnumToString(Map<EnumType, ? extends Simple
} else {
Map<String, SimpleStruct> modifiableMap = new LinkedHashMap<>(mapOfEnumToSimpleStructParam.size());
mapOfEnumToSimpleStructParam.forEach((key, value) -> {
String result = key.toString();
String result = key == null ? null : key.toString();
modifiableMap.put(result, value);
});
map = Collections.unmodifiableMap(modifiableMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, String> mapOfEnumToStr
} else {
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfEnumToStringParam.size());
mapOfEnumToStringParam.forEach((key, value) -> {
String result = key.toString();
String result = key == null ? null : key.toString();
modifiableMap.put(result, value);
});
map = Collections.unmodifiableMap(modifiableMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static Map<String, String> copyEnumToString(Map<String, EnumType> mapOfStringToE
} else {
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfStringToEnumParam.size());
mapOfStringToEnumParam.forEach((key, value) -> {
String result = value.toString();
String result = value == null ? null : value.toString();
modifiableMap.put(key, result);
});
map = Collections.unmodifiableMap(modifiableMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.services.protocolrestjson.model;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;

public class MemberCopierTest {
private MockSyncHttpClient mockHttpClient;
private ProtocolRestJsonClient client;

@BeforeEach
public void setupClient() {
mockHttpClient = new MockSyncHttpClient();
mockHttpClient.stubNextResponse200();

client = ProtocolRestJsonClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid",
"skid")))
.region(Region.US_EAST_1)
.httpClient(mockHttpClient)
.build();
}

@Test
public void enumListWithNulls_serializesWithoutNPE() {
AllTypesRequest request = AllTypesRequest.builder()
.listOfEnums(Arrays.asList(EnumType.ENUM_VALUE1, null, EnumType.ENUM_VALUE2))
.build();

assertDoesNotThrow(() -> client.allTypes(request));
}

@Test
public void stringListWithNulls_serializesWithoutNPE() {
AllTypesRequest request = AllTypesRequest.builder()
.simpleList(Arrays.asList("Foo", null, "Bar"))
.build();

assertDoesNotThrow(() -> client.allTypes(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -640,5 +640,28 @@
}
}
}
},
{
"description": "List of enums with null values are marshalled correctly without NPE",
"given": {
"input": {
"ListOfEnums": [
"EnumValue1",
null,
"EnumValue2"
]
}
},
"when": {
"action": "marshall",
"operation": "AllTypes"
},
"then": {
"serializedAs": {
"body": {
"jsonEquals": "{\"ListOfEnums\": [\"EnumValue1\", null, \"EnumValue2\"]}"
}
}
}
}
]
Loading