I am using Hl7.Fhir.R4 library in my project which is an API Wrapper (in ASP.NET Core 2.2) around Azure API for FHIR. In a View Model, I declared a FHIR Element (FHIR's General-Purpose Data Type) as a property. Example:
public class MyPatient
{
public string Name { get; set;}
public Hl7.Fhir.Model.CodeableConcept MaritalStatus { get; set;} //This is
//defined in the library
}
Now, the problem is: The 'MaritalStatus' is not being parsed from json to the c# object (defined in the library), properly (i.e it is just 'not null'). Neither is there any exception being thrown.
Explained With Details: This is the JSON I am receiving from the front-end:
{
"name": "TheName",
"maritalStatus": {
"coding": [
{
"system": "terminology.hl7.org/CodeSystem/v3-MaritalStatus",
"code": "U",
"display": "unmarried"
}
],
"text": "Unmarried"
}
}
This is my model (C# class) against the JSON I am receiving from the front-end:
public class MyPatient
{
public string Name { get; set;}
public Hl7.Fhir.Model.CodeableConcept MaritalStatus { get; set;}
}
This the controller's action (in the wrapper layer - my project) which is entertaining the request:
[HttpPut("{id}")]
[Consumes("application/json")]
[Produces("application/json")]
public async Task<ActionResult> Update([Required][FromRoute] string id,[Required][FromBody] MyPatient myPatient)
{
if (ModelState.IsValid)
{
Hl7.Fhir.Model.Patient patient = await _fhirClient.ReadAsync<Patient>(location: "Patient/" + id);
patient.MaritalStatus = myPatient.MaritalStatus;
patient.Name[0].Text = myPatient.Name;
patient = await _fhirClient.UpdateAsync<Patient>(patient);
return Ok();
}//ends If ModelState.IsValid
return BadRequest();
}//ends Update
First, your instance isn't valid FHIR. You need to adhere to the syntax defined in the FHIR spec - and 'name' on Patient is not a simple string. As well, you need to declare the resourceType. That's not optional. Second, if you want to use the official library, you can't go off and invent your own classes and expect them to magically work - use the built-in parser. It'll be perfectly fine even if you've only got two elements present (plus the resourceType).