From e9ced060810e14e43ba2a7bc498d256ba5685be6 Mon Sep 17 00:00:00 2001 From: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com> Date: Tue, 3 Mar 2026 11:43:04 -0800 Subject: [PATCH 1/3] Serializer: Fixes unsafe (T)(object)stream cast in FromStream (#5620) Replaces the unsafe (T)(object)stream cast pattern with safe 'is' pattern matching in all FromStream implementations. The old pattern could throw an InvalidCastException when T was a Stream subclass (e.g., MemoryStream) but the runtime stream was a different Stream type. The fix uses 'stream is T typedStream' for safe runtime type checking and throws a descriptive InvalidCastException when the types are incompatible. Fixed in 7 files (2 core SDK serializers, 3 samples, 2 encryption modules). Added unit tests for both CosmosJsonDotNetSerializer and CosmosSystemTextJsonSerializer confirming the fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/Common/CosmosJsonDotNetSerializer.cs | 8 +++++- .../src/CosmosJsonDotNetSerializer.cs | 8 +++++- .../Usage/ItemManagement/Program.cs | 8 +++++- .../ReEncryptionJsonSerializer.cs | 8 +++++- .../CosmosSystemTextJsonSerializer.cs | 8 +++++- .../Serializer/CosmosJsonDotNetSerializer.cs | 8 +++++- .../CosmosSystemTextJsonSerializer.cs | 8 +++++- .../CosmosJsonSerializerUnitTests.cs | 26 ++++++++++++++++++ .../CosmosSystemTextJsonSerializerTest.cs | 27 +++++++++++++++++++ 9 files changed, 102 insertions(+), 7 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs index e0b7e1121f..844e841e45 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs @@ -44,7 +44,13 @@ public T FromStream(Stream stream) if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..bd8f4cd821 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs @@ -47,7 +47,13 @@ public T FromStream(Stream stream) if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..6288313606 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs @@ -642,7 +642,13 @@ private static T FromStream(Stream stream) { if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..531a767bd4 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs @@ -40,7 +40,13 @@ public T FromStream(Stream stream) if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..6580812a05 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs @@ -37,7 +37,13 @@ public override T FromStream(Stream stream) if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..30273c9d21 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs @@ -93,7 +93,13 @@ public override T FromStream(Stream stream) { if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } JsonSerializer jsonSerializer = this.GetSerializer(); diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs index 6fcf5ee2c9..05e30487b0 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs @@ -42,7 +42,13 @@ public override T FromStream(Stream stream) if (typeof(Stream).IsAssignableFrom(typeof(T))) { - return (T)(object)stream; + if (stream is T typedStream) + { + return typedStream; + } + + throw new InvalidCastException( + $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); } 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..7fd1fa34f1 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,32 @@ 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 ValidateFromStreamWithIncompatibleStreamTypeThrowsDescriptiveError() + { + CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer(); + using MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 }); + + // FromStream with a MemoryStream should throw InvalidCastException + // with a descriptive message, not a raw InvalidCastException + InvalidCastException exception = Assert.ThrowsException( + () => cosmosDefaultJsonSerializer.FromStream(memoryStream)); + Assert.IsTrue(exception.Message.Contains("MemoryStream")); + Assert.IsTrue(exception.Message.Contains("FileStream")); + } + [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..dfd5cf7dc0 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,32 @@ 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); + } + + [TestMethod] + public void TestFromStreamWithIncompatibleStreamTypeThrowsDescriptiveError() + { + // Arrange. + MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 }); + + // Act and Assert - FromStream with a MemoryStream should throw + // InvalidCastException with a descriptive message. + InvalidCastException exception = Assert.ThrowsException( + () => this.stjSerializer.FromStream(memoryStream)); + Assert.IsTrue(exception.Message.Contains("MemoryStream")); + Assert.IsTrue(exception.Message.Contains("FileStream")); + } } } \ No newline at end of file From 72841f6aabd2b85f15df729c3cbba2cb94af81f4 Mon Sep 17 00:00:00 2001 From: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com> Date: Wed, 4 Mar 2026 12:02:00 -0800 Subject: [PATCH 2/3] Serializer: Simplifies FromStream pattern per review feedback Replaces the 3-part block (typeof guard + is check + throw) with a simple 'if (stream is T typedStream)' pattern match across all serializer files. Removes incompatible stream type tests since mismatched cases now fall through to deserialization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/Common/CosmosJsonDotNetSerializer.cs | 10 ++-------- .../src/CosmosJsonDotNetSerializer.cs | 10 ++-------- .../Usage/ItemManagement/Program.cs | 10 ++-------- .../ReEncryptionJsonSerializer.cs | 10 ++-------- .../CosmosSystemTextJsonSerializer.cs | 10 ++-------- .../src/Serializer/CosmosJsonDotNetSerializer.cs | 10 ++-------- .../Serializer/CosmosSystemTextJsonSerializer.cs | 10 ++-------- .../CosmosJsonSerializerUnitTests.cs | 14 -------------- .../Json/CosmosSystemTextJsonSerializerTest.cs | 13 ------------- 9 files changed, 14 insertions(+), 83 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs index 844e841e45..5a86777600 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs @@ -42,15 +42,9 @@ public T FromStream(Stream stream) { ArgumentValidation.ThrowIfNull(stream); - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 bd8f4cd821..82af3b2a2c 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs @@ -45,15 +45,9 @@ public T FromStream(Stream stream) throw new ArgumentNullException(nameof(stream)); } - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 6288313606..e0da7f5952 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs @@ -640,15 +640,9 @@ private static T FromStream(Stream stream) { using (stream) { - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 531a767bd4..f8f13e81d1 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs @@ -38,15 +38,9 @@ public T FromStream(Stream stream) throw new ArgumentNullException(nameof(stream)); } - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 6580812a05..80a6e86a76 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs @@ -35,15 +35,9 @@ public override T FromStream(Stream stream) return default; } - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 30273c9d21..9ef07f9deb 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs @@ -91,15 +91,9 @@ public override T FromStream(Stream stream) { using (stream) { - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 05e30487b0..0fd4999399 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs @@ -40,15 +40,9 @@ public override T FromStream(Stream stream) if (stream == null) throw new ArgumentNullException(nameof(stream)); - if (typeof(Stream).IsAssignableFrom(typeof(T))) + if (stream is T typedStream) { - if (stream is T typedStream) - { - return typedStream; - } - - throw new InvalidCastException( - $"Stream of type '{stream.GetType().FullName}' is not compatible with the requested type '{typeof(T).FullName}'."); + 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 7fd1fa34f1..3b4b9c1887 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosJsonSerializerUnitTests.cs @@ -44,20 +44,6 @@ public void ValidateFromStreamWithBaseStreamType() Assert.AreSame(memoryStream, result); } - [TestMethod] - public void ValidateFromStreamWithIncompatibleStreamTypeThrowsDescriptiveError() - { - CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer(); - using MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 }); - - // FromStream with a MemoryStream should throw InvalidCastException - // with a descriptive message, not a raw InvalidCastException - InvalidCastException exception = Assert.ThrowsException( - () => cosmosDefaultJsonSerializer.FromStream(memoryStream)); - Assert.IsTrue(exception.Message.Contains("MemoryStream")); - Assert.IsTrue(exception.Message.Contains("FileStream")); - } - [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 dfd5cf7dc0..c8afd0afa6 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 @@ -240,18 +240,5 @@ public void TestFromStreamWithBaseStreamType() Assert.AreSame(memoryStream, result); } - [TestMethod] - public void TestFromStreamWithIncompatibleStreamTypeThrowsDescriptiveError() - { - // Arrange. - MemoryStream memoryStream = new MemoryStream(new byte[] { 1, 2, 3 }); - - // Act and Assert - FromStream with a MemoryStream should throw - // InvalidCastException with a descriptive message. - InvalidCastException exception = Assert.ThrowsException( - () => this.stjSerializer.FromStream(memoryStream)); - Assert.IsTrue(exception.Message.Contains("MemoryStream")); - Assert.IsTrue(exception.Message.Contains("FileStream")); - } } } \ No newline at end of file From 7547b6641b0a14f173d227a7d6f7a6c734da1049 Mon Sep 17 00:00:00 2001 From: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com> Date: Thu, 5 Mar 2026 11:41:24 -0800 Subject: [PATCH 3/3] Serializer: Adds type guard to prevent stream bypass for non-Stream types The simplified 'stream is T' check was too broad - when T=object (e.g. dynamic), it always matched, returning the raw stream instead of deserializing. Restored the typeof(Stream).IsAssignableFrom(typeof(T)) guard in a combined condition. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/Common/CosmosJsonDotNetSerializer.cs | 2 +- .../src/CosmosJsonDotNetSerializer.cs | 2 +- Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs | 2 +- .../ReEncryptionSupport/ReEncryptionJsonSerializer.cs | 2 +- .../Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs | 2 +- .../src/Serializer/CosmosJsonDotNetSerializer.cs | 2 +- .../src/Serializer/CosmosSystemTextJsonSerializer.cs | 2 +- .../Json/CosmosSystemTextJsonSerializerTest.cs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs index 5a86777600..61c277028c 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Common/CosmosJsonDotNetSerializer.cs @@ -42,7 +42,7 @@ public T FromStream(Stream stream) { ArgumentValidation.ThrowIfNull(stream); - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs index 82af3b2a2c..5ef7d43f76 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/CosmosJsonDotNetSerializer.cs @@ -45,7 +45,7 @@ public T FromStream(Stream stream) throw new ArgumentNullException(nameof(stream)); } - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs index e0da7f5952..2bc74c0967 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs @@ -640,7 +640,7 @@ private static T FromStream(Stream stream) { using (stream) { - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs index f8f13e81d1..14dca9fb19 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ReEncryption/ReEncryptionSupport/ReEncryptionJsonSerializer.cs @@ -38,7 +38,7 @@ public T FromStream(Stream stream) throw new ArgumentNullException(nameof(stream)); } - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs index 80a6e86a76..59de07fdfd 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs @@ -35,7 +35,7 @@ public override T FromStream(Stream stream) return default; } - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs index 9ef07f9deb..a89ed40b39 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosJsonDotNetSerializer.cs @@ -91,7 +91,7 @@ public override T FromStream(Stream stream) { using (stream) { - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs index 0fd4999399..113f2a38c6 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSystemTextJsonSerializer.cs @@ -40,7 +40,7 @@ public override T FromStream(Stream stream) if (stream == null) throw new ArgumentNullException(nameof(stream)); - if (stream is T typedStream) + if (typeof(Stream).IsAssignableFrom(typeof(T)) && stream is T typedStream) { return typedStream; } 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 c8afd0afa6..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 @@ -241,4 +241,4 @@ public void TestFromStreamWithBaseStreamType() } } -} \ No newline at end of file +}