|
3 | 3 | using System.Globalization; |
4 | 4 | using System.IO; |
5 | 5 | using System.Text; |
| 6 | +using System.Text.Encodings.Web; |
6 | 7 | using System.Text.Json; |
7 | 8 | using System.Threading; |
8 | 9 | using Microsoft.Kiota.Abstractions; |
@@ -156,7 +157,7 @@ public void WritesSampleCollectionOfObjectValues() |
156 | 157 | // Assert |
157 | 158 | var expectedString = "[{" + |
158 | 159 | "\"id\":\"48d31887-5fad-4d73-a9f5-3c356e68a038\"," + |
159 | | - "\"numbers\":\"one,two\"," + |
| 160 | + "\"numbers\":\"One,Two\"," + |
160 | 161 | "\"testNamingEnum\":\"Item2:SubItem1\"," + |
161 | 162 | "\"mobilePhone\":null," + |
162 | 163 | "\"accountEnabled\":false," + |
@@ -205,7 +206,7 @@ public void WritesEnumValuesAsCamelCasedIfNotEscaped() |
205 | 206 |
|
206 | 207 | // Assert |
207 | 208 | var expectedString = "[{" + |
208 | | - "\"testNamingEnum\":\"item1\"" + // Camel Cased |
| 209 | + "\"testNamingEnum\":\"Item1\"" + // Camel Cased |
209 | 210 | "}]"; |
210 | 211 | Assert.Equal(expectedString, serializedJsonString); |
211 | 212 | } |
@@ -258,6 +259,68 @@ public void WriteGuidUsingConverter() |
258 | 259 | Assert.Equal(expectedString, serializedJsonString); |
259 | 260 | } |
260 | 261 |
|
| 262 | + [Fact] |
| 263 | + public void ForwardsOptionsToWriterFromSerializationContext() |
| 264 | + { |
| 265 | + // Arrange |
| 266 | + var testEntity = new TestEntity |
| 267 | + { |
| 268 | + Id = "testId", |
| 269 | + AdditionalData = new Dictionary<string, object>() |
| 270 | + { |
| 271 | + {"href", "https://graph.microsoft.com/users/{user-id}"}, |
| 272 | + {"unicodeName", "你好"} |
| 273 | + } |
| 274 | + }; |
| 275 | + var serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.General) |
| 276 | + { |
| 277 | + WriteIndented = true, |
| 278 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 279 | + }; |
| 280 | + var serializationContext = new KiotaJsonSerializationContext(serializerOptions); |
| 281 | + using var jsonSerializerWriter = new JsonSerializationWriter(serializationContext); |
| 282 | + |
| 283 | + // Act |
| 284 | + jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity); |
| 285 | + var serializedStream = jsonSerializerWriter.GetSerializedContent(); |
| 286 | + using var reader = new StreamReader(serializedStream, Encoding.UTF8); |
| 287 | + var serializedJsonString = reader.ReadToEnd(); |
| 288 | + |
| 289 | + // Assert |
| 290 | + const string expectedString = "{\n \"id\": \"testId\",\n \"href\": \"https://graph.microsoft.com/users/{user-id}\",\n \"unicodeName\": \"你好\"\n}"; |
| 291 | + Assert.Contains("\n", serializedJsonString); // string is indented and not escaped |
| 292 | + Assert.Contains("你好", serializedJsonString); // string is indented and not escaped |
| 293 | + Assert.Equal(expectedString, serializedJsonString.Replace("\r", string.Empty)); // string is indented and not escaped |
| 294 | + } |
| 295 | + |
| 296 | + [Fact] |
| 297 | + public void UsesDefaultOptionsToWriterFromSerializationContext() |
| 298 | + { |
| 299 | + // Arrange |
| 300 | + var testEntity = new TestEntity |
| 301 | + { |
| 302 | + Id = "testId", |
| 303 | + AdditionalData = new Dictionary<string, object>() |
| 304 | + { |
| 305 | + {"href", "https://graph.microsoft.com/users/{user-id}"}, |
| 306 | + {"unicodeName", "你好"} |
| 307 | + } |
| 308 | + }; |
| 309 | + using var jsonSerializerWriter = new JsonSerializationWriter(new KiotaJsonSerializationContext()); |
| 310 | + |
| 311 | + // Act |
| 312 | + jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity); |
| 313 | + var serializedStream = jsonSerializerWriter.GetSerializedContent(); |
| 314 | + using var reader = new StreamReader(serializedStream, Encoding.UTF8); |
| 315 | + var serializedJsonString = reader.ReadToEnd(); |
| 316 | + |
| 317 | + // Assert |
| 318 | + var expectedString = $"{{\"id\":\"testId\",\"href\":\"https://graph.microsoft.com/users/{{user-id}}\",\"unicodeName\":\"\\u4F60\\u597D\"}}"; |
| 319 | + Assert.DoesNotContain("\n", serializedJsonString); // string is not indented and not escaped |
| 320 | + Assert.DoesNotContain("你好", serializedJsonString); // string is not indented and not escaped |
| 321 | + Assert.Contains("\\u4F60\\u597D", serializedJsonString); // string is not indented and not escaped |
| 322 | + Assert.Equal(expectedString, serializedJsonString); // string is indented and not escaped |
| 323 | + } |
261 | 324 | [Fact] |
262 | 325 | public void WriteGuidUsingNoConverter() |
263 | 326 | { |
|
0 commit comments