Skip to content

Fix for Ambiguous match exception in patch call#5321

Merged
PTaladay merged 4 commits intomainfrom
externalcontribution/Charles-Patrick-Moore/ambiguousmatchpatchexceptionfix
Jan 12, 2026
Merged

Fix for Ambiguous match exception in patch call#5321
PTaladay merged 4 commits intomainfrom
externalcontribution/Charles-Patrick-Moore/ambiguousmatchpatchexceptionfix

Conversation

@PTaladay
Copy link
Contributor

@PTaladay PTaladay commented Jan 9, 2026

Description

External contribution by Charles Patrick Moore. This fixes the issue with ambiguous match exception during a patch operation where the route was the same, but the content type was different. This now will return a bad request when an empty body is sent in this circumstance.

Related issues

Addresses [issue AB#180232].

Testing

Tested against existing tests. Unit and end to end tests were added in to cover edge case scenario.

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)

Charles-Patrick-Moore and others added 3 commits January 9, 2026 12:23
* initial commit

* Fix AmbiguousMatchException between PatchJson and PatchFhir methods works!

* Revert "Fix AmbiguousMatchException between PatchJson and PatchFhir methods works!"

This reverts commit 5b80b3c.

* Revert "initial commit"

This reverts commit 42707e6.

* Added EmptyBodyBehavior Disallow

* Fix Merge Conflicts

* Added Unit Tests
@PTaladay PTaladay added this to the FY26\Q2\2Wk\2Wk13 milestone Jan 9, 2026
@PTaladay PTaladay requested a review from a team as a code owner January 9, 2026 22:41
@PTaladay PTaladay added Bug Bug bug bug. Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Open source This change is only relevant to the OSS code or release. No-PaaS-breaking-change labels Jan 9, 2026
[Fact]
public async Task GivenPatchFhirRequestWithEmptyBody_WhenInvoked_ThenReturnsBadRequest()
{
var request = new HttpRequestMessage(HttpMethod.Patch, "Patient/example");

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning test

Disposable 'HttpRequestMessage' is created but not disposed.

Copilot Autofix

AI about 2 months ago

In general, the fix is to ensure that the HttpRequestMessage is disposed after use, ideally by wrapping its lifetime in a using scope. This will deterministically release any underlying resources associated with the request and its content once the call to SendAsync and subsequent assertions are complete.

For this specific method GivenPatchFhirRequestWithEmptyBody_WhenInvoked_ThenReturnsBadRequest in test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.cs, the best minimal fix is to wrap the creation and use of request in a using block. We only need to surround the existing code that creates the HttpRequestMessage, sets its Content, sends it, and asserts on the response with using (var request = new HttpRequestMessage(...)) { ... }. No additional imports are required because using is a language construct, not a library. This preserves functionality while ensuring Dispose is called on request (and, transitively, its Content) when the block ends.

Concretely, on lines 701–707, replace the straight‑line code that instantiates request with a using block that encloses lines 701–707. The body of the new using block will be identical to the existing code, just indented one level.

Suggested changeset 1
test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.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/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.cs b/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.cs
--- a/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.cs
+++ b/test/Microsoft.Health.Fhir.Shared.Tests.E2E/Rest/FhirPathPatchTests.cs
@@ -698,13 +698,15 @@
         [Fact]
         public async Task GivenPatchFhirRequestWithEmptyBody_WhenInvoked_ThenReturnsBadRequest()
         {
-            var request = new HttpRequestMessage(HttpMethod.Patch, "Patient/example");
-            request.Content = new StringContent(string.Empty); // Empty body
-            request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/fhir+json");
+            using (var request = new HttpRequestMessage(HttpMethod.Patch, "Patient/example"))
+            {
+                request.Content = new StringContent(string.Empty); // Empty body
+                request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/fhir+json");
 
-            var response = await _client.HttpClient.SendAsync(request);
+                var response = await _client.HttpClient.SendAsync(request);
 
-            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
+            }
         }
     }
 }
EOF
@@ -698,13 +698,15 @@
[Fact]
public async Task GivenPatchFhirRequestWithEmptyBody_WhenInvoked_ThenReturnsBadRequest()
{
var request = new HttpRequestMessage(HttpMethod.Patch, "Patient/example");
request.Content = new StringContent(string.Empty); // Empty body
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/fhir+json");
using (var request = new HttpRequestMessage(HttpMethod.Patch, "Patient/example"))
{
request.Content = new StringContent(string.Empty); // Empty body
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/fhir+json");

var response = await _client.HttpClient.SendAsync(request);
var response = await _client.HttpClient.SendAsync(request);

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
}
}
}
Copilot is powered by AI and may make mistakes. Always verify output.
@PTaladay PTaladay merged commit 8414db5 into main Jan 12, 2026
62 of 63 checks passed
@PTaladay PTaladay deleted the externalcontribution/Charles-Patrick-Moore/ambiguousmatchpatchexceptionfix branch January 12, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug. No-PaaS-breaking-change Open source This change is only relevant to the OSS code or release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants