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

Fix twin desired property payload conversion - MQTT #3322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iothub/device/src/Pipeline/MqttTransportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ private void HandleReceivedDesiredPropertiesUpdateRequest(MqttApplicationMessage
// This message is always QoS 0, so no ack will be sent.
receivedEventArgs.AutoAcknowledge = true;

Dictionary<string, object> desiredPropertyPatchDictionary = _payloadConvention.GetObject<Dictionary<string, object>>(receivedEventArgs.ApplicationMessage.Payload);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was _payloadConvention null here or something? Why do we want to always use the default payload convention?

Copy link
Contributor Author

@tmahmood-microsoft tmahmood-microsoft May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the payload from service client is always converted using default Payload/Newtonsoft.Json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good then. Leave a comment to that effect here for future reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to adding a comment in here explaining why default convention is used. You could reference the fact that we deserialize this into a dictionary based on service defined convention of it being a utf-8 encoded json string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the payload from service client is always converted using default Payload/Newtonsoft.Json

Not quite. However it was sent from the service client, it is just JSON.

The problem is that our SDK deserializes the twin using Newtonsoft.Json. Then, individual properties can be deserialized to the user's choice, but not until after we've converted them back to bytes using the default payload convention again.

Dictionary<string, object> desiredPropertyPatchDictionary = DefaultPayloadConvention.Instance.GetObject<Dictionary<string, object>>(receivedEventArgs.ApplicationMessage.Payload);
var desiredPropertyPatch = new DesiredProperties(desiredPropertyPatchDictionary)
{
PayloadConvention = _payloadConvention,
Expand Down
2 changes: 1 addition & 1 deletion iothub/device/tests/DeviceClientTwinApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void IotHubDeviceClient_Verify_GetTwinResponse()
};
reported["$version"] = "1";
reported.Add("key", "value");
var desired = new DesiredProperties(new Dictionary<string, object>() { { "$version", "1" } });
var desired = new DesiredProperties(new Dictionary<string, object>() { { "$version", "1" }, { "id", "1234" } });
GetTwinResponse twinResponse = new GetTwinResponse
{
Status = 404,
Expand Down