Skip to content

Commit 18a5955

Browse files
committed
add null check when converting enum to string
1 parent 8966845 commit 18a5955

File tree

10 files changed

+68
-12
lines changed

10 files changed

+68
-12
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/model/MemberCopierSpec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ private String copyMethodBody(CodeBlock.Builder code, BuilderTransform builderTr
224224
case NONE:
225225
return inputVariableName;
226226
case ENUM_TO_STRING:
227-
code.add("$T $N = $N.toString();", String.class, outputVariableName, inputVariableName);
227+
code.add(
228+
"$T $N = $N == null ? null : $N.toString();",
229+
String.class, outputVariableName, inputVariableName, inputVariableName);
228230
return outputVariableName;
229231
case STRING_TO_ENUM:
230232
code.add("$1T $2N = $1T.fromValue($3N);", enumType, outputVariableName, inputVariableName);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/listofenumscopier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static List<String> copyEnumToString(Collection<EnumType> listOfEnumsParam) {
3333
} else {
3434
List<String> modifiableList = new ArrayList<>(listOfEnumsParam.size());
3535
listOfEnumsParam.forEach(entry -> {
36-
String result = entry.toString();
36+
String result = entry == null ? null : entry.toString();
3737
modifiableList.add(result);
3838
});
3939
list = Collections.unmodifiableList(modifiableList);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/listofmapofenumtostringcopier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static List<Map<String, String>> copyEnumToString(Collection<? extends Map<EnumT
5353
} else {
5454
Map<String, String> modifiableMap = new LinkedHashMap<>(entry.size());
5555
entry.forEach((key, value) -> {
56-
String result = key.toString();
56+
String result = key == null ? null : key.toString();
5757
modifiableMap.put(result, value);
5858
});
5959
map = Collections.unmodifiableMap(modifiableMap);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofenumtoenumcopier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ static Map<String, String> copyEnumToString(Map<EnumType, EnumType> mapOfEnumToE
3232
} else {
3333
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfEnumToEnumParam.size());
3434
mapOfEnumToEnumParam.forEach((key, value) -> {
35-
String result = key.toString();
36-
String result1 = value.toString();
35+
String result = key == null ? null : key.toString();
36+
String result1 = value == null ? null : value.toString();
3737
modifiableMap.put(result, result1);
3838
});
3939
map = Collections.unmodifiableMap(modifiableMap);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofenumtolistofenumscopier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ static Map<String, List<String>> copyEnumToString(Map<EnumType, ? extends Collec
4747
} else {
4848
Map<String, List<String>> modifiableMap = new LinkedHashMap<>(mapOfEnumToListOfEnumsParam.size());
4949
mapOfEnumToListOfEnumsParam.forEach((key, value) -> {
50-
String result = key.toString();
50+
String result = key == null ? null : key.toString();
5151
List<String> list;
5252
if (value == null || value instanceof SdkAutoConstructList) {
5353
list = DefaultSdkAutoConstructList.getInstance();
5454
} else {
5555
List<String> modifiableList = new ArrayList<>(value.size());
5656
value.forEach(entry -> {
57-
String result1 = entry.toString();
57+
String result1 = entry == null ? null : entry.toString();
5858
modifiableList.add(result1);
5959
});
6060
list = Collections.unmodifiableList(modifiableList);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofenumtomapofstringtoenumcopier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ static Map<String, Map<String, String>> copyEnumToString(
4343
} else {
4444
Map<String, Map<String, String>> modifiableMap = new LinkedHashMap<>(mapOfEnumToMapOfStringToEnumParam.size());
4545
mapOfEnumToMapOfStringToEnumParam.forEach((key, value) -> {
46-
String result = key.toString();
46+
String result = key == null ? null : key.toString();
4747
Map<String, String> map1;
4848
if (value == null || value instanceof SdkAutoConstructMap) {
4949
map1 = DefaultSdkAutoConstructMap.getInstance();
5050
} else {
5151
Map<String, String> modifiableMap1 = new LinkedHashMap<>(value.size());
5252
value.forEach((key1, value1) -> {
53-
String result1 = value1.toString();
53+
String result1 = value1 == null ? null : value1.toString();
5454
modifiableMap1.put(key1, result1);
5555
});
5656
map1 = Collections.unmodifiableMap(modifiableMap1);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofenumtosimplestructcopier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static Map<String, SimpleStruct> copyEnumToString(Map<EnumType, ? extends Simple
6262
} else {
6363
Map<String, SimpleStruct> modifiableMap = new LinkedHashMap<>(mapOfEnumToSimpleStructParam.size());
6464
mapOfEnumToSimpleStructParam.forEach((key, value) -> {
65-
String result = key.toString();
65+
String result = key == null ? null : key.toString();
6666
modifiableMap.put(result, value);
6767
});
6868
map = Collections.unmodifiableMap(modifiableMap);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofenumtostringcopier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static Map<String, String> copyEnumToString(Map<EnumType, String> mapOfEnumToStr
3232
} else {
3333
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfEnumToStringParam.size());
3434
mapOfEnumToStringParam.forEach((key, value) -> {
35-
String result = key.toString();
35+
String result = key == null ? null : key.toString();
3636
modifiableMap.put(result, value);
3737
});
3838
map = Collections.unmodifiableMap(modifiableMap);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/model/mapofstringtoenumcopier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static Map<String, String> copyEnumToString(Map<String, EnumType> mapOfStringToE
3232
} else {
3333
Map<String, String> modifiableMap = new LinkedHashMap<>(mapOfStringToEnumParam.size());
3434
mapOfStringToEnumParam.forEach((key, value) -> {
35-
String result = value.toString();
35+
String result = value == null ? null : value.toString();
3636
modifiableMap.put(key, result);
3737
});
3838
map = Collections.unmodifiableMap(modifiableMap);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.protocolrestjson.model;
17+
18+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19+
20+
import java.util.Arrays;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
24+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
25+
import software.amazon.awssdk.regions.Region;
26+
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
27+
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;
28+
29+
public class MemberCopierTest {
30+
private MockSyncHttpClient mockHttpClient;
31+
private ProtocolRestJsonClient client;
32+
33+
@BeforeEach
34+
public void setupClient() {
35+
mockHttpClient = new MockSyncHttpClient();
36+
mockHttpClient.stubNextResponse200();
37+
38+
client = ProtocolRestJsonClient.builder()
39+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid",
40+
"skid")))
41+
.region(Region.US_EAST_1)
42+
.httpClient(mockHttpClient)
43+
.build();
44+
}
45+
46+
@Test
47+
public void enumListWithNulls_serializesWithoutNPE() {
48+
AllTypesRequest request = AllTypesRequest.builder()
49+
.listOfEnums(Arrays.asList(EnumType.ENUM_VALUE1, null, EnumType.ENUM_VALUE2))
50+
.build();
51+
52+
assertDoesNotThrow(() -> client.allTypes(request));
53+
}
54+
}

0 commit comments

Comments
 (0)