Skip to content

Commit f71543e

Browse files
Merge pull request #1690 from FirelyTeam/bugfix/1681-client-handle-prefer-operation-outcome-resource-in-response
FhirClient: handle prefer operation outcome in resource response
2 parents feb3da5 + a22c507 commit f71543e

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

src/Hl7.Fhir.Core.Tests/Rest/FhirClientTests.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ public class FhirClientTests
3838
//public static Uri testEndpoint = new Uri("http://fhirtest.uhn.ca/baseDstu3");
3939
//public static Uri testEndpoint = new Uri("http://localhost:49911/fhir");
4040
//public static Uri testEndpoint = new Uri("http://sqlonfhir-stu3.azurewebsites.net/fhir");
41-
public static Uri testEndpoint = new Uri("https://vonk.fire.ly/r3");
41+
public static Uri testEndpoint = new Uri("https://server.fire.ly/r3");
4242

4343
//public static Uri _endpointSupportingSearchUsingPost = new Uri("http://localhost:49911/fhir");
4444
public static Uri _endpointSupportingSearchUsingPost = new Uri("http://localhost:4080");
4545
//public static Uri _endpointSupportingSearchUsingPost = new Uri("https://vonk.fire.ly/r3");
4646

4747
public static Uri TerminologyEndpoint = new Uri("https://stu3.ontoserver.csiro.au/fhir");
4848

49+
private static string patientId = "pat1" + ModelInfo.Version;
50+
private static string locationId = "loc1" + ModelInfo.Version;
51+
4952
#if !NETCOREAPP2_1
5053
[ClassInitialize]
5154
public static void ClassInitialize(TestContext testContext)
@@ -87,7 +90,7 @@ private static void CreateItems()
8790
Family = "Donald",
8891
}
8992
},
90-
Id = "pat1",
93+
Id = patientId,
9194
Identifier = new List<Identifier>()
9295
{
9396
new Identifier()
@@ -104,7 +107,7 @@ private static void CreateItems()
104107
{
105108
City = "Den Burg"
106109
},
107-
Id = "1"
110+
Id = locationId
108111
};
109112

110113
// Create the patient
@@ -281,7 +284,7 @@ private void testReadWithFormat(BaseFhirClient client)
281284
public void ReadWrongResourceType()
282285
{
283286
LegacyFhirClient client = new LegacyFhirClient(testEndpoint);
284-
var loc = client.Read<Patient>("Location/1");
287+
var loc = client.Read<Patient>("Location/" + locationId);
285288
}
286289

287290
[TestMethod, TestCategory("FhirClient"), TestCategory("IntegrationTest")]
@@ -294,7 +297,7 @@ public void ReadWrongResourceTypeHttpClient()
294297

295298
private void testReadWrongResourceType(BaseFhirClient client)
296299
{
297-
var loc = client.Read<Patient>("Location/1");
300+
var loc = client.Read<Patient>("Location/" + locationId);
298301
}
299302

300303
[TestMethod, TestCategory("FhirClient"), TestCategory("IntegrationTest")]
@@ -315,14 +318,14 @@ public void ReadHttpClient()
315318

316319
private void testReadClient(BaseFhirClient client)
317320
{
318-
var loc = client.Read<Location>("Location/1");
321+
var loc = client.Read<Location>("Location/" + locationId);
319322
Assert.IsNotNull(loc);
320323
Assert.AreEqual("Den Burg", loc.Address.City);
321324

322-
Assert.AreEqual("1", loc.Id);
325+
Assert.AreEqual(locationId, loc.Id);
323326
Assert.IsNotNull(loc.Meta.VersionId);
324327

325-
var loc2 = client.Read<Location>(ResourceIdentity.Build("Location", "1", loc.Meta.VersionId));
328+
var loc2 = client.Read<Location>(ResourceIdentity.Build("Location", locationId, loc.Meta.VersionId));
326329
Assert.IsNotNull(loc2);
327330
Assert.AreEqual(loc2.Id, loc.Id);
328331
Assert.AreEqual(loc2.Meta.VersionId, loc.Meta.VersionId);
@@ -338,7 +341,7 @@ private void testReadClient(BaseFhirClient client)
338341
Assert.AreEqual("404", client.LastResult.Status);
339342
}
340343

