From 389b76d998abcdc5055425c0d218634146d77035 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 1 Aug 2024 12:54:08 +0300 Subject: [PATCH] Adds costomization support --- CHANGELOG.md | 1 + .../json/JsonSerializationWriter.cs | 6 +- .../json/JsonSerializationWriterTests.cs | 63 +++++++++++++++++++ .../serialization/text/TextParseNodeTests.cs | 6 +- 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63bedfae..dae766be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.10.1] - 2024-08-01 - Cleans up enum serialization to read from attributes for form and text serialization [#284](https://github.com/microsoft/kiota-dotnet/issues/284) +- Pass relevant `JsonWriterOptions` from the `KiotaJsonSerializationContext.Options` to the `Utf8JsonWriter` when writing json to enable customization. [#281](https://github.com/microsoft/kiota-dotnet/issues/281) ## [1.10.0] - 2024-07-17 diff --git a/src/serialization/json/JsonSerializationWriter.cs b/src/serialization/json/JsonSerializationWriter.cs index a7d8ca9d..30d574b8 100644 --- a/src/serialization/json/JsonSerializationWriter.cs +++ b/src/serialization/json/JsonSerializationWriter.cs @@ -48,7 +48,11 @@ public JsonSerializationWriter() public JsonSerializationWriter(KiotaJsonSerializationContext kiotaJsonSerializationContext) { _kiotaJsonSerializationContext = kiotaJsonSerializationContext; - writer = new Utf8JsonWriter(_stream); + writer = new Utf8JsonWriter(_stream, new JsonWriterOptions + { + Encoder = kiotaJsonSerializationContext.Options.Encoder, + Indented = kiotaJsonSerializationContext.Options.WriteIndented + }); } /// diff --git a/tests/serialization/json/JsonSerializationWriterTests.cs b/tests/serialization/json/JsonSerializationWriterTests.cs index 252e5eba..d2362f34 100644 --- a/tests/serialization/json/JsonSerializationWriterTests.cs +++ b/tests/serialization/json/JsonSerializationWriterTests.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.IO; using System.Text; +using System.Text.Encodings.Web; using System.Text.Json; using System.Threading; using Microsoft.Kiota.Abstractions; @@ -258,6 +259,68 @@ public void WriteGuidUsingConverter() Assert.Equal(expectedString, serializedJsonString); } + [Fact] + public void ForwardsOptionsToWriterFromSerializationContext() + { + // Arrange + var testEntity = new TestEntity + { + Id = "testId", + AdditionalData = new Dictionary() + { + {"href", "https://graph.microsoft.com/users/{user-id}"}, + {"unicodeName", "你好"} + } + }; + var serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.General) + { + WriteIndented = true, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; + var serializationContext = new KiotaJsonSerializationContext(serializerOptions); + using var jsonSerializerWriter = new JsonSerializationWriter(serializationContext); + + // Act + jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity); + var serializedStream = jsonSerializerWriter.GetSerializedContent(); + using var reader = new StreamReader(serializedStream, Encoding.UTF8); + var serializedJsonString = reader.ReadToEnd(); + + // Assert + const string expectedString = "{\n \"id\": \"testId\",\n \"href\": \"https://graph.microsoft.com/users/{user-id}\",\n \"unicodeName\": \"你好\"\n}"; + Assert.Contains("\n", serializedJsonString); // string is indented and not escaped + Assert.Contains("你好", serializedJsonString); // string is indented and not escaped + Assert.Equal(expectedString, serializedJsonString.Replace("\r", string.Empty)); // string is indented and not escaped + } + + [Fact] + public void UsesDefaultOptionsToWriterFromSerializationContext() + { + // Arrange + var testEntity = new TestEntity + { + Id = "testId", + AdditionalData = new Dictionary() + { + {"href", "https://graph.microsoft.com/users/{user-id}"}, + {"unicodeName", "你好"} + } + }; + using var jsonSerializerWriter = new JsonSerializationWriter(new KiotaJsonSerializationContext()); + + // Act + jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity); + var serializedStream = jsonSerializerWriter.GetSerializedContent(); + using var reader = new StreamReader(serializedStream, Encoding.UTF8); + var serializedJsonString = reader.ReadToEnd(); + + // Assert + var expectedString = $"{{\"id\":\"testId\",\"href\":\"https://graph.microsoft.com/users/{{user-id}}\",\"unicodeName\":\"\\u4F60\\u597D\"}}"; + Assert.DoesNotContain("\n", serializedJsonString); // string is not indented and not escaped + Assert.DoesNotContain("你好", serializedJsonString); // string is not indented and not escaped + Assert.Contains("\\u4F60\\u597D", serializedJsonString); // string is not indented and not escaped + Assert.Equal(expectedString, serializedJsonString); // string is indented and not escaped + } [Fact] public void WriteGuidUsingNoConverter() { diff --git a/tests/serialization/text/TextParseNodeTests.cs b/tests/serialization/text/TextParseNodeTests.cs index 4ac1dbae..39cfab12 100644 --- a/tests/serialization/text/TextParseNodeTests.cs +++ b/tests/serialization/text/TextParseNodeTests.cs @@ -41,12 +41,12 @@ public void TextParseNode_GetEnumFromString() [Fact] public void TextParseNode_GetEnumFromEnumMember() { - var text = "Value_2"; + var text = "Item2:SubItem1"; var parseNode = new TextParseNode(text); - var result = parseNode.GetEnumValue(); + var result = parseNode.GetEnumValue(); - Assert.Equal(TestEnum.SecondItem, result); + Assert.Equal(TestNamingEnum.Item2SubItem1, result); } [Fact]