Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public T FromStream<T>(Stream stream)
{
ArgumentValidation.ThrowIfNull(stream);

if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

using (StreamReader sr = new (stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public T FromStream<T>(Stream stream)
throw new ArgumentNullException(nameof(stream));
}

if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

using (StreamReader sr = new StreamReader(stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ private static T FromStream<T>(Stream stream)
{
using (stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

using (StreamReader sr = new StreamReader(stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public T FromStream<T>(Stream stream)
throw new ArgumentNullException(nameof(stream));
}

if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

using (StreamReader sr = new StreamReader(stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public override T FromStream<T>(Stream stream)
return default;
}

if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public override T FromStream<T>(Stream stream)
{
using (stream)
{
if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

JsonSerializer jsonSerializer = this.GetSerializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public override T FromStream<T>(Stream stream)
if (stream == null)
throw new ArgumentNullException(nameof(stream));

if (typeof(Stream).IsAssignableFrom(typeof(T)))
if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream)
{
return (T)(object)stream;
return typedStream;
}

if (stream.CanSeek && stream.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public class CosmosJsonSerializerUnitTests

private readonly string toDoActivityJson = @"{""id"":""c1d433c1-369d-430e-91e5-14e3ce588f71"",""taskNum"":42,""cost"":1.7976931348623157E+308,""description"":""cosmos json serializer"",""status"":""TBD""}";

[TestMethod]
public void ValidateFromStreamWithBaseStreamType()
{
CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer();
using MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 });

// FromStream<Stream> with a MemoryStream should succeed
Stream result = cosmosDefaultJsonSerializer.FromStream<Stream>(memoryStream);
Assert.IsNotNull(result);
Assert.AreSame(memoryStream, result);
}

[TestMethod]
public void ValidateSerializer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,19 @@ public void TestPolymorphicSerialization_SerializeDeserialize_PreservesType()
Assert.AreEqual(original.Color, deserializedCircle.Color);
Assert.AreEqual(((Circle)original).Radius, deserializedCircle.Radius);
}
[TestMethod]
public void TestFromStreamWithBaseStreamType()
{
// Arrange.
MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 });

// Act - FromStream<Stream> with a MemoryStream should succeed.
Stream result = this.stjSerializer.FromStream<Stream>(memoryStream);

// Assert.
Assert.IsNotNull(result);
Assert.AreSame(memoryStream, result);
}

}
}
}
Loading