Search code examples
c#json.nethl7-fhir

Error with Create Patient FHIR API payload in Firely .Net Sdk


I'm trying to create a patient in the EPIC Sandbox using the below API Url https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Patient

I'm using the firely .Net Sdk for EPIC FHIR API Integration. In Postman, I'm able to create a patient using the below sample payload from the Sandbox environment itself.

{
"resourceType": "Patient",
"identifier": [
    {
        "use": "usual",
        "system": "urn:oid:2.16.840.1.113883.4.1",
        "value": "000-00-0000"
    }
],
"name": [
    {
        "use": "usual",
        "family": "family",
        "given": [
            "firstName",
            "lastName"
        ]
    }
],
"gender": "male",
"birthDate": "2000-01-01"
}

But the Firely SDK Patient Class generates the below payload:

{
"identifier": [
    {
        "system": {
            "value": "urn:oid:2.16.840.1.113883.4.1"
        },
        "value": {
            "value": "000-00-0000"
        }
    }
],
"name": [
    {
        "family": {
            "value": "family"
        },
        "given": [
            {
                "value": "firstName"
            },
            {
                "value": "lastName"
            }
        ]
    }
],
"gender": {
    "value": "male"
},
"birthDate": {
    "value": "2000-10-10"
}
}

Please find the code below(ignore the hardcodes) I'm using to create a patient

public Patient CreatePatient()
{
Patient newPatient = new Patient();

// Add identifier
newPatient.Identifier.Add(new Identifier
{
    Use = Identifier.IdentifierUse.Usual,
    System = "urn:oid:2.16.840.1.113883.4.1",
    Value = "000-00-0000"
});

// Add name
newPatient.Name.Add(new HumanName
{
    Use = HumanName.NameUse.Usual,
    Family = "family",
    Given = new string[] { "firstName", "lastName" }
});

// Set gender
newPatient.Gender = AdministrativeGender.Male;

// Set birth date
newPatient.BirthDate = "2000-10-10";

// Send FHIR request to create patient on the server
var response = _fhirService.Execute(client => client.Create(newPatient));

if (response is NewPatient patient)
{
    return patient;
}
else
{
    throw new Exception("Failed to create patient");
}
}

For resolving the issue, I tried the below code as well

        // Serialize the Patient instance to JSON
        var serializer = new FhirJsonSerializer();
        string json = serializer.SerializeToString(newPatient);

Still, I'm unable to create a patient as its giving the below error:

<OperationOutcome xmlns="http://hl7.org/fhir">
<issue>
    <severity value="fatal" />
    <code value="structure" />
    <diagnostics value="Failed to parse; structural issues in the content." />
    <expression value="$this" />
</issue>

Solution

  • See discussion at https://github.com/FirelyTeam/firely-net-sdk/issues/2657 where we try to get to the bottom of this.

    The Firely SDK serializes just fine, and the FhirClient which is part of that works. The main suspect for causing the problem is the Execute method in the custom FhirService which is not part of the SDK.