Skip to content

Commit

Permalink
Use invariant culture for (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinM85 committed Jul 17, 2024
1 parent cc8c813 commit 8421930
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 376 deletions.
5 changes: 2 additions & 3 deletions src/abstractions/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
// ------------------------------------------------------------------------------

using System;
using System.Security;

namespace Microsoft.Kiota.Abstractions
{
/// <summary>
/// Model to represent only the date component of a DateTime
/// </summary>
public struct Time:IEquatable<Time>
public struct Time : IEquatable<Time>
{
#if NET6_0_OR_GREATER
/// <summary>
Expand All @@ -32,7 +31,7 @@ public struct Time:IEquatable<Time>
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
public bool Equals(Time other) => (Hour, Minute, Second) == (other.Hour, other.Minute, other.Second);
public bool Equals(Time other) => Hour == other.Hour && Minute == other.Minute && Second == other.Second;

/// <inheritdoc />
public override bool Equals(object? o) => (o is Time other) && Equals(other);
Expand Down
17 changes: 9 additions & 8 deletions src/serialization/form/FormParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Xml;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Extensions;
Expand Down Expand Up @@ -144,9 +143,9 @@ public IEnumerable<T> GetCollectionOfPrimitiveValues<T>()
}
}
/// <inheritdoc/>
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? result : null;
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? result : null;
/// <inheritdoc/>
public Date? GetDateValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Date(result) : null;
public Date? GetDateValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? new Date(result) : null;
/// <inheritdoc/>
public decimal? GetDecimalValue() => decimal.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
Expand Down Expand Up @@ -223,22 +222,24 @@ private void AssignFieldValues<T>(T item) where T : IParsable
}

/// <inheritdoc/>
public Time? GetTimeValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Time(result) : null;
public Time? GetTimeValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? new Time(result) : null;

/// <inheritdoc/>
#if NET5_0_OR_GREATER
IEnumerable<T?> IParseNode.GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>()
public IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum
#else
IEnumerable<T?> IParseNode.GetCollectionOfEnumValues<T>()
public IEnumerable<T?> GetCollectionOfEnumValues<T>() where T : struct, Enum
#endif
{
foreach(var v in DecodedValue.Split(ComaSeparator, StringSplitOptions.RemoveEmptyEntries))
yield return EnumHelpers.GetEnumValue<T>(v);
}

/// <inheritdoc/>
#if NET5_0_OR_GREATER
T? IParseNode.GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>()
public T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum
#else
T? IParseNode.GetEnumValue<T>()
public T? GetEnumValue<T>() where T : struct, Enum
#endif
{
return EnumHelpers.GetEnumValue<T>(DecodedValue);
Expand Down
8 changes: 4 additions & 4 deletions src/serialization/json/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Xml;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions.Helpers;
using Microsoft.Kiota.Abstractions.Serialization;
using System.Text.Json.Serialization.Metadata;

#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -152,7 +152,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali

if(TryGetUsingTypeInfo(_jsonNode, _jsonSerializerContext.DateTimeOffset, out var dateTimeOffset))
return dateTimeOffset;
else if(DateTimeOffset.TryParse(_jsonNode.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.None, out var dto))
else if(DateTimeOffset.TryParse(_jsonNode.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dto))
return dto;
else return null;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
if(string.IsNullOrEmpty(dateString))
return null;

if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result))
return new Date(result);

return _jsonNode.Deserialize(_jsonSerializerContext.Date);
Expand All @@ -197,7 +197,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
if(string.IsNullOrEmpty(dateString))
return null;

if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result))
return new Time(result);

return _jsonNode.Deserialize(_jsonSerializerContext.Time);
Expand Down
6 changes: 3 additions & 3 deletions src/serialization/text/TextParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public TextParseNode(string? text)
public IEnumerable<T> GetCollectionOfPrimitiveValues<T>() => throw new InvalidOperationException(NoStructuredDataMessage);
#endif
/// <inheritdoc />
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? result : null;
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? result : null;
/// <inheritdoc />
public Date? GetDateValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Date(result) : null;
public Date? GetDateValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? new Date(result) : null;
/// <inheritdoc />
public decimal? GetDecimalValue() => decimal.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
Expand All @@ -70,7 +70,7 @@ public TextParseNode(string? text)
/// <inheritdoc />
public TimeSpan? GetTimeSpanValue() => string.IsNullOrEmpty(Text) ? null : XmlConvert.ToTimeSpan(Text);
/// <inheritdoc />
public Time? GetTimeValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Time(result) : null;
public Time? GetTimeValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var result) ? new Time(result) : null;
/// <inheritdoc />
#if NET5_0_OR_GREATER
public IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum
Expand Down
59 changes: 57 additions & 2 deletions tests/serialization/form/FormParseNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ public void FormParseNode_GetByteArrayValue()

// Act
var result = parseNode.GetByteArrayValue();

// Assert
Assert.Equal(new byte []{ 0x74, 0x65, 0x78, 0x74 }, result);
Assert.Equal([0x74, 0x65, 0x78, 0x74], result);
}

[Fact]
Expand Down Expand Up @@ -343,4 +342,60 @@ public void FormParseNode_GetFloatValue()
// Assert
Assert.Equal(1.1f, result);
}

[Fact]
public void FormParseNode_GetEnumFromInteger()
{
// Arange
var text = "8";
var parseNode = new FormParseNode(text);

// Act
var result = parseNode.GetEnumValue<TestEnum>();

// Assert
Assert.Equal(TestEnum.Eight, result);
}

[Fact]
public void FormParseNode_GetEnumFromString()
{
// Arange
var text = "Sixteen";
var parseNode = new FormParseNode(text);

// Act
var result = parseNode.GetEnumValue<TestEnum>();

// Assert
Assert.Equal(TestEnum.Sixteen, result);
}

[Fact]
public void FormParseNode_GetEnumsFromInteger()
{
// Arange
var text = "8,16";
var parseNode = new FormParseNode(text);

// Act
var result = parseNode.GetCollectionOfEnumValues<TestEnum>();

// Assert
Assert.Equal([TestEnum.Eight, TestEnum.Sixteen], result);
}

[Fact]
public void FormParseNode_GetEnumsFromString()
{
// Arange
var text = "Sixteen,Eight";
var parseNode = new FormParseNode(text);

// Act
var result = parseNode.GetCollectionOfEnumValues<TestEnum>();

// Assert
Assert.Equal([TestEnum.Sixteen, TestEnum.Eight], result);
}
}
2 changes: 1 addition & 1 deletion tests/serialization/text/TextParseNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void TextParseNode_GetByteValue()
var parseNode = new TextParseNode(text);

var result = parseNode.GetByteValue();

Assert.Equal((byte)1, result);
}

Expand Down
Loading

0 comments on commit 8421930

Please sign in to comment.