1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Fix Reminder serialization/deserialization (#166)

Closes #124 

Before we dive into this PR, let's explain a few rules of serialization
and deserialization in System.Text.Json (often referred as STJ).

The important rule of serialization is that fields are ***not***
serialized unless the serializer gets passed an instance of
`JsonSerializerOptions` that explicitly tells it to include fields.
However, properties are serialized by default.

The important rule of ***de***serialization is that class members are
only deserialized if:
1) In case of properties, they must have a setter
2) If they do not have a setter, the constructor must have an argument,
required or optional, that will act as the setter for that property.

Unfortunately, both of these rules were ignored in some commit that
refactored reminders. This PR is here to fix that issue by:
1) Converting fields in `Reminder.cs` to properties
2) Adding an optional argument to the `MemberData` constructor
This commit is contained in:
Octol1ttle 2023-10-17 17:01:24 +05:00 committed by GitHub
parent 67d44ff835
commit a326adb680
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View file

@ -5,10 +5,14 @@ namespace Octobot.Data;
/// </summary> /// </summary>
public sealed class MemberData public sealed class MemberData
{ {
public MemberData(ulong id, DateTimeOffset? bannedUntil = null) public MemberData(ulong id, DateTimeOffset? bannedUntil = null, List<Reminder>? reminders = null)
{ {
Id = id; Id = id;
BannedUntil = bannedUntil; BannedUntil = bannedUntil;
if (reminders is not null)
{
Reminders = reminders;
}
} }
public ulong Id { get; } public ulong Id { get; }

View file

@ -2,7 +2,7 @@ namespace Octobot.Data;
public struct Reminder public struct Reminder
{ {
public DateTimeOffset At; public DateTimeOffset At { get; init; }
public string Text; public string Text { get; init; }
public ulong Channel; public ulong Channel { get; init; }
} }