Skip to content

Commit

Permalink
Merge pull request #468 from microsoft/primitivesFix
Browse files Browse the repository at this point in the history
fix: serialization of primitives present in additionalData
  • Loading branch information
andrueastman authored Nov 13, 2024
2 parents 49d3961 + 83c61e8 commit 459e604
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- Common default project properties for ALL projects-->
<PropertyGroup>
<VersionPrefix>1.15.0</VersionPrefix>
<VersionPrefix>1.15.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- This is overidden in test projects by setting to true-->
<IsTestProject>false</IsTestProject>
Expand Down
12 changes: 7 additions & 5 deletions src/serialization/json/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
@@ -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.
// ------------------------------------------------------------------------------

Expand Down Expand Up @@ -311,7 +311,9 @@ public void WriteEnumValue<T>(string? key, T? value) where T : struct, Enum
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="values">The primitive collection</param>
public void WriteCollectionOfPrimitiveValues<T>(string? key, IEnumerable<T>? values)
public void WriteCollectionOfPrimitiveValues<T>(string? key, IEnumerable<T>? values) => WriteCollectionOfPrimitiveValuesInternal(key, values);

private void WriteCollectionOfPrimitiveValuesInternal(string? key, IEnumerable? values)
{
if(values != null)
{ //empty array is meaningful
Expand Down Expand Up @@ -525,9 +527,6 @@ private void WriteAnyValue<T>(string? key, T value)
case TimeSpan timeSpan:
WriteTimeSpanValue(key, timeSpan);
break;
case IEnumerable<object> coll:
WriteCollectionOfPrimitiveValues(key, coll);
break;
case UntypedNode node:
WriteUntypedValue(key, node);
break;
Expand All @@ -551,6 +550,9 @@ private void WriteAnyValue<T>(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;
Expand Down
34 changes: 33 additions & 1 deletion tests/serialization/json/JsonSerializationWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -40,6 +40,7 @@ public void WritesSampleObjectValue()
{"createdDateTime", DateTimeOffset.MinValue}, // write date value
{"weightInKgs", 51.80m}, // write weigth
{"businessPhones", new List<string>() {"+1 412 555 0109"}}, // write collection of primitives value
{"dates",new List<DateTimeOffset> { 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<string>{ "Value3.1", "Value3.2"}}}, // write nested object value
Expand Down Expand Up @@ -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\"]}," +
Expand Down Expand Up @@ -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>
{
DateTimeOffset.MaxValue, DateTimeOffset.MinValue
};
var testEntity = new TestEntity
{
Id = "testId",
AdditionalData = new Dictionary<string, object>()
{
{"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()
{
Expand Down

0 comments on commit 459e604

Please sign in to comment.