From 3fc146242d0d8159fb1cb13cabbc13d8a2e82234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Fri, 3 May 2024 08:50:34 +0200 Subject: [PATCH 1/7] Implements IAsyncParseNodeFactory interface --- CHANGELOG.md | 6 ++ .../JsonAsyncParseNodeFactoryTests.cs | 57 +++++++++++++++++++ src/JsonParseNodeFactory.cs | 27 ++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 Microsoft.Kiota.Serialization.Json.Tests/JsonAsyncParseNodeFactoryTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 87e8902..ee70e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.3.0] - 2024-05-06 + +### Added + +- Implements IAsyncParseNodeFactory interface which adds async support + ## [1.2.3] - 2024-04-25 ### Changed diff --git a/Microsoft.Kiota.Serialization.Json.Tests/JsonAsyncParseNodeFactoryTests.cs b/Microsoft.Kiota.Serialization.Json.Tests/JsonAsyncParseNodeFactoryTests.cs new file mode 100644 index 0000000..9005719 --- /dev/null +++ b/Microsoft.Kiota.Serialization.Json.Tests/JsonAsyncParseNodeFactoryTests.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Microsoft.Kiota.Serialization.Json.Tests +{ + public class JsonAsyncParseNodeFactoryTests + { + private readonly JsonParseNodeFactory _jsonParseNodeFactory; + private const string TestJsonString = "{\"key\":\"value\"}"; + + public JsonAsyncParseNodeFactoryTests() + { + _jsonParseNodeFactory = new JsonParseNodeFactory(); + } + + [Fact] + public async Task GetsWriterForJsonContentType() + { + using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var jsonWriter = await _jsonParseNodeFactory.GetRootParseNodeAsync(_jsonParseNodeFactory.ValidContentType, jsonStream); + + // Assert + Assert.NotNull(jsonWriter); + Assert.IsAssignableFrom(jsonWriter); + } + + [Fact] + public async Task ThrowsArgumentOutOfRangeExceptionForInvalidContentType() + { + var streamContentType = "application/octet-stream"; + using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var exception = await Assert.ThrowsAsync( + async () => await _jsonParseNodeFactory.GetRootParseNodeAsync(streamContentType, jsonStream)); + + // Assert + Assert.NotNull(exception); + Assert.Equal($"expected a {_jsonParseNodeFactory.ValidContentType} content type", exception.ParamName); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + public async Task ThrowsArgumentNullExceptionForNoContentType(string contentType) + { + using var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(TestJsonString)); + var exception = await Assert.ThrowsAsync( + async () => await _jsonParseNodeFactory.GetRootParseNodeAsync(contentType, jsonStream)); + + // Assert + Assert.NotNull(exception); + Assert.Equal("contentType", exception.ParamName); + } + } +} diff --git a/src/JsonParseNodeFactory.cs b/src/JsonParseNodeFactory.cs index 5a43615..41251da 100644 --- a/src/JsonParseNodeFactory.cs +++ b/src/JsonParseNodeFactory.cs @@ -5,6 +5,8 @@ using System; using System.IO; using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Kiota.Abstractions.Serialization; namespace Microsoft.Kiota.Serialization.Json @@ -12,7 +14,7 @@ namespace Microsoft.Kiota.Serialization.Json /// /// The implementation for json content types /// - public class JsonParseNodeFactory : IParseNodeFactory + public class JsonParseNodeFactory : IAsyncParseNodeFactory { private readonly KiotaJsonSerializationContext _jsonJsonSerializationContext; @@ -43,7 +45,8 @@ public JsonParseNodeFactory(KiotaJsonSerializationContext jsonJsonSerializationC /// /// The content type of the stream to be parsed /// The containing json to parse. - /// An instance of for json manipulation + /// An instance of for json manipulation + [Obsolete("Use GetRootParseNodeAsync instead")] public IParseNode GetRootParseNode(string contentType, Stream content) { if(string.IsNullOrEmpty(contentType)) @@ -56,5 +59,25 @@ public IParseNode GetRootParseNode(string contentType, Stream content) using var jsonDocument = JsonDocument.Parse(content); return new JsonParseNode(jsonDocument.RootElement.Clone(), _jsonJsonSerializationContext); } + /// + /// Asynchronously gets the root of the json to be read. + /// + /// The content type of the stream to be parsed + /// The containing json to parse. + /// The cancellation token for the task + /// An instance of for json manipulation + public async Task GetRootParseNodeAsync(string contentType, Stream content, + CancellationToken cancellationToken = default) + { + if(string.IsNullOrEmpty(contentType)) + throw new ArgumentNullException(nameof(contentType)); + else if(!ValidContentType.Equals(contentType, StringComparison.OrdinalIgnoreCase)) + throw new ArgumentOutOfRangeException($"expected a {ValidContentType} content type"); + + _ = content ?? throw new ArgumentNullException(nameof(content)); + + using var jsonDocument = await JsonDocument.ParseAsync(content); + return new JsonParseNode(jsonDocument.RootElement.Clone(), _jsonJsonSerializationContext); + } } } From b7515fc3033f3f93677ed57785a41915e691b2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Fri, 3 May 2024 13:40:34 +0200 Subject: [PATCH 2/7] Adds missing CancellationToken argument --- src/JsonParseNodeFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonParseNodeFactory.cs b/src/JsonParseNodeFactory.cs index 41251da..f693f2b 100644 --- a/src/JsonParseNodeFactory.cs +++ b/src/JsonParseNodeFactory.cs @@ -76,7 +76,7 @@ public async Task GetRootParseNodeAsync(string contentType, Stream c _ = content ?? throw new ArgumentNullException(nameof(content)); - using var jsonDocument = await JsonDocument.ParseAsync(content); + using var jsonDocument = await JsonDocument.ParseAsync(content, cancellationToken: cancellationToken); return new JsonParseNode(jsonDocument.RootElement.Clone(), _jsonJsonSerializationContext); } } From 44826fe59f2e2be1fdbe00a3ef86be3c3e548736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:38:23 +0200 Subject: [PATCH 3/7] Updates reference to kiota-abstractions-dotnet --- src/Microsoft.Kiota.Serialization.Json.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Kiota.Serialization.Json.csproj b/src/Microsoft.Kiota.Serialization.Json.csproj index beffd07..9524010 100644 --- a/src/Microsoft.Kiota.Serialization.Json.csproj +++ b/src/Microsoft.Kiota.Serialization.Json.csproj @@ -44,7 +44,7 @@ - + From 784295b5659d8e82e2e7874ed649c6ea562f35b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:38:32 +0200 Subject: [PATCH 4/7] Updates changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee70e13..8217681 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Implements IAsyncParseNodeFactory interface which adds async support +- Implements IAsyncParseNodeFactory interface which adds async support ## [1.2.3] - 2024-04-25 From d1c9b4fc132f53584a00ac54f05adaa00ac4a856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:39:26 +0200 Subject: [PATCH 5/7] Updates version --- src/Microsoft.Kiota.Serialization.Json.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Kiota.Serialization.Json.csproj b/src/Microsoft.Kiota.Serialization.Json.csproj index 9524010..97d17c5 100644 --- a/src/Microsoft.Kiota.Serialization.Json.csproj +++ b/src/Microsoft.Kiota.Serialization.Json.csproj @@ -15,7 +15,7 @@ https://aka.ms/kiota/docs true true - 1.2.3 + 1.3.0 true true From 45703e48d360974e527b255ee36b492b251e0416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Tue, 7 May 2024 10:46:26 +0200 Subject: [PATCH 6/7] Updates changelog version date --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8217681..0cdbf0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [1.3.0] - 2024-05-06 +## [1.3.0] - 2024-05-13 ### Added From 2025083eab72cd052e263b4ce7b60e1133f4de17 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 8 May 2024 10:45:23 -0400 Subject: [PATCH 7/7] Update src/JsonParseNodeFactory.cs Co-authored-by: Andrew Omondi --- src/JsonParseNodeFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonParseNodeFactory.cs b/src/JsonParseNodeFactory.cs index f693f2b..561d706 100644 --- a/src/JsonParseNodeFactory.cs +++ b/src/JsonParseNodeFactory.cs @@ -76,7 +76,7 @@ public async Task GetRootParseNodeAsync(string contentType, Stream c _ = content ?? throw new ArgumentNullException(nameof(content)); - using var jsonDocument = await JsonDocument.ParseAsync(content, cancellationToken: cancellationToken); + using var jsonDocument = await JsonDocument.ParseAsync(content, cancellationToken: cancellationToken).ConfigureAwait(false); return new JsonParseNode(jsonDocument.RootElement.Clone(), _jsonJsonSerializationContext); } }