Adventures with Event Grid: Where Did My Null Properties Go?

The other day I noticed when assigning an object to the Data property of an Event Grid Schema message ✉️, any properties, on that object, that had a null value were going to be ignored.

I thought my eyes 👀 were failing me! To test this, I wrote the following in LINQPad.

public const string EVENTGRID_TOPICKEY = "YOUR_EVENTGRID_TOPICKEY";
public const string EVENTGRID_TOPICENDPOINT = "YOUR_EVENTGRID_ENDPOINT";

async Task Main()
{
	var topicCredentials =
		new TopicCredentials(EVENTGRID_TOPICKEY);

	var eventGridClient =
		new EventGridClient(
			topicCredentials);

	var eventGridEventList =
		new List<EventGridEvent>();

	var eventGridEvent =
		new EventGridEvent()
		{
			Id = Guid.NewGuid().ToString(),
			Subject = "LINQPad.Test subject",
			EventType = "LINQPad.Test eventtype",
			Data = new Person { 
				Id = Guid.NewGuid(),
				Name = $"name-{Guid.NewGuid()}",
				NickName = null
			},
			EventTime = DateTime.UtcNow,
			DataVersion = "1"
		};

	eventGridEventList.Add(
		eventGridEvent);

	await eventGridClient.PublishEventsAsync(
		new Uri(EVENTGRID_TOPICENDPOINT).Host,
		eventGridEventList);

	eventGridEvent.Dump();
}

public class Person {
	[JsonProperty("id")]
	public Guid Id { get; set; }
	[JsonProperty("name")]
	public string Name { get; set; }
	[JsonProperty("nick_name")]
	public string NickName {get;set;}
}

For my Person object I am setting NickName to null.

I created a simple Logic App, just composed of an HTTP Trigger, and made it a subscriber to my Event Grid topic.

I fired 🔥 off an Event Grid message ✉️ and inspected the Logic App to see what it received.

Needless to say, I was surprised 😲 to see that my nick_name property had been dropped.

Just to be sure it wasn’t localized to the Logic App, I created another subscriber to the Event Grid topic that wrote the message ✉️ to a Storage Queue.

I fired 🔥 off another Event Grid message ✉️ and inspected my Storage Queue to see what it received.

{
	"id": "bd2085b4-be74-4ed1-95eb-22d9c5527a83",
	"subject": "LINQPad.Test subject",
	"data": {
		"id": "0a44c2bc-3ec9-484f-9345-85a25e3894ee",
		"name": "name-e3b90ff5-4e99-4666-aa30-7df5a6094b13"
	},
	"eventType": "LINQPad.Test eventtype",
	"eventTime": "2019-07-26T10:44:06.9076343Z",
	"dataVersion": "1",
	"metadataVersion": "1",
	"topic": "/subscriptions/SUBSCRIPTIONID/resourceGroups/RESOURCEGROUPNAME/providers/Microsoft.EventGrid/topics/EVENTGRIDTOPIC"
}

Again, my nick_name property had been dropped!

Hmmm…

Now what’s interesting, is if I do the same thing, but send the message ✉️ to Azure Queue Storage, in include the null property, probably because I am using Newtonsoft.Json to serialize the Event Grid message ✉️.

public const string CLOUDSTORAGE_ENDPOINT = "YOUR_CLOUDSTORAGE_ENDPOINT ";

async Task Main()
{
	CloudStorageAccount storageAccount;

	if (CloudStorageAccount.TryParse(CLOUDSTORAGE_ENDPOINT, out storageAccount))
	{
		var cloudQueueClient = storageAccount.CreateCloudQueueClient();
		var cloudQueueReference = cloudQueueClient.GetQueueReference("events");

		var eventGridEvent =
			new EventGridEvent()
			{
				Id = Guid.NewGuid().ToString(),
				Subject = "LINQPad.Test subject",
				EventType = "LINQPad.Test eventtype",
				Data = new Person
				{
					Id = Guid.NewGuid(),
					Name = $"name-{Guid.NewGuid()}",
					NickName = null
				},
				EventTime = DateTime.UtcNow,
				DataVersion = "1"
			};

		var cloudQueueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(eventGridEvent, Newtonsoft.Json.Formatting.Indented));

		await cloudQueueReference.AddMessageAsync(cloudQueueMessage);

		eventGridEvent.Dump();
	}
	else
	{
		Console.WriteLine("A connection string has not been defined for CLOUDSTORAGE_ENDPOINT.");
	}
}

So maybe this is Event Grid specific? Not 100% 💯sure.

When supplying the sample JSON schema for my Logic App’s Parse JSON step, I had to make sure to include ALL my properties. Normally, I just pick off the JSON generated by the incoming Event Grid message ✉️ and just use that, but as I mentioned, it was dropping my null properties.

Once I did that, all was well.

In closing, not saying this is a problem, just something that was unexpected.

Were I to use an Azure Function to read the Event Grid message ✉️, and then inside the Azure Function, convert the Data property to a dynamic object, I would be missing any object properties set to null.

Just something to be aware of, hopefully save a fellow developer a head scratch or two.

Leave a Reply

Your email address will not be published.