Skip to content

[Do not review] Experimental sourcenode parser #5013

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

brendankowitz
Copy link
Member

@brendankowitz brendankowitz commented Jun 2, 2025

Description

Very experimental fhir parser.

image

Related issues

Addresses [issue #].

Testing

Describe how this change was tested.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • When changing or adding behavior, if your code modifies the system design or changes design assumptions, please create and include an ADR.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch|Skip|Feature|Breaking (reason)

{
if (resource is ResourceJsonNode resourceJsonNode)
{
Name = name ?? resourceJsonNode.ResourceType;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.

Copilot Autofix

AI 10 days ago

To fix the issue, we should avoid accessing the virtual property Name in the constructor. Instead, we can introduce a private field to store the value of Name during construction and use this field to implement the Name property. This ensures that the constructor does not rely on virtual calls, eliminating the risk of unexpected behavior.


Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
@@ -25,3 +25,3 @@
         {
-            Name = name ?? resourceJsonNode.ResourceType;
+            _name = name ?? resourceJsonNode.ResourceType;
             ResourceType = resourceJsonNode.ResourceType;
@@ -31,3 +31,3 @@
         {
-            Name = name;
+            _name = name;
             Location = location;
@@ -109,3 +109,4 @@
 
-    public override string Name { get; }
+    private readonly string _name;
+    public override string Name => _name;
 
EOF
@@ -25,3 +25,3 @@
{
Name = name ?? resourceJsonNode.ResourceType;
_name = name ?? resourceJsonNode.ResourceType;
ResourceType = resourceJsonNode.ResourceType;
@@ -31,3 +31,3 @@
{
Name = name;
_name = name;
Location = location;
@@ -109,3 +109,4 @@

public override string Name { get; }
private readonly string _name;
public override string Name => _name;

Copilot is powered by AI and may make mistakes. Always verify output.
if (resource is ResourceJsonNode resourceJsonNode)
{
Name = name ?? resourceJsonNode.ResourceType;
ResourceType = resourceJsonNode.ResourceType;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.

Copilot Autofix

AI 10 days ago

To fix the issue, we should avoid making a virtual call in the constructor. Instead of directly accessing the ResourceType property, we can use a private field to store the value during construction. This ensures that the constructor does not rely on virtual calls, eliminating the risk of unexpected behavior.

Steps to fix:

  1. Introduce a private readonly field _resourceType to store the value of ResourceType during construction.
  2. Modify the ResourceType property to return the value of _resourceType.
  3. Update the constructor to assign the value to _resourceType instead of directly accessing the ResourceType property.

Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
@@ -26,3 +26,3 @@
             Name = name ?? resourceJsonNode.ResourceType;
-            ResourceType = resourceJsonNode.ResourceType;
+            _resourceType = resourceJsonNode.ResourceType;
             Location = location ?? resourceJsonNode.ResourceType;
@@ -115,3 +115,4 @@
 
-    public override string ResourceType { get; }
+    private readonly string _resourceType;
+    public override string ResourceType => _resourceType;
 
EOF
@@ -26,3 +26,3 @@
Name = name ?? resourceJsonNode.ResourceType;
ResourceType = resourceJsonNode.ResourceType;
_resourceType = resourceJsonNode.ResourceType;
Location = location ?? resourceJsonNode.ResourceType;
@@ -115,3 +115,4 @@

public override string ResourceType { get; }
private readonly string _resourceType;
public override string ResourceType => _resourceType;

Copilot is powered by AI and may make mistakes. Always verify output.
{
Name = name ?? resourceJsonNode.ResourceType;
ResourceType = resourceJsonNode.ResourceType;
Location = location ?? resourceJsonNode.ResourceType;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.

Copilot Autofix

AI 10 days ago

To fix the issue, we should avoid directly assigning the virtual property Location in the constructor. Instead, we can use a private field to store the value and expose it through the virtual property. This ensures that the virtual property is not accessed during the base class constructor execution, preventing any unintended behavior.

Steps to implement the fix:

  1. Introduce a private field _location to store the value of Location.
  2. Modify the Location property to return the value of _location.
  3. Update the constructor to assign the value to _location instead of directly assigning to Location.

Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
@@ -23,2 +23,3 @@
     {
+        private readonly string _location;
         if (resource is ResourceJsonNode resourceJsonNode)
@@ -27,3 +28,3 @@
             ResourceType = resourceJsonNode.ResourceType;
-            Location = location ?? resourceJsonNode.ResourceType;
+            _location = location ?? resourceJsonNode.ResourceType;
         }
@@ -113,3 +114,3 @@
 
-    public override string Location { get; }
+    public override string Location => _location;
 
EOF
@@ -23,2 +23,3 @@
{
private readonly string _location;
if (resource is ResourceJsonNode resourceJsonNode)
@@ -27,3 +28,3 @@
ResourceType = resourceJsonNode.ResourceType;
Location = location ?? resourceJsonNode.ResourceType;
_location = location ?? resourceJsonNode.ResourceType;
}
@@ -113,3 +114,3 @@

public override string Location { get; }
public override string Location => _location;

Copilot is powered by AI and may make mistakes. Always verify output.
}
else
{
Name = name;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.

Copilot Autofix

AI 10 days ago

To fix the issue, we should avoid assigning a value to the virtual property Name directly in the constructor. Instead, we can introduce a private backing field for Name and use it internally within the constructor. The Name property can then return the value of this backing field. This ensures that no virtual calls are made during the constructor's execution.


Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
@@ -25,3 +25,3 @@
         {
-            Name = name ?? resourceJsonNode.ResourceType;
+            _name = name ?? resourceJsonNode.ResourceType;
             ResourceType = resourceJsonNode.ResourceType;
@@ -31,3 +31,3 @@
         {
-            Name = name;
+            _name = name;
             Location = location;
@@ -109,3 +109,4 @@
 
-    public override string Name { get; }
+    private readonly string _name;
+    public override string Name => _name;
 
EOF
@@ -25,3 +25,3 @@
{
Name = name ?? resourceJsonNode.ResourceType;
_name = name ?? resourceJsonNode.ResourceType;
ResourceType = resourceJsonNode.ResourceType;
@@ -31,3 +31,3 @@
{
Name = name;
_name = name;
Location = location;
@@ -109,3 +109,4 @@

public override string Name { get; }
private readonly string _name;
public override string Name => _name;

Copilot is powered by AI and may make mistakes. Always verify output.
else
{
Name = name;
Location = location;

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor Warning

Avoid virtual calls in a constructor or destructor.

Copilot Autofix

AI 10 days ago

To fix the issue, avoid accessing the virtual property Location directly in the constructor. Instead, use a private or protected field to store the value during construction, and then implement the Location property to return this field. This ensures that the constructor does not invoke any overridden implementations of the property.

Steps to fix:

  1. Introduce a private or protected field _location to store the value of Location.
  2. Update the constructor to assign the value to _location instead of directly to Location.
  3. Modify the Location property to return the value of _location.

Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/ReflectedSourceNode.cs
@@ -18,2 +18,3 @@
 {
+    private readonly string _location;
     private readonly Lazy<List<(string Name, Lazy<IEnumerable<ISourceNode>> Node)>> _propertySourceNodes;
@@ -27,3 +28,3 @@
             ResourceType = resourceJsonNode.ResourceType;
-            Location = location ?? resourceJsonNode.ResourceType;
+            _location = location ?? resourceJsonNode.ResourceType;
         }
@@ -32,3 +33,3 @@
             Name = name;
-            Location = location;
+            _location = location;
         }
@@ -113,3 +114,3 @@
 
-    public override string Location { get; }
+    public override string Location => _location;
 
EOF
@@ -18,2 +18,3 @@
{
private readonly string _location;
private readonly Lazy<List<(string Name, Lazy<IEnumerable<ISourceNode>> Node)>> _propertySourceNodes;
@@ -27,3 +28,3 @@
ResourceType = resourceJsonNode.ResourceType;
Location = location ?? resourceJsonNode.ResourceType;
_location = location ?? resourceJsonNode.ResourceType;
}
@@ -32,3 +33,3 @@
Name = name;
Location = location;
_location = location;
}
@@ -113,3 +114,3 @@

public override string Location { get; }
public override string Location => _location;

Copilot is powered by AI and may make mistakes. Always verify output.
@brendankowitz brendankowitz force-pushed the personal/bkowitz/sourcenode-parser branch 3 times, most recently from 510f4ef to 0aa243f Compare June 3, 2025 16:00
resource.Meta.LastUpdated = new DateTimeOffset(lastUpdated.DateTime.TruncateToMillisecond(), lastUpdated.Offset);
if (!lastUpdatedIsNull && resource.Meta.LastUpdated.Value > Clock.UtcNow.AddSeconds(10)) // 5 sec is the max for the computers in the domain
var lastUpdated = lastUpdatedIsNull ? Clock.UtcNow : resource.Meta.LastUpdated;
var updatedDateTime = new DateTimeOffset(lastUpdated.Value.DateTime.TruncateToMillisecond(), lastUpdated.Value.Offset);

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
lastUpdated
may be null at this access because it has a nullable type.
Variable
lastUpdated
may be null at this access because it has a nullable type.

Copilot Autofix

AI 10 days ago

To fix the issue, we need to ensure that lastUpdated is never dereferenced when it is null. This can be achieved by adding a null check before accessing lastUpdated.Value. If lastUpdated is null, an appropriate fallback value (e.g., Clock.UtcNow) should be used.

The fix involves:

  1. Adding a null check for lastUpdated before accessing lastUpdated.Value.
  2. Ensuring that the fallback value is used if lastUpdated is null.
Suggested changeset 1
src/Microsoft.Health.Fhir.Shared.Core/Features/Operations/Import/ImportResourceParser.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.Shared.Core/Features/Operations/Import/ImportResourceParser.cs b/src/Microsoft.Health.Fhir.Shared.Core/Features/Operations/Import/ImportResourceParser.cs
--- a/src/Microsoft.Health.Fhir.Shared.Core/Features/Operations/Import/ImportResourceParser.cs
+++ b/src/Microsoft.Health.Fhir.Shared.Core/Features/Operations/Import/ImportResourceParser.cs
@@ -53,2 +53,6 @@
             var lastUpdated = lastUpdatedIsNull ? Clock.UtcNow : resource.Meta.LastUpdated;
+            if (lastUpdated == null)
+            {
+                lastUpdated = Clock.UtcNow;
+            }
             var updatedDateTime = new DateTimeOffset(lastUpdated.Value.DateTime.TruncateToMillisecond(), lastUpdated.Value.Offset);
EOF
@@ -53,2 +53,6 @@
var lastUpdated = lastUpdatedIsNull ? Clock.UtcNow : resource.Meta.LastUpdated;
if (lastUpdated == null)
{
lastUpdated = Clock.UtcNow;
}
var updatedDateTime = new DateTimeOffset(lastUpdated.Value.DateTime.TruncateToMillisecond(), lastUpdated.Value.Offset);
Copilot is powered by AI and may make mistakes. Always verify output.

if (!(_contentElement == null ||
_contentElement.Value.ValueKind != JsonValueKind.Object
|| _contentElement.Value.EnumerateObject().Any() == false))

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == false' can be simplified to '!A'.

Copilot Autofix

AI 11 days ago

To fix the issue, we will simplify the Boolean expression _contentElement.Value.EnumerateObject().Any() == false to !_contentElement.Value.EnumerateObject().Any(). This change reduces unnecessary complexity while preserving the original logic. The fix will be applied directly to line 91 of the file.


Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
@@ -90,3 +90,3 @@
                   _contentElement.Value.ValueKind != JsonValueKind.Object
-                  || _contentElement.Value.EnumerateObject().Any() == false))
+                  || !_contentElement.Value.EnumerateObject().Any()))
             {
EOF
@@ -90,3 +90,3 @@
_contentElement.Value.ValueKind != JsonValueKind.Object
|| _contentElement.Value.EnumerateObject().Any() == false))
|| !_contentElement.Value.EnumerateObject().Any()))
{
Copilot is powered by AI and may make mistakes. Always verify output.

if (prop.ValueKind == JsonValueKind.Array)
{
return (prop.EnumerateArray().Select(x => x).ToArray(), true);

Check warning

Code scanning / CodeQL

Redundant Select Warning

This LINQ selection is redundant and can be removed.

Copilot Autofix

AI 11 days ago

To fix the issue, we will remove the redundant Select call from the LINQ query on line 210. The prop.EnumerateArray() method already returns an IEnumerable<JsonElement> that can be directly converted to an array using the ToArray() method. By removing the Select(x => x) call, we simplify the code without altering its behavior.


Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization/SourceNodes/JsonElementSourceNode.cs
@@ -209,3 +209,3 @@
             {
-                return (prop.EnumerateArray().Select(x => x).ToArray(), true);
+                return (prop.EnumerateArray().ToArray(), true);
             }
EOF
@@ -209,3 +209,3 @@
{
return (prop.EnumerateArray().Select(x => x).ToArray(), true);
return (prop.EnumerateArray().ToArray(), true);
}
Copilot is powered by AI and may make mistakes. Always verify output.
@brendankowitz brendankowitz force-pushed the personal/bkowitz/sourcenode-parser branch 3 times, most recently from 7a07f66 to d4cca2e Compare June 5, 2025 17:29
@brendankowitz brendankowitz force-pushed the personal/bkowitz/sourcenode-parser branch from d4cca2e to c55846e Compare June 5, 2025 18:45
ITypedElement node = sourceNode.ToTypedElement(ModelInfoProvider.StructureDefinitionSummaryProvider);
ITypedElement familyType = node.Select("Patient.name.family").Single();

IReadOnlyCollection<IElementDefinitionSummary> definitions = familyType.ChildDefinitions(ModelInfoProvider.StructureDefinitionSummaryProvider);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
definitions
is useless, since its value is never read.

Copilot Autofix

AI 10 days ago

To fix the issue, the assignment to the variable definitions on line 170 should be removed entirely, as it is not used anywhere in the method. This will clean up the code and eliminate the unnecessary assignment. No additional changes are required, as the removal does not affect the functionality of the method.


Suggested changeset 1
src/Microsoft.Health.Fhir.SourceNodeSerialization.UnitTests/MetaJsonNodeTests.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SourceNodeSerialization.UnitTests/MetaJsonNodeTests.cs b/src/Microsoft.Health.Fhir.SourceNodeSerialization.UnitTests/MetaJsonNodeTests.cs
--- a/src/Microsoft.Health.Fhir.SourceNodeSerialization.UnitTests/MetaJsonNodeTests.cs
+++ b/src/Microsoft.Health.Fhir.SourceNodeSerialization.UnitTests/MetaJsonNodeTests.cs
@@ -169,3 +169,3 @@
 
-        IReadOnlyCollection<IElementDefinitionSummary> definitions = familyType.ChildDefinitions(ModelInfoProvider.StructureDefinitionSummaryProvider);
+        familyType.ChildDefinitions(ModelInfoProvider.StructureDefinitionSummaryProvider);
     }
EOF
@@ -169,3 +169,3 @@

IReadOnlyCollection<IElementDefinitionSummary> definitions = familyType.ChildDefinitions(ModelInfoProvider.StructureDefinitionSummaryProvider);
familyType.ChildDefinitions(ModelInfoProvider.StructureDefinitionSummaryProvider);
}
Copilot is powered by AI and may make mistakes. Always verify output.
@brendankowitz brendankowitz force-pushed the personal/bkowitz/sourcenode-parser branch 2 times, most recently from 75e9def to cbbdcd6 Compare June 10, 2025 17:07
@brendankowitz brendankowitz force-pushed the personal/bkowitz/sourcenode-parser branch from cbbdcd6 to b0bdc96 Compare June 10, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant