Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ public void NestedIntArrayRead_ShouldReturnNullAndKeepReading_GivenAJsonObject()
Coerce("""{"nestedIntArray":{},"stringArray":"{ok}"}""")
.Should().BeEquivalentTo(new { NestedIntArray = (List<List<int>>?) null, StringArray = new[] { "ok" } });

[TestMethod]
[DataRow("""{"intArray":[1,"x"],"stringArray":"{ok}"}""")]
[DataRow("""{"intArray":{},"stringArray":"{ok}"}""")]
public void IntArrayRead_ShouldReturnNullAndKeepReading_GivenABadJsonValue(string json) =>
Coerce(json).Should().BeEquivalentTo(new { IntArray = (List<int>?) null, StringArray = new[] { "ok" } });

[TestMethod]
[DataRow("""{"stringArray":["a",1],"intArray":"{1}"}""")]
[DataRow("""{"stringArray":{},"intArray":"{1}"}""")]
public void StringArrayRead_ShouldReturnNullAndKeepReading_GivenABadJsonValue(string json) =>
Coerce(json).Should().BeEquivalentTo(new { StringArray = (List<string>?) null, IntArray = new[] { 1 } });

[TestMethod]
public void IntArrayRead_ShouldReturnNull_GivenDeeplyNestedBraces() =>
Coerce(JsonSerializer.Serialize(new { intArray = new string('{', 100_000) })).IntArray.Should().BeNull();
Expand Down
4 changes: 4 additions & 0 deletions packages/Realtime/Realtime/Converters/IntArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class IntArrayConverter : JsonConverter<List<int>>
/// <inheritdoc />
public override List<int>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var start = reader;
try
{
switch (reader.TokenType)
Expand All @@ -32,11 +33,14 @@ public class IntArrayConverter : JsonConverter<List<int>>
list.Add(reader.GetInt32());
return list;
default:
reader.Skip();
return null;
}
}
catch
{
reader = start;
reader.Skip();
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private class NestedConverter<TElement> : JsonConverter<List<TElement>>
case JsonTokenType.StartArray:
var list = new List<TElement>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
list.Add(JsonSerializer.Deserialize<TElement>(ref reader, options)!);
list.Add(JsonSerializer.Deserialize<TElement>(ref reader, options) ?? throw new JsonException());
return list;
default:
reader.Skip();
Expand Down
4 changes: 4 additions & 0 deletions packages/Realtime/Realtime/Converters/StringArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class StringArrayConverter : JsonConverter<List<string>>
/// <inheritdoc />
public override List<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var start = reader;
try
{
switch (reader.TokenType)
Expand All @@ -32,11 +33,14 @@ public class StringArrayConverter : JsonConverter<List<string>>
list.Add(reader.GetString()!);
return list;
default:
reader.Skip();
return null;
}
}
catch
{
reader = start;
reader.Skip();
return null;
}
}
Expand Down
Loading