341-
var loc3 = client.Read<Location>(ResourceIdentity.Build("Location", "1", loc.Meta.VersionId));
344+
var loc3 = client.Read<Location>(ResourceIdentity.Build("Location", locationId, loc.Meta.VersionId));
342345
Assert.IsNotNull(loc3);
343346
var jsonSer = new FhirJsonSerializer();
344347
Assert.AreEqual(jsonSer.SerializeToString(loc),
@@ -370,11 +373,11 @@ public void ReadRelativeHttpClient()
370373

371374
private void testReadRelative(BaseFhirClient client)
372375
{
373-
var loc = client.Read<Location>(new Uri("Location/1", UriKind.Relative));
376+
var loc = client.Read<Location>(new Uri("Location/" + locationId, UriKind.Relative));
374377
Assert.IsNotNull(loc);
375378
Assert.AreEqual("Den Burg", loc.Address.City);
376379

377-
var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
380+
var ri = ResourceIdentity.Build(testEndpoint, "Location", locationId);
378381
loc = client.Read<Location>(ri);
379382
Assert.IsNotNull(loc);
380383
Assert.AreEqual("Den Burg", loc.Address.City);
@@ -399,11 +402,11 @@ public void ReadRelativeAsyncHttpClient()
399402

400403
private void testRelativeAsyncClient(BaseFhirClient client)
401404
{
402-
var loc = client.ReadAsync<Location>(new Uri("Location/1", UriKind.Relative)).Result;
405+
var loc = client.ReadAsync<Location>(new Uri("Location/" + locationId, UriKind.Relative)).Result;
403406
Assert.IsNotNull(loc);
404407
Assert.AreEqual("Den Burg", loc.Address.City);
405408

406-
var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
409+
var ri = ResourceIdentity.Build(testEndpoint, "Location", locationId);
407410
loc = client.ReadAsync<Location>(ri).Result;
408411
Assert.IsNotNull(loc);
409412
Assert.AreEqual("Den Burg", loc.Address.City);
@@ -732,7 +735,7 @@ private static void testCreateAndFullRepresentation(BaseFhirClient client)
732735
{
733736
client.Settings.PreferredReturn = Prefer.ReturnRepresentation; // which is also the default
734737

735-
var pat = client.Read<Patient>("Patient/pat1");
738+
var pat = client.Read<Patient>("Patient/" + patientId);
736739
ResourceIdentity ri = pat.ResourceIdentity().WithBase(client.Endpoint);
737740
pat.Id = null;
738741
pat.Identifier.Clear();
@@ -793,7 +796,7 @@ private void testCreateEditDelete(BaseFhirClient client)
793796
{
794797
// client.CompressRequestBody = true;
795798

796-
var pat = client.Read<Patient>("Patient/pat1");
799+
var pat = client.Read<Patient>("Patient/" + patientId);
797800
pat.Id = null;
798801
pat.Identifier.Clear();
799802
pat.Identifier.Add(new Identifier("http://hl7.org/test/2", "99999"));
@@ -1322,7 +1325,7 @@ public void CallsCallbacks()
13221325
status = e.RawResponse.StatusCode;
13231326
};
13241327

1325-
var pat = client.Read<Patient>("Patient/pat1");
1328+
var pat = client.Read<Patient>("Patient/" + patientId);
13261329
Assert.IsTrue(calledBefore);
13271330
Assert.IsNotNull(status);
13281331
Assert.IsNotNull(body);
@@ -1366,7 +1369,7 @@ public void CallsCallbacksHttpClient()
13661369
status = e.RawResponse.StatusCode;
13671370
};
13681371

1369-
var pat = client.Read<Patient>("Patient/pat1");
1372+
var pat = client.Read<Patient>("Patient/" + patientId);
13701373
Assert.IsTrue(calledBefore);
13711374
Assert.IsNotNull(status);
13721375
Assert.IsNotNull(body);
@@ -1494,7 +1497,7 @@ public void TestRefreshHttpClient()
14941497

14951498
private static void clientReadRefresh(BaseFhirClient client)
14961499
{
1497-
var result = client.Read<Patient>("Patient/pat1");
1500+
var result = client.Read<Patient>("Patient/" + patientId);
14981501

14991502
var orig = result.Name[0].FamilyElement.Value;
15001503

@@ -1763,6 +1766,24 @@ public void TestCreatingBinaryResourceHttpClient()
17631766
}
17641767
}
17651768

1769+
/// <summary>
1770+
/// Test for showing issue https://github.com/FirelyTeam/firely-net-sdk/issues/1681
1771+
/// </summary>
1772+
[TestMethod, TestCategory("IntegrationTest"), TestCategory("FhirClient")]
1773+
public void TestPreferOperationOutcome()
1774+
{
1775+
FhirClient client = new FhirClient(testEndpoint);
1776+
client.Settings.PreferredReturn = Prefer.OperationOutcome;
1777+
1778+
var pat = new Patient()
1779+
{
1780+
Name = new List<HumanName> { new HumanName().WithGiven("testy").AndFamily("McTestFace") }
1781+
};
1782+
1783+
var oo = client.Create<Patient>(pat);
1784+
Assert.IsNotNull(client.LastResult.Outcome);
1785+
}
1786+
17661787
[Ignore]
17671788
[TestMethod, TestCategory("IntegrationTest"), TestCategory("FhirClient")]
17681789
public void TestOperationEverything()
@@ -1840,7 +1861,7 @@ public void TestMultipleMessageHandlersInFhirClient()
18401861
};
18411862

18421863
using var client = new FhirClient(testEndpoint, settings: FhirClientSettings.CreateDefault(), testDegatingHandler);
1843-
var loc = client.Read<Location>("Location/1");
1864+
var loc = client.Read<Location>("Location/" + locationId);
18441865
Assert.IsNotNull(testDegatingHandler.LastRequest);
18451866
Assert.IsNotNull(testMessageHandler.LastResponse);
18461867
}

src/Hl7.Fhir.Core/Rest/TypedEntryResponseToBundle.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public static Bundle.EntryComponent ToBundleEntry(this TypedEntryResponse entry,
6262
{
6363
result.Resource = new BaseFhirParser(parserSettings).Parse<Resource>(entry.TypedElement);
6464

65+
//if the response is an operation outcome, add it to response.outcome. This is necessary for when a client uses return=OperationOutcome as a prefer header.
66+
// see also issue #1681
67+
if (result.Resource is OperationOutcome)
68+
{
69+
result.Response.Outcome = result.Resource;
70+
}
71+
6572
if (result.Response.Location != null)
6673
result.Resource.ResourceBase = new ResourceIdentity(result.Response.Location).BaseUri;
6774
}

0 commit comments

Comments
 (0)