From 9177ad1ca8805a9c1d2926febf918203e564f9df Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Fri, 3 Dec 2021 12:12:46 -0600 Subject: [PATCH] Parse enqueued mail message model to object (#1742) * Parse enqueued mail message model to object The model of an MailQueueMessage is of type object to enable enqueueing of any message. However, this means the we are not able to parse a serialized json object back into the original object. Provide the model type to enable this deserialization * Use ExpandoObject for deserialized queue message model --- src/Core/Models/Mail/IMailQueueMessage.cs | 2 +- src/Core/Models/Mail/MailQueueMessage.cs | 7 +++++-- .../Utilities/ExpandoObjectJsonConverter.cs | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/Core/Utilities/ExpandoObjectJsonConverter.cs diff --git a/src/Core/Models/Mail/IMailQueueMessage.cs b/src/Core/Models/Mail/IMailQueueMessage.cs index c9d70bc947..89899c0e8a 100644 --- a/src/Core/Models/Mail/IMailQueueMessage.cs +++ b/src/Core/Models/Mail/IMailQueueMessage.cs @@ -9,6 +9,6 @@ namespace Bit.Core.Models.Mail IEnumerable BccEmails { get; set; } string Category { get; set; } string TemplateName { get; set; } - object Model { get; set; } + dynamic Model { get; set; } } } diff --git a/src/Core/Models/Mail/MailQueueMessage.cs b/src/Core/Models/Mail/MailQueueMessage.cs index 2606853eb8..2fdc56750d 100644 --- a/src/Core/Models/Mail/MailQueueMessage.cs +++ b/src/Core/Models/Mail/MailQueueMessage.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using Newtonsoft.Json; +using Bit.Core.Utilities; namespace Bit.Core.Models.Mail { @@ -9,11 +11,12 @@ namespace Bit.Core.Models.Mail public IEnumerable BccEmails { get; set; } public string Category { get; set; } public string TemplateName { get; set; } - public object Model { get; set; } + [JsonConverter(typeof(ExpandoObjectJsonConverter))] + public dynamic Model { get; set; } public MailQueueMessage() { } - public MailQueueMessage(MailMessage message, string templateName, object model) + public MailQueueMessage(MailMessage message, string templateName, dynamic model) { Subject = message.Subject; ToEmails = message.ToEmails; diff --git a/src/Core/Utilities/ExpandoObjectJsonConverter.cs b/src/Core/Utilities/ExpandoObjectJsonConverter.cs new file mode 100644 index 0000000000..78a1ce87bc --- /dev/null +++ b/src/Core/Utilities/ExpandoObjectJsonConverter.cs @@ -0,0 +1,19 @@ +using System; +using System.Dynamic; +using Newtonsoft.Json; + +namespace Bit.Core.Utilities +{ + public class ExpandoObjectJsonConverter : JsonConverter + { + public override bool CanWrite => false; + public override bool CanConvert(Type objectType) => true; + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return serializer.Deserialize(reader); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); + } +}