Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISO 8601 date strings are modified in cloud to device method results #3093

Closed
pihai opened this issue Jan 31, 2023 · 4 comments
Closed

ISO 8601 date strings are modified in cloud to device method results #3093

pihai opened this issue Jan 31, 2023 · 4 comments
Assignees
Labels
bug Something isn't working. fix-checked-in Fix checked into main or preview, but not yet released. IoTSDK Tracks all IoT SDK issues across the board

Comments

@pihai
Copy link

pihai commented Jan 31, 2023

Context

  • SDK version used: Microsoft.Azure.Devices 1.38.2

Description of the issue

If a DateTimeOffest formatted as ISO 8601 string is used in a CloudToDeviceMethodResult then the SDK messes with the date, like removing trailing zeros.

Code sample exhibiting the issue

using System.Globalization;
using System.Text;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;

const string serviceConnectionString = "<TODO>";
const string deviceConnectionString = "<TODO>";

var deviceId = Microsoft.Azure.Devices.Client.IotHubConnectionStringBuilder.Create(deviceConnectionString).DeviceId;
const string methodName = "test";
var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, Microsoft.Azure.Devices.Client.TransportType.Amqp_WebSocket_Only);

var date = new DateTimeOffset(638107582284599400, TimeSpan.FromHours(1));
var responseJson = JsonConvert.SerializeObject(new MyDto { MyIso8601Date = date.ToString("o", CultureInfo.InvariantCulture) });
var responseBytes = Encoding.UTF8.GetBytes(responseJson);

await deviceClient.SetMethodHandlerAsync(
    methodName, 
    async (request, context) => new MethodResponse(responseBytes, 0),
    null);

var serviceClient = ServiceClient.CreateFromConnectionString(serviceConnectionString, Microsoft.Azure.Devices.TransportType.Amqp_WebSocket_Only);

var result = await serviceClient.InvokeDeviceMethodAsync(deviceId, new CloudToDeviceMethod(methodName).SetPayloadJson("{}"));

var actualResponse = result.GetPayloadAsJson();

Console.WriteLine(responseJson);
Console.WriteLine(actualResponse);

public class MyDto
{
    public string MyIso8601Date { get; set; }
}

Outputs:

{"MyIso8601Date":"2023-01-31T10:37:08.4599400+01:00"}
{"MyIso8601Date":"2023-01-31T10:37:08.45994+01:00"}

Notice that the trailing zeros are gone. This is problematic when the string is converted back into a DateTimeOffset using the same format identifier because a FormatException is thrown:

DateTimeOffset.ParseExact("2023-01-31T10:37:08.45994+01:00", "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

I think that the problem originates from here, where the response gets deserialized with Newtonsoft.Json:

T entity = JsonConvert.DeserializeObject<T>(str);

It is a known and unfortunate behavior of Newtonsoft.Json: JamesNK/Newtonsoft.Json#862

In my opinion the SDK should not alter the strings returned from the device methods.

@pihai pihai added the bug Something isn't working. label Jan 31, 2023
@github-actions github-actions bot added the IoTSDK Tracks all IoT SDK issues across the board label Jan 31, 2023
@brycewang-microsoft brycewang-microsoft self-assigned this Feb 1, 2023
@brycewang-microsoft
Copy link
Contributor

Hi @pihai, thanks for bringing this to our attention! The fix has been merged in, and I will update this thread again once we ship the release.

@brycewang-microsoft brycewang-microsoft added the fix-checked-in Fix checked into main or preview, but not yet released. label Feb 22, 2023
@drwill-ms
Copy link
Contributor

@drwill-ms drwill-ms reopened this Mar 13, 2023
@drwill-ms
Copy link
Contributor

My mistake, this was only shipped in v2 preview so far.

@brycewang-microsoft
Copy link
Contributor

Hi @pihai, the fix has been shipped in Microsoft.Azure.Devices 1.39.0 with the latest release. I am closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. fix-checked-in Fix checked into main or preview, but not yet released. IoTSDK Tracks all IoT SDK issues across the board
Projects
None yet
Development

No branches or pull requests

3 participants