-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
ADT: Fix the issue with non-default object serializers in Query APIs #18379
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e50973
Fix the bug with non-default ObjectSerializer in QueryClient
azabbasi 3b8e487
Add tests
azabbasi 16b2bad
Address PR comments
azabbasi 6971efd
Format files
azabbasi 8dbb0e3
Update
azabbasi a60ff57
Update snippets and test code.
azabbasi 34386c0
Update DigitalTwinsClient.cs
azabbasi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
sdk/digitaltwins/Azure.DigitalTwins.Core/tests/NewtonsoftObjectSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Azure.Core.Serialization; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.DigitalTwins.Core.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for custom ObjectSerializer. | ||
azabbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Users can specify their own serializer/deserializer and not go with the default JsonObjectSerializer. | ||
/// SDK needs to make sure it can properly use different serializers and the behavior is seamless. | ||
/// Specifically, we have a work around in the query code which requires use of a System.Text.Json serializer because it works on a JsonElement. | ||
/// When the user initializes with a non-default serializer, we must not use that to do this work. | ||
/// </summary> | ||
public class NewtonsoftObjectSerializerTests : E2eTestBase | ||
{ | ||
public NewtonsoftObjectSerializerTests(bool isAsync) | ||
: base(isAsync) | ||
{ | ||
} | ||
|
||
[Test] | ||
public async Task TestNewtonsoftObjectSerializerWithDigitalTwins() | ||
{ | ||
DigitalTwinsClient defaultClient = GetClient(); | ||
|
||
string roomTwinId = await GetUniqueTwinIdAsync(defaultClient, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); | ||
string floorModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); | ||
string roomModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); | ||
|
||
try | ||
{ | ||
// arrange | ||
|
||
// create room model | ||
string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); | ||
await CreateAndListModelsAsync(defaultClient, new List<string> { roomModel }).ConfigureAwait(false); | ||
|
||
// act | ||
|
||
// create room twin | ||
BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); | ||
await defaultClient.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); | ||
|
||
// Create a client with NewtonsoftJsonObjectSerializer configured as the serializer. | ||
DigitalTwinsClient testClient = GetClient( | ||
new DigitalTwinsClientOptions | ||
{ | ||
Serializer = new NewtonsoftJsonObjectSerializer() | ||
}); | ||
|
||
// Get digital twin using the simple DigitalTwin model annotated with Newtonsoft attributes | ||
SimpleNewtonsoftDtModel getResponse = await testClient.GetDigitalTwinAsync<SimpleNewtonsoftDtModel>(roomTwinId).ConfigureAwait(false); | ||
|
||
getResponse.Id.Should().NotBeNullOrEmpty("Digital twin ID should not be null or empty"); | ||
|
||
// Query DigitalTwins using the simple DigitalTwin model annotated with Newtonsoft attributes | ||
AsyncPageable<SimpleNewtonsoftDtModel> queryResponse = testClient.QueryAsync<SimpleNewtonsoftDtModel>("SELECT * FROM DIGITALTWINS"); | ||
|
||
await foreach (SimpleNewtonsoftDtModel twin in queryResponse) | ||
{ | ||
twin.Id.Should().NotBeNullOrEmpty("Digital twin Id should not be null or empty"); | ||
} | ||
} | ||
finally | ||
{ | ||
// cleanup | ||
try | ||
{ | ||
// delete twin | ||
if (!string.IsNullOrWhiteSpace(roomTwinId)) | ||
{ | ||
await defaultClient.DeleteDigitalTwinAsync(roomTwinId).ConfigureAwait(false); | ||
} | ||
|
||
// delete models | ||
if (!string.IsNullOrWhiteSpace(roomModelId)) | ||
{ | ||
await defaultClient.DeleteModelAsync(roomModelId).ConfigureAwait(false); | ||
drwill-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Test clean up failed: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pakrym if we could use a JsonElement or a string instead of a stream to feed into the object serializer, we wouldn't have to go through these hoops. Any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would still have to turn the JsonElement into the string/stream before feeding to Newtonsoft.Json Parse.
We are adding a BinaryData overload in this release though.