Skip to content

Commit

Permalink
Merge pull request #602 from PinguApps/bug/fix-serializing-documents
Browse files Browse the repository at this point in the history
Applied a fix serializing documents
  • Loading branch information
pingu2k4 authored Jan 2, 2025
2 parents 06c40e9 + 9fcb947 commit 95c59cb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Shared/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace PinguApps.Appwrite.Shared;
public static class Constants
{
public const string Version = "1.0.1";
public const string Version = "1.0.2";
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ public override void Write(Utf8JsonWriter writer, Document value, JsonSerializer
dateTimeConverter.Write(writer, value.UpdatedAt, options);

writer.WritePropertyName("$permissions");
JsonSerializer.Serialize(writer, value.Permissions, options);
if (value.Permissions is null)
{
writer.WriteNullValue();
}
else
{
JsonSerializer.Serialize(writer, value.Permissions, options);
}

// Write dynamic properties from the Data dictionary
foreach (var kvp in value.Data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using PinguApps.Appwrite.Shared.Responses;
Expand Down Expand Up @@ -199,7 +198,14 @@ public override void Write(Utf8JsonWriter writer, Document<TData> value, JsonSer
dateTimeConverter.Write(writer, value.UpdatedAt, options);

writer.WritePropertyName("$permissions");
permissionsListConverter.Write(writer, value.Permissions.ToList(), options);
if (value.Permissions is null)
{
writer.WriteNullValue();
}
else
{
permissionsListConverter.Write(writer, [.. value.Permissions], options);
}

// Serialize the Data property
if (value.Data is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,33 @@ public void Write_ObjectValue_SerializesCorrectly()

Assert.Contains("\"objectField\":{}}", json);
}

[Fact]
public void Write_NullPermissions_WritesNull()
{
var document = new Document(
"1",
"col1",
"db1",
DateTime.Parse("2020-10-15T06:38:00.000+00:00"),
DateTime.Parse("2020-10-15T06:38:00.000+00:00"),
null,
new Dictionary<string, object?> { { "customField", "customValue" } }
);

var json = JsonSerializer.Serialize(document, _options);

var expectedJson = @"
{
""$id"": ""1"",
""$collectionId"": ""col1"",
""$databaseId"": ""db1"",
""$createdAt"": ""2020-10-15T06:38:00.000+00:00"",
""$updatedAt"": ""2020-10-15T06:38:00.000+00:00"",
""$permissions"": null,
""customField"": ""customValue""
}".ReplaceLineEndings("").Replace(" ", "");

Assert.Equal(JsonDocument.Parse(expectedJson).RootElement.ToString(), JsonDocument.Parse(json).RootElement.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,51 @@ public void WriteValue_DoubleNumber_WritesDoubleValue()
Assert.Equal(JsonValueKind.Number, doubleFieldElement.ValueKind);
Assert.Equal(1.23e20, doubleFieldElement.GetDouble());
}

[Fact]
public void Write_NullPermissions_WritesNull()
{
var testData = new TestData
{
Field1 = "value1",
Field2 = 42,
Field3 = true,
Field4 = DateTime.Parse("2020-10-15T06:38:00.000+00:00"),
Field5 = ["item1", "item2"],
Field6 = new Dictionary<string, object?> { { "key1", "value1" }, { "key2", 2 } }
};

var document = new Document<TestData>(
"1",
"col1",
"db1",
DateTime.Parse("2020-10-15T06:38:00.000+00:00"),
DateTime.Parse("2020-10-15T06:38:00.000+00:00"),
null,
testData
);

var json = JsonSerializer.Serialize(document, _options);

var expectedJson = @"
{
""$id"": ""1"",
""$collectionId"": ""col1"",
""$databaseId"": ""db1"",
""$createdAt"": ""2020-10-15T06:38:00.000+00:00"",
""$updatedAt"": ""2020-10-15T06:38:00.000+00:00"",
""$permissions"": null,
""Field1"": ""value1"",
""Field2"": 42,
""Field3"": true,
""Field4"": ""2020-10-15T06:38:00.000+00:00"",
""Field5"": [""item1"", ""item2""],
""Field6"": { ""key1"": ""value1"", ""key2"": 2 },
""FloatField"": null,
""LongField"": null,
""DoubleField"": null
}".ReplaceLineEndings("").Replace(" ", "");

Assert.Equal(JsonDocument.Parse(expectedJson).RootElement.ToString(), JsonDocument.Parse(json).RootElement.ToString());
}
}

0 comments on commit 95c59cb

Please sign in to comment.