Skip to content

Commit

Permalink
Addressed Error for Events Missing Required Fields (#15076)
Browse files Browse the repository at this point in the history
* Handled deserialization of events with missing fields

Should throw an error if required fields for EventGridEvent are missing when Parse is called

* Added tests for this error

* Added a similar constructor in CloudEvent that checks if Id (a required field) is null
  • Loading branch information
kerri-lee authored Sep 11, 2020
1 parent ad80cd5 commit 0e6d31d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public static EventGridEvent ToEventGridEvent(this BinaryData binaryData)
JsonDocument requestDocument = JsonDocument.Parse(binaryData.ToBytes());
EventGridEventInternal egEventInternal = EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement);

EventGridEvent egEvent = new EventGridEvent()
{
Subject = egEventInternal.Subject,
EventType = egEventInternal.EventType,
DataVersion = egEventInternal.DataVersion,
Id = egEventInternal.Id,
EventTime = egEventInternal.EventTime,
SerializedData = egEventInternal.Data
};
EventGridEvent egEvent = new EventGridEvent(
egEventInternal.Data,
egEventInternal.Subject,
egEventInternal.EventType,
egEventInternal.DataVersion,
egEventInternal.EventTime,
egEventInternal.Id);

return egEvent;
}
Expand All @@ -56,17 +54,15 @@ public static CloudEvent ToCloudEvent(this BinaryData binaryData)
}

CloudEvent cloudEvent = new CloudEvent(
cloudEventInternal.Source,
cloudEventInternal.Type)
{
Id = cloudEventInternal.Id,
Time = cloudEventInternal.Time,
DataBase64 = cloudEventInternal.DataBase64,
DataSchema = cloudEventInternal.Dataschema,
DataContentType = cloudEventInternal.Datacontenttype,
Subject = cloudEventInternal.Subject,
SerializedData = cloudEventInternal.Data
};
cloudEventInternal.Id,
cloudEventInternal.Source,
cloudEventInternal.Type,
cloudEventInternal.Time,
cloudEventInternal.Dataschema,
cloudEventInternal.Datacontenttype,
cloudEventInternal.Subject,
cloudEventInternal.Data,
cloudEventInternal.DataBase64);

if (cloudEventInternal.AdditionalProperties != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ public CloudEvent(string source, string type, BinaryData data, string dataConten
ExtensionAttributes = new Dictionary<string, object>();
}

internal CloudEvent(string id, string source, string type, DateTimeOffset? time, string dataSchema, string dataContentType, string subject, JsonElement? serializedData, byte[] dataBase64)
{
Argument.AssertNotNull(id, nameof(id));
Argument.AssertNotNull(source, nameof(source));
Argument.AssertNotNull(type, nameof(type));

Id = id;
Source = source;
Type = type;
Time = time;
DataSchema = dataSchema;
DataContentType = dataContentType;
Subject = subject;
SerializedData = serializedData;
DataBase64 = dataBase64;
ExtensionAttributes = new Dictionary<string, object>();
}

/// <summary> An identifier for the event. The combination of id and source must be unique for each distinct event. </summary>
public string Id { get; set; } = Guid.NewGuid().ToString();

Expand Down Expand Up @@ -163,17 +181,15 @@ public static CloudEvent[] Parse(string requestContent)
}

CloudEvent cloudEvent = new CloudEvent(
cloudEventInternal.Id,
cloudEventInternal.Source,
cloudEventInternal.Type)
{
Id = cloudEventInternal.Id,
Time = cloudEventInternal.Time,
DataBase64 = cloudEventInternal.DataBase64,
DataSchema = cloudEventInternal.Dataschema,
DataContentType = cloudEventInternal.Datacontenttype,
Subject = cloudEventInternal.Subject,
SerializedData = cloudEventInternal.Data
};
cloudEventInternal.Type,
cloudEventInternal.Time,
cloudEventInternal.Dataschema,
cloudEventInternal.Datacontenttype,
cloudEventInternal.Subject,
cloudEventInternal.Data,
cloudEventInternal.DataBase64);

if (cloudEventInternal.AdditionalProperties != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ public EventGridEvent(object data, string subject, string eventType, string data
DataVersion = dataVersion;
}

internal EventGridEvent()
internal EventGridEvent(JsonElement serializedData, string subject, string eventType, string dataVersion, DateTimeOffset eventTime, string id)
{
Argument.AssertNotNull(subject, nameof(subject));
Argument.AssertNotNull(eventType, nameof(eventType));
Argument.AssertNotNull(dataVersion, nameof(dataVersion));
Argument.AssertNotNull(id, nameof(id));

Subject = subject;
SerializedData = serializedData;
EventType = eventType;
DataVersion = dataVersion;
EventTime = eventTime;
Id = id;
}

/// <summary> An unique identifier for the event. </summary>
Expand Down Expand Up @@ -100,15 +111,13 @@ public static EventGridEvent[] Parse(string requestContent)

foreach (EventGridEventInternal egEventInternal in egEventsInternal)
{
EventGridEvent egEvent = new EventGridEvent()
{
Subject = egEventInternal.Subject,
EventType = egEventInternal.EventType,
DataVersion = egEventInternal.DataVersion,
Id = egEventInternal.Id,
EventTime = egEventInternal.EventTime,
SerializedData = egEventInternal.Data
};
EventGridEvent egEvent = new EventGridEvent(
egEventInternal.Data,
egEventInternal.Subject,
egEventInternal.EventType,
egEventInternal.DataVersion,
egEventInternal.EventTime,
egEventInternal.Id);

egEvents.Add(egEvent);
}
Expand Down
20 changes: 20 additions & 0 deletions sdk/eventgrid/Azure.Messaging.EventGrid/tests/ConsumeEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public void EGEventGetDataThrowsWhenCalledWithoutParse()
Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku);
}
}

[Test]
public void EGEventParseThrowsIfMissingRequiredProperties()
{
// missing Id and DataVersion
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\" }, \"eventType\": \"Contoso.Items.ItemReceived\", \"eventTime\": \"2018-01-25T22:12:19.4556811Z\", \"metadataVersion\": \"1\"}]";

Assert.That(() => EventGridEvent.Parse(requestContent),
Throws.InstanceOf<ArgumentNullException>());
}
#endregion

#region Custom event tests
Expand Down Expand Up @@ -1479,6 +1489,16 @@ public void CloudEventGetDataThrowsWhenCalledWithoutParse()
Assert.AreEqual("512d38b6-c7b8-40c8-89fe-f46f9e9622b6", eventData1.ItemSku);
}
}

[Test]
public void CloudEventParseThrowsIfMissingRequiredProperties()
{
// missing Id and Source
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\"}]";

Assert.That(() => CloudEvent.Parse(requestContent),
Throws.InstanceOf<ArgumentNullException>());
}
#endregion

#region Custom event tests
Expand Down

0 comments on commit 0e6d31d

Please sign in to comment.