-
Notifications
You must be signed in to change notification settings - Fork 181
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
Treat datetime values as Strings during json deserialization in Json… #562
Changes from 5 commits
6d692cc
104b068
e7b8332
3fe3915
f2bad8a
9a4205e
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 |
---|---|---|
|
@@ -4,15 +4,34 @@ | |
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using EnsureThat; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Extensions; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Models; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Health.Fhir.Liquid.Converter.Parsers | ||
{ | ||
public class JsonDataParser : IDataParser | ||
{ | ||
private static readonly JsonSerializerSettings DefaultSerializerSettings = new JsonSerializerSettings() | ||
{ | ||
DateParseHandling = DateParseHandling.None, | ||
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. @dustinburson @pallar-ms This is technically a breaking change for anyone using the OSS code. But I'd like to address it here as its a bug. A couple of ideas:
Thoughts? 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. Have you looked at how this is defined/used in the FHIR service. I am concerned this won't allow us to change the behavior per request at the FHIR service level. I am not concerned about a breaking change in the OSS. We can do the necessary version update as you said and add documentation if necessary. 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. An alternative is to just create a StrictJsonDataParser & StrictJsonProcessor. This would preserve backwards compat but allow us to define which parser we use in the API calls. 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. The Fhir Service pulls out the correct IFhirConveter at request time from a map. After this change the map will hold the 'fixed' datatime logic. The plan is to create a new JsonParser with 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. Is the plan to use an API version of the request or another request body input to decide which parser to use? 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. My current idea is to create a new request body input called The map idea is interesting but I can see it getting a bit messy as we would need to add in another dimension to the tuple key, as you've said. I think it would be easier with a conditional:
We could also capture this logic inside of a factory to keep it a bit cleaner. 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. Sure, that would work and I agree keeping it in the factory would be better. Might need to adjust this accordingly - https://github.com/microsoft/FHIR-Converter/blob/main/src/Microsoft.Health.Fhir.Liquid.Converter/Processors/ConvertProcessorFactory.cs The only reason I proposed the map update was to keep the pattern of choosing the processor consistent from the map. Otherwise on first glance it looks odd why for one request param 'JsonFhirConversionDatesAsStrings' we pick the processor differently and then for another request param 'InputDataType' we use the map. But not strongly advocating it either since yeah adding to the key tuple is not too extendable and neat if we have another field later. 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. Curious if we can supply other configurable settings in JsonTextReader()? 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. I was looking into Unsure about any perf impacts. Is there a test/perf harness available where I can try and get numbers? 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. I set up some unit tests to quickly evaluate perf. For each deserialization method, I performed 5 runs and captured the overall time. Each run performed 1 million deserializations. The results are below. There doesn't appear to be much difference between the two approaches, so I'll go with JTokenParseTestAsync
00:00:07.7852149
00:00:06.5830398
00:00:06.9395215
00:00:05.9901044
00:00:06.0748714
JsonConvertTestAsync
00:00:06.4465205
00:00:06.6376552
00:00:06.4552853
00:00:06.4866381
00:00:06.3361394 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. Great, thanks for evaluating the perf impact. |
||
}; | ||
|
||
public JsonDataParser() | ||
: this(DefaultSerializerSettings) | ||
{ | ||
} | ||
|
||
public JsonDataParser(JsonSerializerSettings jsonSerializerSettings) | ||
{ | ||
JsonSerializerSettings = EnsureArg.IsNotNull(jsonSerializerSettings, nameof(jsonSerializerSettings)); | ||
} | ||
|
||
protected JsonSerializerSettings JsonSerializerSettings { get; private set; } | ||
|
||
public object Parse(string json) | ||
{ | ||
if (string.IsNullOrWhiteSpace(json)) | ||
|
@@ -22,7 +41,7 @@ public object Parse(string json) | |
|
||
try | ||
{ | ||
return JToken.Parse(json).ToObject(); | ||
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. Does JObject.Parse(json) also have the same behaviour? If so, I do see other places where JObject.Parse is used and would be good to check if a similar setting needs to be applied there too depending on the input being parsed in those cases. 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. I'd suspect that all JXXX.Parse methods have the same behavior. There seem to be some default settings in Newtonsoft that are undesirable for our needs. Agree that we should go through and address this if needed across the project. I suggest doing that work in a separate PR to reduce the scope of this one, which is to address a known customer issue. We may also want to use this opportunity to move away from Newtonsoft and onto .Net's implementation, which would be a bigger effort. 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. Makes sense. But just to point out, in the thread you forwarded, Dustin mentioned that this problem exists even in the post processing logic which also impacts the customer's issue being addressed. 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. I think there is a potential issue with 'pre/post' processing the result in ADF, and not within Convert/FHIR Server. Looking at the internal PostProcessor of Json it looks like DateParsing is already disabled. I've tested out using Convert/Fhir Server directly and verified that results can come back with the original date format preserved. |
||
return JsonConvert.DeserializeObject<JToken>(json, JsonSerializerSettings).ToObject(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using System.Text; | ||
using System.Threading; | ||
using DotLiquid; | ||
using EnsureThat; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Extensions; | ||
|
@@ -18,20 +19,32 @@ | |
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json; | ||
using Microsoft.Health.Fhir.Liquid.Converter.Parsers; | ||
using Microsoft.Health.MeasurementUtility; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using NJsonSchema; | ||
|
||
namespace Microsoft.Health.Fhir.Liquid.Converter.Processors | ||
{ | ||
public class JsonToHl7v2Processor : BaseProcessor | ||
{ | ||
private readonly IDataParser _parser = new JsonDataParser(); | ||
private static readonly JsonSerializerSettings DefaultSerializerSettings = new JsonSerializerSettings() | ||
{ | ||
DateParseHandling = DateParseHandling.None, | ||
}; | ||
|
||
private readonly IDataParser _parser; | ||
|
||
private string[] _segmentsWithFieldSeparator = new string[] { "MSH", "BHS", "FHS" }; | ||
|
||
public JsonToHl7v2Processor(ProcessorSettings processorSettings, ILogger<JsonToHl7v2Processor> logger) | ||
: this(processorSettings, new JsonDataParser(), logger) | ||
{ | ||
} | ||
|
||
public JsonToHl7v2Processor(ProcessorSettings processorSettings, IDataParser parser, ILogger<JsonToHl7v2Processor> logger) | ||
: base(processorSettings, logger) | ||
{ | ||
_parser = EnsureArg.IsNotNull(parser, nameof(parser)); | ||
} | ||
|
||
protected override string InternalConvert(string data, string rootTemplate, ITemplateProvider templateProvider, TraceInfo traceInfo = null) | ||
|
@@ -45,7 +58,7 @@ protected override string InternalConvert(string data, string rootTemplate, ITem | |
|
||
var result = InternalConvertFromObject(jsonData, rootTemplate, templateProvider, traceInfo); | ||
|
||
var hl7Message = GenerateHL7Message(JObject.Parse(result)); | ||
var hl7Message = GenerateHL7Message(ConvertToJObject(result)); | ||
|
||
var hl7String = ConvertHl7MessageToString(hl7Message); | ||
|
||
|
@@ -56,7 +69,7 @@ public string Convert(JObject data, string rootTemplate, ITemplateProvider templ | |
{ | ||
var jsonData = data.ToObject(); | ||
var result = InternalConvertFromObject(jsonData, rootTemplate, templateProvider, traceInfo); | ||
var hl7Message = GenerateHL7Message(JObject.Parse(result)); | ||
var hl7Message = GenerateHL7Message(ConvertToJObject(result)); | ||
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. @pallar-ms @dustinburson This class also uses the updated We can also add this in a backwards compatible way in the Fhir-Server As @pallar-ms mentioned there are a few other places where 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. FYI, the JsonToHL7v2Processor is only used in the new convert preview APIs and is not supported in the FHIR server $convert. 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. Great! That makes things easier |
||
|
||
var hl7String = ConvertHl7MessageToString(hl7Message); | ||
return hl7String; | ||
|
@@ -239,5 +252,10 @@ protected override void CreateTraceInfo(object data, Context context, TraceInfo | |
jsonTraceInfo.ValidateSchemas = jsonContext.ValidateSchemas; | ||
} | ||
} | ||
|
||
private JObject ConvertToJObject(string input) | ||
{ | ||
return JsonConvert.DeserializeObject<JObject>(input, DefaultSerializerSettings); | ||
} | ||
} | ||
} |
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.
nit suggestion: Probably doesn't matter for this test, but just for consistency and in case a search all is done to get all references, maybe changing this to also do DeserializeObject() with the serializer settings would help.
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.
Sure, will do
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.
Done