Skip to content

Commit 5043265

Browse files
authored
Fixed a bug where null values are not handled properly when S3EventNotification.fromJson is invoked (#5770)
1 parent 0bce6ee commit 5043265

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "S3 Event Notifications",
4+
"contributor": "",
5+
"description": "Fixed a bug where null values are not handled properly when `S3EventNotification#fromJson` is invoked, throwing `IllegalArgumentException`. See [#5730](https://github.com/aws/aws-sdk-java-v2/issues/5730)"
6+
}

services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/internal/DefaultS3EventNotificationReader.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private S3EventNotification readEvent(JsonNode jsonNode) {
7070
}
7171

7272
private List<S3EventNotificationRecord> readRecords(JsonNode node) {
73-
if (node == null || node.isNull()) {
73+
if (isNull(node)) {
7474
return null;
7575
}
7676
List<JsonNode> recordArray = expectArrayOrNull(node, "Records");
@@ -274,23 +274,27 @@ private String expectStringOrNull(Map<String, JsonNode> node, String name) {
274274
}
275275

276276
private String expectStringOrNull(JsonNode node, String name) {
277-
if (node == null) {
277+
if (isNull(node)) {
278278
return null;
279279
}
280280
Validate.isTrue(node.isString(), "'%s' was not a string", name);
281281
return node.asString();
282282
}
283283

284+
private static boolean isNull(JsonNode node) {
285+
return node == null || node.isNull();
286+
}
287+
284288
private List<JsonNode> expectArrayOrNull(JsonNode node, String name) {
285-
if (node == null) {
289+
if (isNull(node)) {
286290
return null;
287291
}
288292
Validate.isTrue(node.isArray(), "expected '%s' to be an array, but was not.", name);
289293
return node.asArray();
290294
}
291295

292296
private Map<String, JsonNode> expectObjectOrNull(JsonNode node, String name) {
293-
if (node == null) {
297+
if (isNull(node)) {
294298
return null;
295299
}
296300
return expectObject(node, name);
@@ -302,7 +306,7 @@ private Map<String, JsonNode> expectObject(JsonNode node, String name) {
302306
}
303307

304308
private Long expectLong(JsonNode node, String name) {
305-
if (node == null) {
309+
if (isNull(node)) {
306310
return null;
307311
}
308312
Validate.isTrue(node.isNumber(), "expected '%s' to be numeric, but was not", name);

services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,75 @@
4040

4141
class S3EventNotificationReaderTest {
4242

43+
@Test
44+
void fromJson_containsNullValues_shouldSucceed() {
45+
S3EventNotification eventNotification = new S3EventNotification(
46+
Collections.singletonList(new S3EventNotificationRecord(
47+
"us-west-2",
48+
"ObjectCreated:Put",
49+
"aws:s3",
50+
"1970-01-01T00:00:00.000Z",
51+
"2.1",
52+
null,
53+
new ResponseElements(
54+
null, null),
55+
new S3(
56+
"testConfigRule",
57+
new S3Bucket(
58+
"mybucket",
59+
new UserIdentity("A3NL1KOZZKExample"),
60+
"arn:aws:s3:::mybucket"),
61+
new S3Object(
62+
"HappyFace.jpg",
63+
null,
64+
"d41d8cd98f00b204e9800998ecf8427e",
65+
null,
66+
"0055AED6DCD90281E5"),
67+
"1.0"
68+
),
69+
new UserIdentity("AIDAJDPLRKLG7UEXAMPLE"),
70+
null, null, null, null)
71+
));
72+
String json = eventNotification.toJsonPretty();
73+
assertThat(json).isEqualTo("{\n"
74+
+ " \"Records\" : [ {\n"
75+
+ " \"eventVersion\" : \"2.1\",\n"
76+
+ " \"eventSource\" : \"aws:s3\",\n"
77+
+ " \"awsRegion\" : \"us-west-2\",\n"
78+
+ " \"eventTime\" : \"1970-01-01T00:00:00Z\",\n"
79+
+ " \"eventName\" : \"ObjectCreated:Put\",\n"
80+
+ " \"userIdentity\" : {\n"
81+
+ " \"principalId\" : \"AIDAJDPLRKLG7UEXAMPLE\"\n"
82+
+ " },\n"
83+
+ " \"requestParameters\" : null,\n"
84+
+ " \"responseElements\" : {\n"
85+
+ " \"x-amz-request-id\" : null,\n"
86+
+ " \"x-amz-id-2\" : null\n"
87+
+ " },\n"
88+
+ " \"s3\" : {\n"
89+
+ " \"s3SchemaVersion\" : \"1.0\",\n"
90+
+ " \"configurationId\" : \"testConfigRule\",\n"
91+
+ " \"bucket\" : {\n"
92+
+ " \"name\" : \"mybucket\",\n"
93+
+ " \"ownerIdentity\" : {\n"
94+
+ " \"principalId\" : \"A3NL1KOZZKExample\"\n"
95+
+ " },\n"
96+
+ " \"arn\" : \"arn:aws:s3:::mybucket\"\n"
97+
+ " },\n"
98+
+ " \"object\" : {\n"
99+
+ " \"key\" : \"HappyFace.jpg\",\n"
100+
+ " \"size\" : null,\n"
101+
+ " \"eTag\" : \"d41d8cd98f00b204e9800998ecf8427e\",\n"
102+
+ " \"versionId\" : null,\n"
103+
+ " \"sequencer\" : \"0055AED6DCD90281E5\"\n"
104+
+ " }\n"
105+
+ " }\n"
106+
+ " } ]\n"
107+
+ "}");
108+
S3EventNotification actual = S3EventNotification.fromJson(json);
109+
assertThat(actual).isEqualTo(eventNotification);
110+
}
111+
43112
@Test
44113
void givenEventWithoutOptionalFields_whenReadingJson_expectOnlyRequiredFields() {
45114
String eventJson = "{ "

0 commit comments

Comments
 (0)