diff --git a/src/Tests/WhatsAppModelTests.cs b/src/Tests/WhatsAppModelTests.cs
index ca6009d..5f42736 100644
--- a/src/Tests/WhatsAppModelTests.cs
+++ b/src/Tests/WhatsAppModelTests.cs
@@ -29,7 +29,7 @@ public async Task DeserializeMessage(string type, string notification, string id
// If id was empty, it should be automatically fixed and generated
if (id == string.Empty)
{
- Assert.False(string.IsNullOrEmpty(message.Id));
+ Assert.False(string.IsNullOrEmpty(message.Id)); ;
}
else
{
diff --git a/src/WhatsApp/AdditionalPropertiesDictionaryConverter.cs b/src/WhatsApp/AdditionalPropertiesDictionaryConverter.cs
index 899916a..662377e 100644
--- a/src/WhatsApp/AdditionalPropertiesDictionaryConverter.cs
+++ b/src/WhatsApp/AdditionalPropertiesDictionaryConverter.cs
@@ -38,7 +38,7 @@ public override void Write(Utf8JsonWriter writer, AdditionalPropertiesDictionary
{
writer.WriteStartObject();
- foreach (var kvp in value.Where(x => x.Value is not null))
+ foreach (var kvp in value.Where(x => x.Value is not null && !x.Key.StartsWith("__", StringComparison.Ordinal)))
{
writer.WritePropertyName(kvp.Key);
JsonSerializer.Serialize(writer, kvp.Value, options);
diff --git a/src/WhatsApp/Message.cs b/src/WhatsApp/Message.cs
index 574fefa..3cb39d4 100644
--- a/src/WhatsApp/Message.cs
+++ b/src/WhatsApp/Message.cs
@@ -21,6 +21,9 @@ namespace Devlooped.WhatsApp;
[JsonDerivedType(typeof(UnsupportedMessage), "unsupported")]
public abstract partial record Message(string Id, Service Service, User User, long Timestamp) : IMessage
{
+ /// For debugging purposes, exposes the original JSON used to deserialize this instance, if any.
+ internal string? Json => AdditionalProperties?.GetValueOrDefault("__json") as string;
+
///
[JsonConverter(typeof(AdditionalPropertiesDictionaryConverter))]
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
@@ -59,10 +62,14 @@ public abstract partial record Message(string Id, Service Service, User User, lo
{
var message = JsonSerializer.Deserialize(jq, JsonContext.DefaultOptions);
- // Fix empty id for system messages
- if (message is not null && string.IsNullOrEmpty(message.Id))
+ if (message is not null)
{
- message = message with { Id = Ulid.NewUlid().ToString() };
+ message.AdditionalProperties ??= [];
+ message.AdditionalProperties["__json"] = json;
+
+ // Fix empty id for system messages that don't have a message id otherwise
+ if (string.IsNullOrEmpty(message.Id))
+ message = message with { Id = Ulid.NewUlid().ToString() };
}
return message;