diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs index ef55457..1a1b323 100644 --- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs +++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureCollectionConverterTest.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Text.Json; using System.Text.Json.Serialization; using NetTopologySuite.Features; using NetTopologySuite.Geometries; @@ -68,5 +70,20 @@ public void TestSanD(OgcGeometryType type, int num, bool threeD) for (int i = 0; i < fc.Count; i++) FeatureConverterTest.CheckEquality(fc[i], d[i]); } + + [Test] + [GeoJsonIssueNumber(143)] + public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer() + { + var options = DefaultOptions; + var node = JsonSerializer.SerializeToNode(new FeatureCollection(), options)!; + node["_skippedProperty"] = "irrelevant"; + var nodes = Enumerable.Repeat(node, 500).ToArray(); + using SingleByteReadingMemoryStream stream = new(); + JsonSerializer.Serialize(stream, nodes, options); + stream.Position = 0; + var roundtrip = JsonSerializer.Deserialize(stream, options); + Assert.That(roundtrip, Has.Length.EqualTo(500)); + } } } diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs index 7dbc9e3..c0affb9 100644 --- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs +++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; @@ -128,6 +129,21 @@ public void WriteJsonWithIdTest(string idPropertyName, object id) CheckEquality(value, deserialized, idPropertyName); } + [Test] + [GeoJsonIssueNumber(143)] + public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer() + { + var options = DefaultOptions; + var node = JsonSerializer.SerializeToNode(new Feature(), options)!; + node["_skippedProperty"] = "irrelevant"; + var nodes = Enumerable.Repeat(node, 500).ToArray(); + using SingleByteReadingMemoryStream stream = new(); + JsonSerializer.Serialize(stream, nodes, options); + stream.Position = 0; + var roundtrip = JsonSerializer.Deserialize(stream, options); + Assert.That(roundtrip, Has.Length.EqualTo(500)); + } + public static IEnumerable FeatureIdTestCases { get @@ -244,7 +260,7 @@ public void TestNumericFeatureIdMustBeValidDecimal() }} }} "; - + Assert.That(() => JsonSerializer.Deserialize(serialized, DefaultOptions), Throws.InstanceOf()); } } diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/GeometryConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/GeometryConverterTest.cs index 8a1ddf4..efbb1e0 100644 --- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/GeometryConverterTest.cs +++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/GeometryConverterTest.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Linq; using System.Text.Json; using NetTopologySuite.Geometries; using NUnit.Framework; @@ -184,5 +185,20 @@ public void TestWriteReadWkt(string wkt) Assert.That(geomS.IsEmpty ? geomD.IsEmpty : geomS.EqualsTopologically(geomD)); } + + [Test] + [GeoJsonIssueNumber(143)] + public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer() + { + var options = DefaultOptions; + var node = JsonSerializer.SerializeToNode(Point.Empty, options)!; + node["_skippedProperty"] = "irrelevant"; + var nodes = Enumerable.Repeat(node, 500).ToArray(); + using SingleByteReadingMemoryStream stream = new(); + JsonSerializer.Serialize(stream, nodes, options); + stream.Position = 0; + var roundtrip = JsonSerializer.Deserialize(stream, options); + Assert.That(roundtrip, Has.Length.EqualTo(500)); + } } } diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/SingleByteReadingMemoryStream.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/SingleByteReadingMemoryStream.cs new file mode 100644 index 0000000..c132e65 --- /dev/null +++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/SingleByteReadingMemoryStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace NetTopologySuite.IO.GeoJSON4STJ.Test; + +/// +/// A implementation that will never read more than one byte at a time. +/// +internal sealed class SingleByteReadingMemoryStream : MemoryStream +{ + public override int Read(byte[] buffer, int offset, int count) + { + return base.Read(buffer, offset, Math.Min(1, count)); + } + + public override int Read(Span buffer) + { + return base.Read(buffer[.. Math.Min(1, buffer.Length)]); + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + return base.ReadAsync(buffer, offset, Math.Min(1, count), cancellationToken); + } + + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + return base.ReadAsync(buffer[.. Math.Min(1, buffer.Length)], cancellationToken); + } +}