-
Notifications
You must be signed in to change notification settings - Fork 2k
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 digital twins client not deserializing date times correctly #16975
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.ArrayList; | ||
|
@@ -53,17 +54,18 @@ | |
@ServiceClient(builder = DigitalTwinsClientBuilder.class, isAsync = true) | ||
public final class DigitalTwinsAsyncClient { | ||
private static final ClientLogger logger = new ClientLogger(DigitalTwinsAsyncClient.class); | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
private static ObjectMapper mapper; | ||
private final DigitalTwinsServiceVersion serviceVersion; | ||
private final AzureDigitalTwinsAPIImpl protocolLayer; | ||
private static final Boolean includeModelDefinitionOnGet = true; | ||
private final JsonSerializer serializer; | ||
|
||
DigitalTwinsAsyncClient(String serviceEndpoint, HttpPipeline pipeline, DigitalTwinsServiceVersion serviceVersion, JsonSerializer jsonSerializer) { | ||
final SimpleModule stringModule = new SimpleModule("String Serializer"); | ||
stringModule.addSerializer(new DigitalTwinsStringSerializer(String.class, mapper)); | ||
|
||
JacksonAdapter jacksonAdapter = new JacksonAdapter(); | ||
mapper = jacksonAdapter.serializer(); // Use the same mapper in this layer that the generated layer will use | ||
stringModule.addSerializer(new DigitalTwinsStringSerializer(String.class, mapper)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this string serializer? Are there any APIs that still operate on Strings? I thought we got rid of all those, and everything was objects now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was the plan back when we were going to force APIs to take in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, so now we are back to the original state where we support |
||
jacksonAdapter.serializer().registerModule(stringModule); | ||
|
||
this.serviceVersion = serviceVersion; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,15 @@ | |
import com.azure.identity.ClientSecretCredentialBuilder; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.function.Function; | ||
|
||
public class ComponentSyncSamples { | ||
|
@@ -46,6 +51,10 @@ public static void main(String[] args) throws IOException { | |
.setLogLevel(parsedArguments.getHttpLogDetailLevel())) | ||
.buildClient(); | ||
|
||
// This mapper gets used to deserialize a digital twin that has a date time within a property metadata, so it | ||
// needs to have this module in order to correctly deserialize that date time | ||
mapper.registerModule(new JavaTimeModule()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the module that was missing from our default mapper in the digital twins client. We get this module for free when we construct the JacksonAdapter and call .serializer() on that jacksonAdapter. We need to explicitly use it here, though |
||
|
||
runComponentSample(); | ||
} | ||
|
||
|
@@ -145,6 +154,7 @@ public static void runComponentSample() throws JsonProcessingException { | |
ConsoleLogger.print("Retrieved component for digital twin " + basicDigitalTwinId + " :"); | ||
for (String key : getComponentResponse.getContents().keySet()) { | ||
ConsoleLogger.print("\t" + key + " : " + getComponentResponse.getContents().get(key)); | ||
ConsoleLogger.print("\t\tLast updated on: " + getComponentResponse.getMetadata().get(key).getLastUpdatedOn()); | ||
} | ||
|
||
// Clean up | ||
|
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.
The issue was that we used a different mapper in our convenience layer than we did in our generated layer. The generated layer one (from jacksonAdapter.serializer()) has a lot of useful modules registered already, including a module for parsing of date times. By simply using the generated layer's adapter in this layer as well, this issue is solved
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.
Note that this mapper is not to be confused with the custom serializer that users can provide. If the user provides a custom serializer, then this mapper won't be used in the convenience layer