Skip to content

Commit 2951950

Browse files
Allow missing required properties when parsing CloudEvent (Azure#16046)
1 parent 04ded2a commit 2951950

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/CloudEvent.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ public CloudEvent(string source, string type, BinaryData data, string dataConten
8787

8888
internal CloudEvent(string id, string source, string type, DateTimeOffset? time, string dataSchema, string dataContentType, string subject, JsonElement serializedData, byte[] dataBase64)
8989
{
90-
Argument.AssertNotNull(id, nameof(id));
91-
Argument.AssertNotNull(source, nameof(source));
92-
Argument.AssertNotNull(type, nameof(type));
93-
9490
Id = id;
9591
Source = source;
9692
Type = type;
@@ -174,12 +170,6 @@ public static CloudEvent[] Parse(string requestContent)
174170

175171
foreach (CloudEventInternal cloudEventInternal in cloudEventsInternal)
176172
{
177-
// Case where Data and Type are null - cannot pass null Type into CloudEvent constructor
178-
if (cloudEventInternal.Type == null)
179-
{
180-
cloudEventInternal.Type = "";
181-
}
182-
183173
CloudEvent cloudEvent = new CloudEvent(
184174
cloudEventInternal.Id,
185175
cloudEventInternal.Source,

sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,13 +1534,16 @@ public void CloudEventGetDataThrowsWhenCalledWithoutParse()
15341534
}
15351535

15361536
[Test]
1537-
public void CloudEventParseThrowsIfMissingRequiredProperties()
1537+
public void CloudEventParseDoesNotThrowIfMissingRequiredProperties()
15381538
{
1539-
// missing Id and Source
1540-
string requestContent = "[{ \"subject\": \"\", \"data\": { \"itemSku\": \"512d38b6-c7b8-40c8-89fe-f46f9e9622b6\", \"itemUri\": \"https://rp-eastus2.eventgrid.azure.net:553/eventsubscriptions/estest/validate?id=B2E34264-7D71-453A-B5FB-B62D0FDC85EE&t=2018-04-26T20:30:54.4538837Z&apiVersion=2018-05-01-preview&token=1BNqCxBBSSE9OnNSfZM4%2b5H9zDegKMY6uJ%2fO2DFRkwQ%3d\" }, \"type\": \"Contoso.Items.ItemReceived\"}]";
1541-
1542-
Assert.That(() => CloudEvent.Parse(requestContent),
1543-
Throws.InstanceOf<ArgumentNullException>());
1539+
// missing Id, Source, SpecVersion, and Type
1540+
string requestContent = "[{ \"subject\": \"Subject-0\", \"data\": { \"itemSku\": \"512d38b6-c7b8-40c8-89fe-f46f9e9622b6\", \"itemUri\": \"https://rp-eastus2.eventgrid.azure.net:553/eventsubscriptions/estest/validate?id=B2E34264-7D71-453A-B5FB-B62D0FDC85EE&t=2018-04-26T20:30:54.4538837Z&apiVersion=2018-05-01-preview&token=1BNqCxBBSSE9OnNSfZM4%2b5H9zDegKMY6uJ%2fO2DFRkwQ%3d\" }}]";
1541+
CloudEvent[] events = CloudEvent.Parse(requestContent);
1542+
var cloudEvent = events[0];
1543+
Assert.IsNull(cloudEvent.Id);
1544+
Assert.IsNull(cloudEvent.Source);
1545+
Assert.IsNull(cloudEvent.Type);
1546+
Assert.AreEqual("Subject-0", cloudEvent.Subject);
15441547
}
15451548
#endregion
15461549

@@ -1609,7 +1612,7 @@ public void ConsumeCloudEventWithNoData()
16091612

16101613
Assert.AreEqual(eventData1, null);
16111614
Assert.AreEqual(eventData2, null);
1612-
Assert.AreEqual(events[0].Type, "");
1615+
Assert.IsNull(events[0].Type);
16131616
}
16141617

16151618
[Test]
@@ -1623,7 +1626,7 @@ public void ConsumeCloudEventWithExplicitlyNullData()
16231626

16241627
Assert.AreEqual(eventData1, null);
16251628
Assert.AreEqual(eventData2, null);
1626-
Assert.AreEqual(events[0].Type, "");
1629+
Assert.IsNull(events[0].Type);
16271630
}
16281631
#endregion
16291632

0 commit comments

Comments
 (0)