FirelyTeam / firely-net-sdk

The official Firely .NET SDK for HL7 FHIR
Other
830 stars 345 forks source link

ModelState is invalid - Questionnaire purpose #1205

Closed mattkoch614 closed 4 years ago

mattkoch614 commented 4 years ago

I have a controller method that is attempting to run model binding validation on a Questionnaire (json) being passed in via a POST request.

The Questionnaire JSON representation is simple, and model validation consistently fails when it tries to deal with the purpose attribute:

{
    "resourceType": "Questionnaire",
    "id": "1",
    "identifier": [
        {
            "system": "https://system.abcde.org/",
            "value": "MED-1234"

        }
    ],
    "name": "teststudy123",
    "title": "Important Document",
    "status": "draft",
    "subjectType": [
        "ResearchStudy"
    ],
    "purpose": "Part A",
    "item": [
        {
            "linkId": "1"
            //omitted for brevity
        }
    ]
}

Here's a snippet from the Controller method:

        [Route("")]
        [HttpPost]
        public async Task<IHttpActionResult> store([FromBody] Questionnaire request)
        {
            if (!ModelState.IsValid)
            return BadRequest(ModelState);
            //...
        }

It seems that using a simple string for "Purpose" fails because it can't convert it to "Hl7.Fhir.Model.Markdown" but the FHIR spec indicates that string representations should be valid (for Markdown data types.):

Exception = {"Error converting value \"Part A\" to type 'Hl7.Fhir.Model.Markdown'. Path 'purpose', line 17, position 33."}

InnerException = {"Could not cast or convert from System.String to Hl7.Fhir.Model.Markdown."}
brianpos commented 4 years ago

I'd check the version of the fhir libs that you're using, that might cause issues there, also consider the http://github.com/brianpos/fhir-net-web-api facade project that does all the interface bindings, and let's you worry about the actual functionality you want to handle. Also handles xml and json without extra code on your side. Theres a demo in there showing a server that uses the file system for storage.

mattkoch614 commented 4 years ago

Thanks @brianpos. We got around this by accepting a JObject as a parameter in the Controller method instead of a strongly typed Questionnaire, and then just parsing the text representation of that into a Questionnaire. This works for our use case.

marcovisserFurore commented 4 years ago

The question has been answered. I close this issue.