Skip to content

Commit

Permalink
Merge pull request #608 from PinguApps/bug/document-read
Browse files Browse the repository at this point in the history
Fixed reading json for documents
  • Loading branch information
pingu2k4 authored Jan 3, 2025
2 parents a3bcd81 + 065a184 commit 8c948ec
Show file tree
Hide file tree
Showing 6 changed files with 46 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.3";
public const string Version = "1.0.4";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void ValidateStartObject(ref Utf8JsonReader reader)

private DocumentFields ReadDocumentFields(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
var dateTimeConverter = new MultiFormatDateTimeConverter();
var dateTimeConverter = new NullableDateTimeConverter();
var permissionListConverter = new PermissionListConverter();
var fields = new DocumentFields();

Expand All @@ -64,7 +64,7 @@ private DocumentFields ReadDocumentFields(ref Utf8JsonReader reader, JsonSeriali
}

private static void ProcessProperty(ref Utf8JsonReader reader, string propertyName, DocumentFields fields,
MultiFormatDateTimeConverter dateTimeConverter, PermissionListConverter permissionListConverter, JsonSerializerOptions options)
NullableDateTimeConverter dateTimeConverter, PermissionListConverter permissionListConverter, JsonSerializerOptions options)
{
switch (propertyName)
{
Expand Down Expand Up @@ -223,7 +223,7 @@ public override void Write(Utf8JsonWriter writer, Document<TData> value, JsonSer

internal void WriteValue(Utf8JsonWriter writer, JsonElement element, JsonSerializerOptions options)
{
var dateTimeConverter = new MultiFormatDateTimeConverter();
var dateTimeConverter = new NullableDateTimeConverter();

switch (element.ValueKind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class NullableDateTimeConverter : JsonConverter<DateTime?>

public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

if (reader.TokenType == JsonTokenType.String)
{
var stringValue = reader.GetString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class PermissionListConverter : JsonConverter<List<Permission>>

public override List<Permission>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

if (reader.TokenType is not JsonTokenType.StartArray)
{
throw new JsonException("Expected start of array");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,20 @@ public void Write_WhenValueIsNull_WritesNullValue()
var json = Encoding.UTF8.GetString(stream.ToArray());
Assert.Equal("null", json);
}

[Fact]
public void Read_DirectNullToken_ReturnsNull()
{
// Arrange
var converter = new NullableDateTimeConverter();
var json = "null";
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
reader.Read(); // Advance to first token

// Act
var result = converter.Read(ref reader, typeof(DateTime?), _options);

// Assert
Assert.Null(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,20 @@ public void Write_MultiplePermissions_SerializesCorrectly()
var expectedJson = "[\"read(\\\"any\\\")\",\"write(\\\"user:123/verified\\\")\",\"create(\\\"team:456/admin\\\")\"]";
Assert.Equal(expectedJson, json);
}

[Fact]
public void Read_DirectNullToken_ReturnsNull()
{
// Arrange
var converter = new PermissionListConverter();
var json = "null";
var reader = new Utf8JsonReader(System.Text.Encoding.UTF8.GetBytes(json));
reader.Read(); // Advance to first token

// Act
var result = converter.Read(ref reader, typeof(List<Permission>), _options);

// Assert
Assert.Null(result);
}
}

0 comments on commit 8c948ec

Please sign in to comment.