From b4858ce527539e537d625762864609d43d0d86df Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 23 Apr 2024 17:03:07 +0300 Subject: [PATCH 1/6] Validates invocation count --- .../Store/InMemoryBackingStoreTests.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs b/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs index 0758b56f..e75fcb08 100644 --- a/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs @@ -440,6 +440,31 @@ public void TestsBackingStoreEmbeddedInModelWithByUpdatingNestedIBackedModelColl var colleagueSubscriptions = GetSubscriptionsPropertyFromBackingStore(testUser.Colleagues[0].BackingStore); Assert.Single(colleagueSubscriptions);// only one subscription to be invoked for the collection "colleagues" } + + [Fact] + public void TestsBackingStoreNestedInvocationCounts() + { + // Arrange dummy user with initialized backing store + var invocationCount = 0; + var testUser = new TestEntity(); + testUser.BackingStore.Subscribe((_, _, _) => invocationCount++, "testId"); + testUser.Id = "84c747c1-d2c0-410d-ba50-fc23e0b4abbe"; + var colleagues = new List(); + for(int i = 0; i < 10; i++) + { + colleagues.Add(new TestEntity + { + Id = "2fe22fe5-1132-42cf-90f9-1dc17e325a74", + BusinessPhones = new List + { + "+1 234 567 891" + } + }); + } + testUser.Colleagues = colleagues; + testUser.BackingStore.InitializationCompleted = true; + Assert.Equal(2,invocationCount); + } /// /// Helper function to pull out the private `subscriptions` collection property from the InMemoryBackingStore class From d97441665323649a40186c1beca648c1c8e5a953 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 23 Apr 2024 17:08:25 +0300 Subject: [PATCH 2/6] Testing --- .../Store/InMemoryBackingStoreTests.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs b/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs index e75fcb08..27993a78 100644 --- a/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/Store/InMemoryBackingStoreTests.cs @@ -448,7 +448,7 @@ public void TestsBackingStoreNestedInvocationCounts() var invocationCount = 0; var testUser = new TestEntity(); testUser.BackingStore.Subscribe((_, _, _) => invocationCount++, "testId"); - testUser.Id = "84c747c1-d2c0-410d-ba50-fc23e0b4abbe"; + testUser.Id = "84c747c1-d2c0-410d-ba50-fc23e0b4abbe"; // invocation site 1 var colleagues = new List(); for(int i = 0; i < 10; i++) { @@ -461,9 +461,10 @@ public void TestsBackingStoreNestedInvocationCounts() } }); } - testUser.Colleagues = colleagues; - testUser.BackingStore.InitializationCompleted = true; - Assert.Equal(2,invocationCount); + testUser.Colleagues = colleagues; // invocation site 2 + testUser.BackingStore.InitializationCompleted = true; // initialize + + Assert.Equal(2,invocationCount);// only called twice } /// From 8f7019e940ed48746d1b54b2c735e799a219e043 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 02:21:28 +0000 Subject: [PATCH 3/6] Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0 Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.0.0 to 2.1.0. - [Release notes](https://github.com/dependabot/fetch-metadata/releases) - [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.0.0...v2.1.0) --- updated-dependencies: - dependency-name: dependabot/fetch-metadata dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/auto-merge-dependabot.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/auto-merge-dependabot.yml b/.github/workflows/auto-merge-dependabot.yml index 145487da..72d96dd1 100644 --- a/.github/workflows/auto-merge-dependabot.yml +++ b/.github/workflows/auto-merge-dependabot.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v2.0.0 + uses: dependabot/fetch-metadata@v2.1.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" From 48d51c93dad58237fd8b192ae816c34538ed6859 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 29 Apr 2024 02:46:55 +0000 Subject: [PATCH 4/6] Bump xunit from 2.7.1 to 2.8.0 Bumps [xunit](https://github.com/xunit/xunit) from 2.7.1 to 2.8.0. - [Commits](https://github.com/xunit/xunit/compare/2.7.1...2.8.0) --- updated-dependencies: - dependency-name: xunit dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .../Microsoft.Kiota.Abstractions.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj b/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj index 47d57a54..f13282b2 100644 --- a/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj +++ b/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj @@ -18,7 +18,7 @@ environments https://github.com/microsoft/vstest/issues/2469--> - + runtime; build; native; contentfiles; analyzers; buildtransitive all From ce440b0aaf21bd4869f8e4a207e200b3c959e541 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 29 Apr 2024 06:23:21 +0000 Subject: [PATCH 5/6] Bump xunit.runner.visualstudio from 2.5.8 to 2.8.0 Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.5.8 to 2.8.0. - [Release notes](https://github.com/xunit/visualstudio.xunit/releases) - [Commits](https://github.com/xunit/visualstudio.xunit/compare/2.5.8...2.8.0) --- updated-dependencies: - dependency-name: xunit.runner.visualstudio dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .../Microsoft.Kiota.Abstractions.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj b/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj index f13282b2..c2424637 100644 --- a/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj +++ b/Microsoft.Kiota.Abstractions.Tests/Microsoft.Kiota.Abstractions.Tests.csproj @@ -19,7 +19,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all From 5cb32272500938964fed896c9acd1a1e4590ca4e Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 3 May 2024 11:19:22 -0400 Subject: [PATCH 6/6] - adds async overloads for serialization Signed-off-by: Vincent Biret --- CHANGELOG.md | 20 +++++----- .../SerializationHelpersTests.cs | 9 +++-- .../KiotaJsonSerializer.Serialization.cs | 24 +++++++++++ .../KiotaSerializer.Serialization.cs | 40 +++++++++++++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e353bc..2f3333e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,29 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Adds async overloads for serialization helpers + ## [1.8.4] - 2024-04-19 - Bumps Std.UriTemplate to version 0.0.57 ## [1.8.3] - 2024-04-18 -- Have set the license expression on the nuget package rather than bundling in a file (https://github.com/microsoft/kiota-abstractions-dotnet/issues/219) +- Have set the license expression on the nuget package rather than bundling in a file () ## [1.8.2] - 2024-04-18 -- Have made System.Diagnostics.DiagnosticSource only be included on Net Standard's TFM & net 5 (https://github.com/microsoft/kiota-abstractions-dotnet/issues/218) +- Have made System.Diagnostics.DiagnosticSource only be included on Net Standard's TFM & net 5 () ## [1.8.1] - 2024-03-26 ### Changed -- `MultipartBody` now supports an optional `fileName` parameter to specify the file name of the part. (https://github.com/microsoft/kiota-abstractions-dotnet/issues/212) +- `MultipartBody` now supports an optional `fileName` parameter to specify the file name of the part. () ## [1.8.0] - 2024-03-18 ### Added -- Added support for untyped nodes. (https://github.com/microsoft/kiota-abstractions-dotnet/issues/175) +- Added support for untyped nodes. () ## [1.7.12] - 2024-03-08 @@ -44,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Updated IParseNode enum methods `DynamicallyAccessedMembersAttribute` to `PublicFields`. - Fixed AOT compiler warnings from ILC. - + ## [1.7.10] - 2024-02-26 ### Changed @@ -74,20 +76,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Improve AllowedHost validator to throw an error if `https://` or `http://` prefix is present in a allowed host value.(https://github.com/microsoft/kiota-abstractions-dotnet/issues/165) +- Improve AllowedHost validator to throw an error if `https://` or `http://` prefix is present in a allowed host value.() ## [1.7.5] - 2024-01-11 ### Changed -- Fixes missing query parameters when the parameter values are empty strings.(https://github.com/microsoft/kiota-abstractions-dotnet/issues/172) +- Fixes missing query parameters when the parameter values are empty strings.() ## [1.7.4] - 2024-01-09 ### Changed - Fixed Method not found error due to conflicting dependencies by updating Std.UriTemplate dependency. -- Fixed unicode characters decoding in URI (https://github.com/std-uritemplate/std-uritemplate/issues/114). +- Fixed unicode characters decoding in URI (). ## [1.7.3] - 2023-11-30 @@ -166,7 +168,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed a bug where excess duplicate subscriptions would be created on the same property in the backing store causing performance issues in some scenarios. Related to https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1994 +- Fixed a bug where excess duplicate subscriptions would be created on the same property in the backing store causing performance issues in some scenarios. Related to ## [1.3.0] - 2023-08-01 diff --git a/Microsoft.Kiota.Abstractions.Tests/Serialization/SerializationHelpersTests.cs b/Microsoft.Kiota.Abstractions.Tests/Serialization/SerializationHelpersTests.cs index 79d8e2a9..0b119223 100644 --- a/Microsoft.Kiota.Abstractions.Tests/Serialization/SerializationHelpersTests.cs +++ b/Microsoft.Kiota.Abstractions.Tests/Serialization/SerializationHelpersTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Threading.Tasks; using Microsoft.Kiota.Abstractions.Serialization; using Microsoft.Kiota.Abstractions.Tests.Mocks; using Moq; @@ -25,7 +26,7 @@ public void DefensiveObjectCollection() Assert.Throws(() => KiotaSerializer.SerializeAsStream(_jsonContentType, (IEnumerable)null)); } [Fact] - public void SerializesObject() + public async Task SerializesObject() { var mockSerializationWriter = new Mock(); mockSerializationWriter.Setup(x => x.GetSerializedContent()).Returns(new MemoryStream(UTF8Encoding.UTF8.GetBytes("{'id':'123'}"))); @@ -33,7 +34,7 @@ public void SerializesObject() mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny())).Returns(mockSerializationWriter.Object); SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object; - var result = KiotaSerializer.SerializeAsString(_jsonContentType, new TestEntity() + var result = await KiotaSerializer.SerializeAsStringAsync(_jsonContentType, new TestEntity() { Id = "123" }); @@ -45,7 +46,7 @@ public void SerializesObject() mockSerializationWriter.Verify(x => x.GetSerializedContent(), Times.Once); } [Fact] - public void SerializesObjectCollection() + public async Task SerializesObjectCollection() { var mockSerializationWriter = new Mock(); mockSerializationWriter.Setup(x => x.GetSerializedContent()).Returns(new MemoryStream(UTF8Encoding.UTF8.GetBytes("[{'id':'123'}]"))); @@ -53,7 +54,7 @@ public void SerializesObjectCollection() mockSerializationWriterFactory.Setup(x => x.GetSerializationWriter(It.IsAny())).Returns(mockSerializationWriter.Object); SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories[_jsonContentType] = mockSerializationWriterFactory.Object; - var result = KiotaSerializer.SerializeAsString(_jsonContentType, new List { + var result = await KiotaSerializer.SerializeAsStringAsync(_jsonContentType, new List { new() { Id = "123" diff --git a/src/serialization/KiotaJsonSerializer.Serialization.cs b/src/serialization/KiotaJsonSerializer.Serialization.cs index aa87f420..a35df57a 100644 --- a/src/serialization/KiotaJsonSerializer.Serialization.cs +++ b/src/serialization/KiotaJsonSerializer.Serialization.cs @@ -2,8 +2,11 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ +using System; using System.Collections.Generic; using System.IO; +using System.Threading; +using System.Threading.Tasks; #if NET5_0_OR_GREATER using System.Diagnostics.CodeAnalysis; #endif @@ -27,9 +30,20 @@ public static Stream SerializeAsStream(T value) where T : IParsable /// Type of the object to serialize /// The object to serialize. /// The serialized representation as a string. + [Obsolete("This method is obsolete, use the async method instead")] public static string SerializeAsString(T value) where T : IParsable => KiotaSerializer.SerializeAsString(_jsonContentType, value); + /// + /// Serializes the given object into a string based on the content type. + /// + /// Type of the object to serialize + /// The object to serialize. + /// The token to monitor for cancellation requests. + /// The serialized representation as a string. + public static Task SerializeAsStringAsync(T value, CancellationToken cancellationToken = default) where T : IParsable + => KiotaSerializer.SerializeAsStringAsync(_jsonContentType, value, cancellationToken); + /// /// Serializes the given object into a string based on the content type. /// @@ -45,7 +59,17 @@ public static Stream SerializeAsStream(IEnumerable value) where T : IParsa /// Type of the object to serialize /// The object to serialize. /// The serialized representation as a string. + [Obsolete("This method is obsolete, use the async method instead")] public static string SerializeAsString(IEnumerable value) where T : IParsable => KiotaSerializer.SerializeAsString(_jsonContentType, value); + /// + /// Serializes the given object into a string based on the content type. + /// + /// Type of the object to serialize + /// The object to serialize. + /// The token to monitor for cancellation requests. + /// The serialized representation as a string. + public static Task SerializeAsStringAsync(IEnumerable value, CancellationToken cancellationToken = default) where T : IParsable + => KiotaSerializer.SerializeAsStringAsync(_jsonContentType, value, cancellationToken); } \ No newline at end of file diff --git a/src/serialization/KiotaSerializer.Serialization.cs b/src/serialization/KiotaSerializer.Serialization.cs index 7abbd99b..732b89e4 100644 --- a/src/serialization/KiotaSerializer.Serialization.cs +++ b/src/serialization/KiotaSerializer.Serialization.cs @@ -5,6 +5,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; +using System.Threading.Tasks; #if NET5_0_OR_GREATER using System.Diagnostics.CodeAnalysis; #endif @@ -37,6 +39,7 @@ public static Stream SerializeAsStream(string contentType, T value) where T : /// Content type to serialize the object to /// The object to serialize. /// The serialized representation as a string. + [Obsolete("This method is obsolete, use the async method instead")] public static string SerializeAsString(string contentType, T value) where T : IParsable { using var stream = SerializeAsStream(contentType, value); @@ -48,6 +51,19 @@ public static string SerializeAsString(string contentType, T value) where T : /// Type of the object to serialize /// Content type to serialize the object to /// The object to serialize. + /// The token to monitor for cancellation requests. + /// The serialized representation as a string. + public static Task SerializeAsStringAsync(string contentType, T value, CancellationToken cancellationToken = default) where T : IParsable + { + using var stream = SerializeAsStream(contentType, value); + return GetStringFromStreamAsync(stream, cancellationToken); + } + /// + /// Serializes the given object into a string based on the content type. + /// + /// Type of the object to serialize + /// Content type to serialize the object to + /// The object to serialize. /// The serialized representation as a stream. public static Stream SerializeAsStream(string contentType, IEnumerable value) where T : IParsable { @@ -62,16 +78,40 @@ public static Stream SerializeAsStream(string contentType, IEnumerable val /// Content type to serialize the object to /// The object to serialize. /// The serialized representation as a string. + [Obsolete("This method is obsolete, use the async method instead")] public static string SerializeAsString(string contentType, IEnumerable value) where T : IParsable { using var stream = SerializeAsStream(contentType, value); return GetStringFromStream(stream); } + /// + /// Serializes the given object into a string based on the content type. + /// + /// Type of the object to serialize + /// Content type to serialize the object to + /// The object to serialize. + /// The token to monitor for cancellation requests. + /// The serialized representation as a string. + public static Task SerializeAsStringAsync(string contentType, IEnumerable value, CancellationToken cancellationToken = default) where T : IParsable + { + using var stream = SerializeAsStream(contentType, value); + return GetStringFromStreamAsync(stream, cancellationToken); + } + [Obsolete("This method is obsolete, use the async method instead")] private static string GetStringFromStream(Stream stream) { using var reader = new StreamReader(stream); return reader.ReadToEndAsync().ConfigureAwait(false).GetAwaiter().GetResult(); // so the asp.net projects don't get an error } + private static async Task GetStringFromStreamAsync(Stream stream, CancellationToken cancellationToken) + { + using var reader = new StreamReader(stream); +#if NET7_0_OR_GREATER + return await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); +#else + return await reader.ReadToEndAsync().ConfigureAwait(false); +#endif + } private static ISerializationWriter GetSerializationWriter(string contentType, object value) { if(string.IsNullOrEmpty(contentType)) throw new ArgumentNullException(nameof(contentType));