Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tests/WhatsAppModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/WhatsApp/AdditionalPropertiesDictionaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions src/WhatsApp/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>For debugging purposes, exposes the original JSON used to deserialize this instance, if any.</summary>
internal string? Json => AdditionalProperties?.GetValueOrDefault("__json") as string;

/// <inheritdoc/>
[JsonConverter(typeof(AdditionalPropertiesDictionaryConverter))]
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
Expand Down Expand Up @@ -59,10 +62,14 @@ public abstract partial record Message(string Id, Service Service, User User, lo
{
var message = JsonSerializer.Deserialize<Message>(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;
Expand Down