diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs index e0b7e1121f..61c277028c 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs @@ -42,9 +42,9 @@ public T FromStream(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)) diff --git a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs index 4488136702..5ef7d43f76 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs @@ -45,9 +45,9 @@ public T FromStream(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)) diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs index 589bd2051e..2bc74c0967 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs @@ -640,9 +640,9 @@ private static T FromStream(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)) diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs index 710ebce662..14dca9fb19 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs @@ -38,9 +38,9 @@ public T FromStream(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)) diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs index 3331f5155b..59de07fdfd 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs @@ -35,9 +35,9 @@ public override T FromStream(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); diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs index 279bf95c71..a89ed40b39 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs @@ -91,9 +91,9 @@ public override T FromStream(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(); diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs index 6fcf5ee2c9..113f2a38c6 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs @@ -40,9 +40,9 @@ public override T FromStream(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) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs index f8deb921cd..3b4b9c1887 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs @@ -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 with a MemoryStream should succeed + Stream result = cosmosDefaultJsonSerializer.FromStream(memoryStream); + Assert.IsNotNull(result); + Assert.AreSame(memoryStream, result); + } + [TestMethod] public void ValidateSerializer() { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Json/CosmosSystemTextJsonSerializerTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Json/CosmosSystemTextJsonSerializerTest.cs index 268caef4fb..5756b70547 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Json/CosmosSystemTextJsonSerializerTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Json/CosmosSystemTextJsonSerializerTest.cs @@ -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 with a MemoryStream should succeed. + Stream result = this.stjSerializer.FromStream(memoryStream); + + // Assert. + Assert.IsNotNull(result); + Assert.AreSame(memoryStream, result); + } + } -} \ No newline at end of file +}