hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2.02k stars 1.32k forks source link

QuestionnaireResponse to Questionnaire links broken #1583

Closed sdatchley closed 4 years ago

sdatchley commented 4 years ago

Description Adding Questionnaires and linked QuestionnaireResponses does not create any HFJ_RES_LINK records thereby making QuestionnaireResponse searches based on Questionnaire return no resources. Also affects reverse include searches. Occurs in both public DSTU3 and R4 uhn instances. It was working in version 3.x HAPI builds (have old databases showing hfj_res_link rows linking QRs to Qs).

To reproduce I added example Q and QRs per bundle at end of issue. Existing data examples: http://hapi.fhir.org/baseR4/Questionnaire/55738/_history/1 http://hapi.fhir.org/baseR4/QuestionnaireResponse/55739/_history/1 http://hapi.fhir.org/baseR4/Questionnaire?_id=55738&_revinclude= (expect to get related QRs) http://hapi.fhir.org/baseR4/QuestionnaireResponse?_id=55739&_include= (expect to get referenced Patient and Questionnaire, only get Patient) http://hapi.fhir.org/baseR4/QuestionnaireResponse?questionnaire=55738&_include=* (no resources found)

http://hapi.fhir.org/baseDstu3/Questionnaire/2624379/_history/1 http://hapi.fhir.org/baseDstu3/QuestionnaireResponse/2624380/_history/1 http://hapi.fhir.org/baseDstu3/Questionnaire?_id=2624379&_revinclude= (expect to get related QRs) http://hapi.fhir.org/baseDstu3/QuestionnaireResponse?_id=2624380&_include= (expect to get referenced Patient and Questionnaire, only get Patient) http://hapi.fhir.org/baseDstu3/QuestionnaireResponse?questionnaire=2624379&_include=* (expect to find the Questionnaire resources by Questionnaire)

Expected behavior Linked Questionnaire or QuestionnaireResponse resources should be included in any searches, whether reverse include or include. Also, you should be able to search for QRs based on Questionnaire.

Example bundle { "resourceType": "Bundle", "type": "transaction", "entry": [ { "fullUrl": "Patient/P1", "resource": { "resourceType": "Patient", "id": "P1", "name": [{ "family": "Doe", "given": ["John"] }] }, "request": { "method": "POST", "url": "Patient" } }, { "fullUrl": "Questionnaire/Q1", "resource": { "resourceType": "Questionnaire", "id": "Q1", "name": "test questionnaire", "title": "test title", "status": "active", "subjectType": ["Patient"], "item": [ { "linkId": "1", "text": "Text 1", "type": "integer" }, { "linkId": "2", "text": "Text 2", "type": "string" }, { "linkId": "3", "text": "Text 3", "type": "dateTime" }, { "linkId": "4", "text": "Text 4", "type": "text" }, { "linkId": "5", "text": "Text 5", "type": "integer" } ] }, "request": { "method": "POST", "url": "Questionnaire" } }, { "fullUrl": "QuestionnaireResponse/QR1", "resource": { "resourceType": "QuestionnaireResponse", "id": "QR1", "questionnaire": "Q1", "status": "in-progress", "subject": { "reference": "Patient/P1" }, "authored": "2019-11-06T17:08:02-05:00" }, "request": { "method": "POST", "url": "QuestionnaireResponse" } }, { "fullUrl": "QuestionnaireResponse/QR2", "resource": { "resourceType": "QuestionnaireResponse", "id": "QR2", "questionnaire": "Q1", "status": "in-progress", "subject": { "reference": "Patient/P1" }, "authored": "2019-11-06T17:08:02-05:00" }, "request": { "method": "POST", "url": "QuestionnaireResponse" } } ] }

sdatchley commented 4 years ago

Also, not sure if related, the constraints in the R4 schematron for the QuestionnaireResponse does not have the constraints involving Questionnaire.

DSTU3 has constraints https://github.com/jamesagnew/hapi-fhir/blob/master/hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/schema/questionnaireresponse.sch

R4 does not https://github.com/jamesagnew/hapi-fhir/blob/master/hapi-fhir-validation-resources-r4/src/main/resources/org/hl7/fhir/r4/model/schema/questionnaireresponse.sch

sdatchley commented 4 years ago

So the type of the Questionnaire link in the FHIR spec changed from reference to canonical between STU3 and R4. Looking at the HAPI code in ResourceLinkExtractor you have this code in extractResourceLinks:

` if (nextObject instanceof CanonicalType) { StorageProcessingMessage msg = new StorageProcessingMessage(); msg.setMessage("Ignoring canonical reference (indexing canonidcal is not yet supported): " + ((CanonicalType) nextObject).getValueAsString()); HookParams params = new HookParams() .add(RequestDetails.class, theRequest) .addIfMatchesType(ServletRequestDetails.class, theRequest) .add(StorageProcessingMessage.class, msg); JpaInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequest, Pointcut.JPA_PERFTRACE_WARNING, params);

// nextObject = new Reference(((CanonicalType) nextObject).getValueAsString()); // wasCanonical = true; return; } `

Which prevents the creation of the ResourceLink later in the method. This code was changed on July 5th, 2019 in commit for issue 1358

I don't see a way you can query for QuestionnaireResponses via Questionnaire as is per FHIR spec with this code.

jamesagnew commented 4 years ago

Hmm, this is interesting.

Technically as a canonical reference I'm not sure it makes sense for this search to actually work.. A canonical type isn't supposed to be used for relative links AFAIK.

That said- I understand why you would want this to work, and I bet others would too. I'm going to post a question on chat.fhir.org about this.

jamesagnew commented 4 years ago

https://chat.fhir.org/#narrow/stream/179166-implementers/topic/References.20to.20Canonical

jamesagnew commented 4 years ago

Based on the response- I think there is a bigger issue here, but I am going to put a patch into HAPI so that your current search does work (in other words, we'll index references to a canonical that specifically holds a local reference as a reference, even though technically this is not valid)