Skip to content

Commit

Permalink
Fix deserialization bug (#46666)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhz0704 authored Dec 3, 2024
1 parent 31289c4 commit b1b1e2e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion sdk/maps/Azure.Maps.Routing/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "net",
"TagPrefix": "net/maps/Azure.Maps.Routing",
"Tag": "net/maps/Azure.Maps.Routing_54064ae0c1"
"Tag": "net/maps/Azure.Maps.Routing_0755b0a67d"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ private async ValueTask<Response<RouteMatrixResult>> WaitForCompletionAsync(bool
_id = paths[paths.Length - 1];
}

RouteMatrixResult result = null;
return Response.FromValue(result, response);
return await WaitForCompletionAsync(async, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System.Text.Json;

namespace Azure.Maps.Routing.Models
{
public partial class RouteMatrix
{
internal static RouteMatrix DeserializeRouteMatrix(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
int? statusCode = default;
RouteMatrixResultResponse response = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("statusCode"u8))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
statusCode = property.Value.GetInt32();
continue;
}
if (property.NameEquals("response"u8))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
if (statusCode == 200) {
response = RouteMatrixResultResponse.DeserializeRouteMatrixResultResponse(property.Value);
} else {
response = new RouteMatrixResultResponse(null);
}
continue;
}
}
return new RouteMatrix(statusCode, response);
}

/// <summary> Deserializes the model from a raw response. </summary>
/// <param name="response"> The response to deserialize the model from. </param>
internal static RouteMatrix FromResponse(Response response)
{
using var document = JsonDocument.Parse(response.Content);
return DeserializeRouteMatrix(document.RootElement);
}
}
}
11 changes: 8 additions & 3 deletions sdk/maps/Azure.Maps.Routing/tests/RouteMatrixTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -27,15 +28,19 @@ public async Task CanSyncRequestRouteMatrix()
new GeoPosition(123.751, 45.9375),
new GeoPosition(123.791, 45.96875)
},
Destinations = new List<GeoPosition>() { new GeoPosition(123.767, 45.90625) },
Destinations = new List<GeoPosition>()
{
new GeoPosition(1, 13.5471),
new GeoPosition(123.767, 45.90625),
},
};
var result = await client.GetImmediateRouteMatrixAsync(routeMatrixQuery);

Assert.AreEqual("0.0.1", result.Value.FormatVersion);
Assert.AreEqual(2, result.Value.Matrix.Count);
Assert.AreEqual(1, result.Value.Matrix[0].Count);
Assert.AreEqual(2, result.Value.Matrix[0].Count);
Assert.AreEqual(2, result.Value.Summary.SuccessfulRoutes);
Assert.AreEqual(2, result.Value.Summary.TotalRoutes);
Assert.AreEqual(4, result.Value.Summary.TotalRoutes);
}

[RecordedTest]
Expand Down

0 comments on commit b1b1e2e

Please sign in to comment.