Skip to content

Commit

Permalink
Merge pull request #2723 from FirelyTeam/feature/2661-fhirclient-seri…
Browse files Browse the repository at this point in the history
…alizer-settings

#2661 FhirClient Serialization engine modification
  • Loading branch information
mmsmits authored Feb 21, 2024
2 parents bc1f5f8 + cc9bc1f commit 250bfd5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Hl7.Fhir.Base/Rest/BaseFhirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace Hl7.Fhir.Rest
public partial class BaseFhirClient : IDisposable
{
internal readonly ModelInspector Inspector;
private readonly IFhirSerializationEngine _serializationEngine;
private readonly Lazy<List<HttpStatusCode>> _200responses = new(() => Enum.GetValues(typeof(HttpStatusCode)).Cast<HttpStatusCode>().Where(n => (int)n > 199 && (int)n < 300).ToList());

/// <summary>
Expand Down Expand Up @@ -78,8 +77,6 @@ internal BaseFhirClient(Uri endpoint, ModelInspector inspector, FhirClientSettin
Inspector = inspector;
Settings = settings;
Endpoint = getValidatedEndpoint(endpoint);
_serializationEngine = settings.SerializationEngine ??
FhirSerializationEngineFactory.Legacy.FromParserSettings(Inspector, settings.ParserSettings ?? new());

Requester = requester;

Expand Down Expand Up @@ -1069,7 +1066,7 @@ private static ResourceIdentity verifyResourceIdentity(Uri location, bool needId
var request = tx.Entry[0];
var requestMessage = request.ToHttpRequestMessage(
Requester.BaseUrl,
_serializationEngine,
getSerializationEngine(),
Settings.UseFhirVersionInAcceptHeader ? fhirVersion : null,
Settings);

Expand All @@ -1079,7 +1076,7 @@ private static ResourceIdentity verifyResourceIdentity(Uri location, bool needId
// of the server, add a suggestion about this in the (legacy) parsing exception.
var suggestedVersionOnParseError = !Settings.VerifyFhirVersion ? fhirVersion : null;
(LastResult, LastBody, LastBodyAsText, LastBodyAsResource, var issue) =
await ValidateResponse(responseMessage, expect, _serializationEngine, suggestedVersionOnParseError)
await ValidateResponse(responseMessage, expect, getSerializationEngine(), suggestedVersionOnParseError)
.ConfigureAwait(false);

// If an error occurred while trying to interpret and validate the response, we will bail out now.
Expand Down Expand Up @@ -1172,6 +1169,11 @@ private static bool isPostOrPutOrPatch(Bundle.EntryComponent interaction) =>

private bool _versionChecked = false;

private IFhirSerializationEngine getSerializationEngine()
{
return Settings.SerializationEngine ?? FhirSerializationEngineFactory.Legacy.FromParserSettings(Inspector, Settings.ParserSettings ?? new());
}

private async Task verifyServerVersion(CancellationToken ct)
{
if (!Settings.VerifyFhirVersion) return;
Expand Down
24 changes: 24 additions & 0 deletions src/Hl7.Fhir.Support.Poco.Tests/Rest/FhirClientMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Hl7.Fhir.Introspection;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Protected;
Expand Down Expand Up @@ -416,6 +417,29 @@ public async T.Task TestCanMockFhirClient()
mock.Verify(c => c.ReadAsync<TestPatient>(It.IsAny<string>(), null, null, null), Times.Once);
}

[TestMethod]
public void TestFhirClientSettings()
{
var fhirClient1 = new BaseFhirClient(
new Uri("http://example.org"), TESTINSPECTOR, new FhirClientSettings { SerializationEngine = FhirSerializationEngineFactory.Strict(TESTINSPECTOR) }
);

MethodInfo? serializationEngineField = fhirClient1.GetType().GetMethod("getSerializationEngine", BindingFlags.NonPublic | BindingFlags.Instance);
serializationEngineField.Should().NotBeNull();

(serializationEngineField!.Invoke(fhirClient1, []) is PocoSerializationEngine).Should().BeTrue();

var fhirClient2 = new BaseFhirClient(
new Uri("http://example.org"), TESTINSPECTOR)
;

(serializationEngineField!.Invoke(fhirClient2, []) is ElementModelSerializationEngine).Should().BeTrue();

fhirClient2.Settings.SerializationEngine = FhirSerializationEngineFactory.Strict(TESTINSPECTOR);

(serializationEngineField!.Invoke(fhirClient2, []) is PocoSerializationEngine).Should().BeTrue();
}


[TestMethod]
public async T.Task TestCanCancelInteraction()
Expand Down

0 comments on commit 250bfd5

Please sign in to comment.