y-crdt / ydotnet

.NET bindings for yrs.
MIT License
35 stars 8 forks source link

YDotnet.Extensions: Exception converting Map to strongly typed model #86

Closed akatakritos closed 7 months ago

akatakritos commented 7 months ago
    record TodoItem(string Title, bool IsDone);

    [Fact]
    public void CanParseListOfMapsToObjects()
    {
        var doc = new Doc();
        doc.Array("todos");
        using (var txn = doc.WriteTransaction())
        {
            var map = Input.Map(new Dictionary<string, Input>()
            {
                ["Title"] = Input.String("Make dinner"),
                ["IsDone"] = Input.Boolean(false)
            });
            txn.GetArray("todos").InsertRange(txn, 0, map);
        }

        using (var txn = doc.ReadTransaction())
        {
            var array = txn.GetArray("todos");
            var element = array.Get(txn, 0);
            var parsed = element.To<TodoItem>(txn);

            Assert.Equal(new TodoItem("Make dinner", false), parsed);
        }
    }
System.InvalidOperationException: Cannot write a JSON property within an array or as the first JSON token. Current token type is 'StartArray'.

System.InvalidOperationException
Cannot write a JSON property within an array or as the first JSON token. Current token type is 'StartArray'.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource, Int32 currentDepth, Int32 maxDepth, Byte token, JsonTokenType tokenType)
   at System.Text.Json.Utf8JsonWriter.WriteStringByOptionsPropertyName(ReadOnlySpan`1 propertyName)
   at System.Text.Json.Utf8JsonWriter.WritePropertyName(ReadOnlySpan`1 propertyName)
   at YDotNet.Extensions.YDotNetExtensions.<ToJson>g__WriteProperty|5_4(String key, Output value, Utf8JsonWriter jsonWriter, Transaction transaction)
   at YDotNet.Extensions.YDotNetExtensions.<ToJson>g__WriteMap|5_2(Map map, Utf8JsonWriter jsonWriter, Transaction transaction)
   at YDotNet.Extensions.YDotNetExtensions.<ToJson>g__WriteValue|5_5(Output output, Utf8JsonWriter jsonWriter, Transaction transaction)
   at YDotNet.Extensions.YDotNetExtensions.ToJson(Output output, Stream stream, Transaction transaction)
   at YDotNet.Extensions.YDotNetExtensions.To[T](Output output, Transaction transaction)
   at Ballpark.Tests.DocTests.CanParseListOfMapsToObjects() in /Users/mattburke/projects/ballpark/server/Ballpark.Tests/DocTests.cs:line 107
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

I think this is due to WriteMap doing a WriteStartArray and WriteEndArray instead of WriteStartObject and WriteEndObject: https://github.com/y-crdt/ydotnet/blob/0ed99e522f977203528c1cc4514efc5201d6ab9d/YDotNet.Extensions/YDotNetExtensions.cs#L94

Would you welcome a PR to add this test and fix the method?

SebastianStehle commented 7 months ago

Would you welcome a PR to add this test and fix the method?

Of course. Tests are also welcome :)