Skip to content

Commit

Permalink
Fix deserialization of missing nullable fields (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkla-dr authored Apr 22, 2024
1 parent 9f658cc commit 823d80e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/Hangfire.Mongo/Dto/DistributedLockDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public DistributedLockDto()
public DistributedLockDto(BsonDocument doc)
{
Id = doc["_id"].AsObjectId;
Resource = doc[nameof(Resource)].StringOrNull();
if (doc.TryGetValue(nameof(Resource), out var resource))
{
Resource = resource.StringOrNull();
}
ExpireAt = doc[nameof(ExpireAt)].ToUniversalTime();
}
/// <summary>
Expand Down
25 changes: 20 additions & 5 deletions src/Hangfire.Mongo/Dto/JobDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,26 @@ public JobDto(BsonDocument doc) : base(doc)
return;
}

Queue = doc[nameof(Queue)].StringOrNull();
FetchedAt = doc[nameof(FetchedAt)].ToNullableLocalTime();
StateName = doc[nameof(StateName)].StringOrNull();
InvocationData = doc[nameof(InvocationData)].StringOrNull();
Arguments = doc[nameof(Arguments)].StringOrNull();
if (doc.TryGetValue(nameof(Queue), out var queue))
{
Queue = queue.StringOrNull();
}
if (doc.TryGetValue(nameof(FetchedAt), out var fetchedAt))
{
FetchedAt = fetchedAt.ToNullableLocalTime();
}
if (doc.TryGetValue(nameof(StateName), out var stateName))
{
StateName = stateName.StringOrNull();
}
if (doc.TryGetValue(nameof(InvocationData), out var invocationData))
{
InvocationData = invocationData.StringOrNull();
}
if (doc.TryGetValue(nameof(Arguments), out var arguments))
{
Arguments = arguments.StringOrNull();
}
Parameters = new Dictionary<string, string>();
if (doc.TryGetValue(nameof(Parameters), out var parameters))
{
Expand Down
5 changes: 4 additions & 1 deletion src/Hangfire.Mongo/Dto/KeyJobDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ protected KeyJobDto(BsonDocument doc) : base(doc)
{
return;
}
Key = doc[nameof(Key)].StringOrNull();
if (doc.TryGetValue(nameof(Key), out var key))
{
Key = key.StringOrNull();
}
}

protected override void Serialize(BsonDocument document)
Expand Down
10 changes: 8 additions & 2 deletions src/Hangfire.Mongo/Dto/ListDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ public ListDto()
}
public ListDto(BsonDocument doc) : base(doc)
{
Item = doc[nameof(Item)].StringOrNull();
Value = doc[nameof(Value)].StringOrNull();
if (doc.TryGetValue(nameof(Item), out var item))
{
Item = item.StringOrNull();
}
if (doc.TryGetValue(nameof(Value), out var value))
{
Value = value.StringOrNull();
}
}
public string Item { get; set; }

Expand Down
5 changes: 4 additions & 1 deletion src/Hangfire.Mongo/Dto/NotificationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public NotificationDto(BsonDocument doc)
{
Id = doc["_id"].AsObjectId;
Type = (NotificationType)doc[nameof(Type)].AsInt32;
Value = doc[nameof(Value)].StringOrNull();
if (doc.TryGetValue(nameof(Value), out var value))
{
Value = value.StringOrNull();
}
}

[BsonId]
Expand Down
15 changes: 12 additions & 3 deletions src/Hangfire.Mongo/Dto/ServerDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ public ServerDto(BsonDocument doc)
{
Id = doc["_id"].AsString;
WorkerCount = doc[nameof(WorkerCount)].AsInt32;
Queues = doc[nameof(Queues)].AsBsonArray.Select(q => q.StringOrNull()).ToArray();
StartedAt = doc[nameof(StartedAt)].ToNullableUniversalTime();
LastHeartbeat = doc[nameof(LastHeartbeat)].ToNullableUniversalTime();
if (doc.TryGetValue(nameof(Queues), out var queues))
{
Queues = queues.AsBsonArray.Select(q => q.StringOrNull()).ToArray();
}
if (doc.TryGetValue(nameof(StartedAt), out var startedAt))
{
StartedAt = startedAt.ToNullableUniversalTime();
}
if (doc.TryGetValue(nameof(LastHeartbeat), out var lastHeartbeat))
{
LastHeartbeat = lastHeartbeat.ToNullableUniversalTime();
}
}
public string Id { get; set; }

Expand Down
10 changes: 8 additions & 2 deletions src/Hangfire.Mongo/Dto/SetDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ public SetDto(BsonDocument doc) : base(doc)
}

Score = doc[nameof(Score)].AsDouble;
Value = doc[nameof(Value)].StringOrNull();
SetType = doc[nameof(SetType)].StringOrNull();
if (doc.TryGetValue(nameof(Value), out var value))
{
Value = value.StringOrNull();
}
if (doc.TryGetValue(nameof(SetType), out var setType))
{
SetType = setType.StringOrNull();
}
}
public double Score { get; set; }

Expand Down
10 changes: 8 additions & 2 deletions src/Hangfire.Mongo/Dto/StateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ public StateDto()
}
public StateDto(BsonDocument doc)
{
Name = doc[nameof(Name)].StringOrNull();
Reason = doc[nameof(Reason)].StringOrNull();
if (doc.TryGetValue(nameof(Name), out var name))
{
Name = name.StringOrNull();
}
if (doc.TryGetValue(nameof(Reason), out var reason))
{
Reason = reason.StringOrNull();
}
CreatedAt = doc[nameof(CreatedAt)].ToUniversalTime();
Data = new Dictionary<string, string>();
if(doc.TryGetValue(nameof(Data), out var data))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool Execute(IMongoDatabase database, MongoStorageOptions storageOptions,
["$set"] = new BsonDocument
{
["Queue"] = jobQueue["Queue"],
["FetchedAt"] = jobQueue["FetchedAt"]
["FetchedAt"] = jobQueue.TryGetValue("FetchedAt", out var fetchedAt) ? fetchedAt : BsonNull.Value
}
}
));
Expand Down

0 comments on commit 823d80e

Please sign in to comment.