Stream: hapi
Topic: setting contained resources
Håkan MacLean (Nov 20 2020 at 16:50):
Hi!
During the java session today I asked why my contained resources did not appear when JSON serialising and got the answer that they have to be referenced properly with their ID to appear. Thanks for the help @Patrick Werner and @James Agnew ! I tried doing this, but they still don't appear. Excerpt from my code:
var module1 = new ActivityDefinition()
.setKind(ActivityDefinition.ActivityDefinitionKind.TASK)
.setUrl("url/to/module1");
module1.setId("idForModue1");
var planDefinition = new PlanDefinition();
var module1action =
new PlanDefinition.PlanDefinitionActionComponent()
.setDescription("module1")
.setDefinition(new UriType(module1.getId())); //using uri instead of canonical
planDefinition.addAction(module1action);
planDefinition.setContained(List.of(module1));
And this is my output, without any contained:
{
"resourceType": "PlanDefinition",
"action": [ {
"description": "module1",
"definitionUri": "idForModue1"
} ]
}
If this is described in the standard or the HAPI docs, please just refer me to the right place in the docs.
Daniel Venton (Nov 20 2020 at 20:52):
I have the same problem. I'm trying to include a Provenance Resource in the contained list. Procedure doesn't reference it, Provenance references the procedure. However, this is supposed to work. All items in the contained list must either reference the parent or be referenced by the parent.
I'm using 5.1.0 of hapi. Here is a test that fails. a serialized bundle that has Procedure w/Provenance. When it is deserialized the Procedure object does have the Provenance, but when re-serialized it does not. Java:
private String serializedBundle = "{\n" +
" \"resourceType\": \"Bundle\",\n" +
" \"id\": \"401cc9a2-5f81-4806-8f33-fbf80e94bed3\",\n" +
" \"meta\": {\n" +
" \"lastUpdated\": \"2020-11-20T11:38:36.350-08:00\"\n" +
" },\n" +
" \"type\": \"searchset\",\n" +
" \"total\": 1,\n" +
" \"link\": [\n" +
" {\n" +
" \"relation\": \"self\",\n" +
" \"url\": \"http://localhost:8083/R4/Procedure?_id=100&_revinclude=Provenance%3Atarget\"\n" +
" }\n" +
" ],\n" +
" \"entry\": [\n" +
" {\n" +
" \"fullUrl\": \"http://localhost:8083/R4/Procedure/100\",\n" +
" \"resource\": {\n" +
" \"resourceType\": \"Procedure\",\n" +
" \"id\": \"100\",\n" +
" \"meta\": {\n" +
" \"lastUpdated\": \"2020-10-29T12:34:55.124Z\"\n" +
" },\n" +
" \"contained\": [\n" +
" {\n" +
" \"resourceType\": \"Practitioner\",\n" +
" \"id\": \"2\",\n" +
" \"identifier\": [\n" +
" {\n" +
" \"system\": \"http://hl7.org/fhir/sid/us-npi\",\n" +
" \"value\": \"ctc123\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"resourceType\": \"Provenance\",\n" +
" \"id\": \"1\",\n" +
" \"target\": [\n" +
" {\n" +
" \"reference\": \"Procedure/100\"\n" +
" }\n" +
" ],\n" +
" \"recorded\": \"2020-10-29T12:34:55.124Z\",\n" +
" \"agent\": [\n" +
" {\n" +
" \"type\": {\n" +
" \"coding\": [\n" +
" {\n" +
" \"system\": \"http://terminology.hl7.org/CodeSystem/provenance-participant-type\",\n" +
" \"code\": \"informant\",\n" +
" \"display\": \"informant\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"who\": {\n" +
" \"reference\": \"#2\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"status\": \"unknown\",\n" +
" \"code\": {\n" +
" \"coding\": [\n" +
" {\n" +
" \"system\": \"http://terminology.lh7.org/CodeSystem/claim-type\",\n" +
" \"code\": \"institutional\",\n" +
" \"display\": \"Institutional Display\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"subject\": {\n" +
" \"reference\": \"Patient/551261670\"\n" +
" },\n" +
" \"performedDateTime\": \"2020-10-29T12:34:55.124Z\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}\n";
@Test
public void testContainedProvenance() {
FhirContext context = FhirContext.forR4();
IParser parser = context.newJsonParser();
Bundle b = (Bundle) parser.parseResource(serializedBundle);
String s = parser.encodeResourceToString(b); //<- this excludes the Provenance
}
Daniel Venton (Nov 20 2020 at 21:04):
Interesting enough, although I think conceptually wrong, if I set the provenance resource to target itself, then it is included in the output.
" \"target\": [\n" +
" {\n" +
" \"reference\": \"#1\"\n" +
" },\n" +
" {\n" +
" \"reference\": \"Procedure/100\"\n" +
" }\n" +
" ],\n" +
Patrick Werner (Nov 22 2020 at 12:46):
@Håkan MacLean
You are still mixing up canonical urls with urls. ActivityDefinition.url is a canonical url.
Patrick Werner (Nov 22 2020 at 13:05):
But i tested it with Canonical as well, was not working.
Patrick Werner (Nov 22 2020 at 13:08):
Possibly this is a bug, as canonicals were introduced after references.
Patrick Werner (Nov 22 2020 at 13:20):
@Daniel Venton please format your code properly, it is very hard to read and help in this form.
Just had a quick look. Provenance wants to reference to Procedure, this is done via the reference string: #
Patrick Werner (Nov 22 2020 at 13:20):
But even with #
it isn't working.
Patrick Werner (Nov 22 2020 at 13:20):
Resources can only be contained in other resources if there is a reference from the resource to the contained resource, or if the contained resource references the container resource. This is intended to ensure that the meaning of the contained resource is clear, and that there is no confusion as to its significance.
For a resource that references the container, the reference is "#"
https://www.hl7.org/fhir/references.html#contained
Håkan MacLean (Nov 22 2020 at 20:19):
Patrick Werner said:
You are still mixing up canonical urls with urls. ActivityDefinition.url is a canonical url.
Thank you for taking the time to look into this @Patrick Werner , really appreciate it! However I'm still confused since I'm not actually using the ActivityDefintion.url for anything atm.
Currently, I'm referencing the ActivityDefinition.id (not url!) from PlanDefinition.action.definition. I also tried adding a # before the id, but that made no difference. See code below:
var module1 = new ActivityDefinition()
.setKind(ActivityDefinition.ActivityDefinitionKind.TASK);
module1.setId("idForModue1");
var planDefinition = new PlanDefinition();
var module1action =
new PlanDefinition.PlanDefinitionActionComponent()
.setDescription("module1")
.setDefinition(new UriType("#" + module1.getId()));
planDefinition.addAction(module1action);
planDefinition.setContained(List.of(module1))
And output:
{
"resourceType": "PlanDefinition",
"action": [ {
"description": "module1",
"definitionUri": "#idForModue1"
} ]
}
Is it perhaps so that PlanDefinition.action.definition does not "count" as a reference somehow? I also recall you mentioned using the actual java object when creating a reference, but I never got that to work either. I tried:
.setDefinition(new Reference(module1))
But then I get a runtime error since Reference is not of type CanonicalType or UriType.
Or if you know of a working example somewhere perhaps I could look at it and debug the HAPI source code do find out how I should do it.
Daniel Venton (Nov 23 2020 at 13:57):
Patrick Werner said:
Daniel Venton please format your code properly, it is very hard to read and help in this form.
Just had a quick look. Provenance wants to reference to Procedure, this is done via the reference string:#
It was formatted correctly until zulip got ahold of it, perhaps there is a code markup that I'm unaware of.
Last updated: Apr 12 2022 at 19:14 UTC