From 55a4ce8646286646a45939588787ac016bd1aee7 Mon Sep 17 00:00:00 2001 From: Lucas Pedroza Date: Tue, 12 Nov 2024 15:14:41 +0100 Subject: [PATCH] Handle unmaterialized sets in PageSerializer --- .../Serializers/PageSerializer.Tests.cs | 12 ++++ Fauna/Serialization/PageSerializer.cs | 67 ++++++++++++------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Fauna.Test/Serialization/Serializers/PageSerializer.Tests.cs b/Fauna.Test/Serialization/Serializers/PageSerializer.Tests.cs index 6ab994bf..c3b63f16 100644 --- a/Fauna.Test/Serialization/Serializers/PageSerializer.Tests.cs +++ b/Fauna.Test/Serialization/Serializers/PageSerializer.Tests.cs @@ -64,4 +64,16 @@ public void DeserializeIntoPageWithUserDefinedClass() Assert.AreEqual(40, deserialized.Data[1].Age); Assert.AreEqual("next_page_cursor", deserialized.After); } + + [Test] + public void DeserializeUnmaterializedSet() + { + const string token = "aftertoken"; + const string wire = $$"""{"@set":"{{token}}"}"""; + + var serializer = Serializer.Generate>(s_ctx); + var deserialized = Helpers.Deserialize(serializer, s_ctx, wire)!; + Assert.IsEmpty(deserialized.Data); + Assert.AreEqual(token, deserialized.After); + } } diff --git a/Fauna/Serialization/PageSerializer.cs b/Fauna/Serialization/PageSerializer.cs index ffa9b7a9..3e4a3a71 100644 --- a/Fauna/Serialization/PageSerializer.cs +++ b/Fauna/Serialization/PageSerializer.cs @@ -1,4 +1,3 @@ -using Fauna.Exceptions; using Fauna.Mapping; using Fauna.Types; @@ -13,11 +12,11 @@ public PageSerializer(ISerializer elemSerializer) _dataSerializer = new ListSerializer(elemSerializer); } - public override List GetSupportedTypes() => new List { FaunaType.Null, FaunaType.Set }; + public override List GetSupportedTypes() => [FaunaType.Null, FaunaType.Set]; public override Page Deserialize(MappingContext ctx, ref Utf8FaunaReader reader) { - var wrapInPage = false; + bool wrapInPage = false; var endToken = TokenType.None; switch (reader.CurrentTokenType) { @@ -32,36 +31,52 @@ public override Page Deserialize(MappingContext ctx, ref Utf8FaunaReader read break; } - List? data = null; - string? after = null; - if (wrapInPage) { - data = _dataSerializer.Deserialize(ctx, ref reader); + var data = _dataSerializer.Deserialize(ctx, ref reader); + return new Page(data, null); } - else - { - while (reader.Read() && reader.CurrentTokenType != endToken) - { - var fieldName = reader.GetString()!; - reader.Read(); - switch (fieldName) - { - case "data": - data = _dataSerializer.Deserialize(ctx, ref reader); - break; - case "after": - after = reader.GetString()!; - break; - } - } + reader.Read(); + return reader.CurrentTokenType == TokenType.String + ? HandleUnmaterialized(ctx, ref reader, endToken) + : HandleMaterialized(ctx, ref reader, endToken); + } + + private Page HandleUnmaterialized(MappingContext ctx, ref Utf8FaunaReader reader, TokenType endToken) + { + string after = reader.GetString()!; + reader.Read(); + if (reader.CurrentTokenType != endToken) + { + throw UnexpectedToken(reader.CurrentTokenType); } - if (data is null) - throw new SerializationException($"No page data found while deserializing into {typeof(Page)}"); + return new Page([], after); + } + + private Page HandleMaterialized(MappingContext ctx, ref Utf8FaunaReader reader, TokenType endToken) + { + List data = []; + string? after = null; + + do + { + string fieldName = reader.GetString()!; + reader.Read(); + + switch (fieldName) + { + case "data": + data = _dataSerializer.Deserialize(ctx, ref reader); + break; + case "after": + after = reader.GetString()!; + break; + } + } while (reader.Read() && reader.CurrentTokenType != endToken); - return new Page(data!, after); + return new Page(data, after); } public override void Serialize(MappingContext context, Utf8FaunaWriter writer, object? o)