From a833f5dd5237113fa7a8038dfd21c80681484d8c Mon Sep 17 00:00:00 2001 From: Clemens Vasters Date: Mon, 20 May 2019 14:37:13 +0200 Subject: [PATCH] Null value check added in EncodeStructuredEvent Signed-off-by: Clemens Vasters --- src/CloudNative.CloudEvents/JsonEventFormatter.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/CloudNative.CloudEvents/JsonEventFormatter.cs b/src/CloudNative.CloudEvents/JsonEventFormatter.cs index d512961..77e9fb2 100644 --- a/src/CloudNative.CloudEvents/JsonEventFormatter.cs +++ b/src/CloudNative.CloudEvents/JsonEventFormatter.cs @@ -112,7 +112,12 @@ namespace CloudNative.CloudEvents var attributes = cloudEvent.GetAttributes(); foreach (var keyValuePair in attributes) { - if (keyValuePair.Value is ContentType) + if (keyValuePair.Value == null) + { + continue; + } + + if (keyValuePair.Value is ContentType && !string.IsNullOrEmpty(((ContentType)keyValuePair.Value).MediaType)) { jObject[keyValuePair.Key] = JToken.FromObject(((ContentType)keyValuePair.Value).ToString()); } @@ -121,7 +126,6 @@ namespace CloudNative.CloudEvents jObject[keyValuePair.Key] = JToken.FromObject(keyValuePair.Value); } } - return Encoding.UTF8.GetBytes(jObject.ToString()); }