diff --git a/CHANGELOG.md b/CHANGELOG.md index 754e90a..bf5fd68 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.15.1] - 2024-11-13 + +### Added + +- Fixes serialization collections of primitives present in additional data. [microsoftgraph/msgraph-sdk-dotnet#2729](https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2729) + ## [1.15.0] - 2024-11-13 ### Added diff --git a/Directory.Build.props b/Directory.Build.props index c3fc775..66c849b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.15.0 + 1.15.1 false diff --git a/src/serialization/json/JsonSerializationWriter.cs b/src/serialization/json/JsonSerializationWriter.cs index d10c72b..af50031 100644 --- a/src/serialization/json/JsonSerializationWriter.cs +++ b/src/serialization/json/JsonSerializationWriter.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ @@ -311,7 +311,9 @@ public void WriteEnumValue(string? key, T? value) where T : struct, Enum /// /// The key of the json node /// The primitive collection - public void WriteCollectionOfPrimitiveValues(string? key, IEnumerable? values) + public void WriteCollectionOfPrimitiveValues(string? key, IEnumerable? values) => WriteCollectionOfPrimitiveValuesInternal(key, values); + + private void WriteCollectionOfPrimitiveValuesInternal(string? key, IEnumerable? values) { if(values != null) { //empty array is meaningful @@ -525,9 +527,6 @@ private void WriteAnyValue(string? key, T value) case TimeSpan timeSpan: WriteTimeSpanValue(key, timeSpan); break; - case IEnumerable coll: - WriteCollectionOfPrimitiveValues(key, coll); - break; case UntypedNode node: WriteUntypedValue(key, node); break; @@ -551,6 +550,9 @@ private void WriteAnyValue(string? key, T value) case IDictionary dictionary: WriteDictionaryValue(key, dictionary); break; + case IEnumerable coll: + WriteCollectionOfPrimitiveValuesInternal(key, coll); + break; case object o: WriteNonParsableObjectValue(key, o); break; diff --git a/tests/serialization/json/JsonSerializationWriterTests.cs b/tests/serialization/json/JsonSerializationWriterTests.cs index 5b7e365..c1f212a 100644 --- a/tests/serialization/json/JsonSerializationWriterTests.cs +++ b/tests/serialization/json/JsonSerializationWriterTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -40,6 +40,7 @@ public void WritesSampleObjectValue() {"createdDateTime", DateTimeOffset.MinValue}, // write date value {"weightInKgs", 51.80m}, // write weigth {"businessPhones", new List() {"+1 412 555 0109"}}, // write collection of primitives value + {"dates",new List { DateTimeOffset.MaxValue , DateTimeOffset.MinValue }}, {"endDateTime", new DateTime(2023,03,14,0,0,0,DateTimeKind.Utc) }, // ensure the DateTime doesn't crash {"manager", new TestEntity{Id = "48d31887-5fad-4d73-a9f5-3c356e68a038"}}, // write nested object value {"anonymousObject", new {Value1 = true, Value2 = "", Value3 = new List{ "Value3.1", "Value3.2"}}}, // write nested object value @@ -69,6 +70,7 @@ public void WritesSampleObjectValue() "\"createdDateTime\":\"0001-01-01T00:00:00+00:00\"," + "\"weightInKgs\":51.80," + "\"businessPhones\":[\"\\u002B1 412 555 0109\"]," + + "\"dates\":[\"9999-12-31T23:59:59.9999999+00:00\",\"0001-01-01T00:00:00+00:00\"]," + "\"endDateTime\":\"2023-03-14T00:00:00+00:00\"," + "\"manager\":{\"id\":\"48d31887-5fad-4d73-a9f5-3c356e68a038\"}," + "\"anonymousObject\":{\"Value1\":true,\"Value2\":\"\",\"Value3\":[\"Value3.1\",\"Value3.2\"]}," + @@ -292,6 +294,36 @@ public void ForwardsOptionsToWriterFromSerializationContext() Assert.Equal(expectedString, serializedJsonString.Replace("\r", string.Empty)); // string is indented and not escaped } + [Fact] + public void WritesPrimitiveCollectionsInAdditionalData() + { + // Arrange + var dates = new List + { + DateTimeOffset.MaxValue, DateTimeOffset.MinValue + }; + var testEntity = new TestEntity + { + Id = "testId", + AdditionalData = new Dictionary() + { + {"dates", dates} + } + }; + using var jsonSerializerWriter = new JsonSerializationWriter(); + // Act + jsonSerializerWriter.WriteObjectValue(string.Empty, testEntity); + var serializedStream = jsonSerializerWriter.GetSerializedContent(); + using var reader = new StreamReader(serializedStream, Encoding.UTF8); + var serializedJsonString = reader.ReadToEnd(); + + // Assert + Assert.Contains("\"id\":\"testId\"", serializedJsonString); + Assert.Contains("\"dates\":[\"", serializedJsonString); + Assert.Contains(JsonSerializer.Serialize(DateTimeOffset.MinValue), serializedJsonString); + Assert.Contains(JsonSerializer.Serialize(DateTimeOffset.MaxValue), serializedJsonString); + } + [Fact] public void UsesDefaultOptionsToWriterFromSerializationContext() {