Conversation
* 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
…chpatchexceptionfix' of https://github.com/microsoft/fhir-server into externalcontribution/Charles-Patrick-Moore/ambiguousmatchpatchexceptionfix
| [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
Show autofix suggestion
Hide autofix suggestion
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.
| @@ -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); | ||
| } | ||
| } | ||
| } | ||
| } |
…externalcontribution/Charles-Patrick-Moore/ambiguousmatchpatchexceptionfix
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
Semver Change (docs)
Patch|Skip|Feature|Breaking (reason)