Search code examples
hl7-fhir

FhirClient not adding version into AdverseEvent


I am using Hl7.Fhir.STU3 and I have an AdverseEvent configured as follows:

var adverseEvent = new AdverseEvent()
{
   Meta = new Meta()
   {
       Profile = new string[] { "https://psims-uat.azure-api.net/taxonomy/fhir/StructureDefinition/patient-safety-adverse-event-5" }
   },                 
   VersionId = "5.0.0"
};

And a FhirClient configured as follows:

var uri = "https://psims-uat.azure-api.net/adverse-event/fhir";
var fhirClient = new FhirClient(uri, messageHandler: _handler);
return await fhirClient.CreateAsync<AdverseEvent>(adverseEvent);

Returns an OperationOutcome error as follows:

Exception: Version '0' is not a recognised active PSIMS Taxonomy version

When I run CreateAsync<AdverseEvent> the versionId is does not seem to be getting applied to the AdverseEvent, but instead appears to be getting added to the Meta element instead, for example, if I serialise the JSON object and review it:

var options = new JsonSerializerOptions().ForFhir().Pretty();
string adverseEventJson = JsonSerializer.Serialize(adverseEvent, options);

This gives me:

{
   "resourceType": "AdverseEvent",
  "meta": {
    "profile": [
      "versionId": "5.0.0",
      "https://psims-uat.azure-api.net/taxonomy/fhir/StructureDefinition/patient-safety-adverse-event-5"
    ]
  }
}

What is the correct approach for sending an AdverseEvent to a Fhir Server?


NOTE: I can send a correctly formatted message in Postman, and not see the same error:

{
    "resourceType": "AdverseEvent",
    "versionId": "5.0.0",
    "meta": {
        "profile": [
            "https://psims-uat.azure-api.net/taxonomy/fhir/StructureDefinition/patient-safety-adverse-event-5"
        ]
    }
}

Results in (as expected):

{
  "resourceType": "OperationOutcome",
  "issue": [
      {
          "severity": "error",
          "code": "invalid",
          "diagnostics": "NullReferenceException: Object reference not set to an instance of an object."
      }
  ]
}

Solution

  • AdverseEvent does not have a field called versionId, except for inside the AdverseEvent.meta. The VersionId field from the Resource class you are setting is just a shortcut for setting the resource's meta.versionId - see the source code here: https://github.com/FirelyTeam/firely-net-sdk/blob/develop/src/Hl7.Fhir.Base/Model/Resource.cs.

    So it makes sense that your serialized output would show the versionId in the Meta - although in my test it ended up as separate field, not as a value for the profile list.

    I do not know what 'recognised active PSIMS Taxonomy version' means, but this seems to be an error from the server that is trying to create your AdverseEvent. Usually servers will assign their own versionIds to resources, so instead of the error I would have expected the assigned version to be ignored and overwritten.

    The error you get when using Postman is indeed expected because the supplied AdverseEvent json is not valid. But I do think that message could have been more clear.

    To send your AdverseEvent to the server, have you tried to leave the versionId field empty?