Adding FHIR extension for ODH Employer

48 views
Skip to first unread message

Tone Southerland

unread,
Jun 30, 2020, 2:38:52 PM6/30/20
to HAPI FHIR
Hi, 
I am working on an implementation of a new FHIR Profile and using the HAPI library. I am able to add codeable concepts, narrative text, etc. and this serializes to JSON just fine.  I can also see the Observation object instantiate the extension as part of that obs object in debug, but I can't seem to work out how to get it to serialize to JSON when my service is called. The extension I am working on is http://hl7.org/fhir/us/odh/StructureDefinition-odh-Employer-extension.html if it matter. 

I'm sure it's something simple... what am I missing here?

Here's my code:

Observation obs = new Observation();
....
List<Extension> extensions = new ArrayList<Extension>();
Extension ext = new Extension();
ext.setUrl("http://hl7.org/fhir/us/odh/StructureDefinition/odh-Employer-extension");
ext.setId("odh-Employer-extension");
obs.addExtension(ext);

Thanks!
Tone

Tone Southerland

unread,
Jun 30, 2020, 3:15:25 PM6/30/20
to HAPI FHIR
So.... I'm answering my own question. This is really simple, but at 100 am when I was initially working through this it didn't seem so for some reason. Was going to delete my post but maybe someone else is looking for the answer to this simple question.

Here's my updated and working code...(and I also pasted the incorrect snippet initially as the Array List was never filled).

Observation obs = new Observation();
....
List<Extension> extensions = new ArrayList<Extension>();
Extension ext = new Extension();
ext.setUrl("http://hl7.org/fhir/us/odh/StructureDefinition/odh-Employer-extension");
ext.setId("odh-Employer-extension");
extensions.add(ext);
obs.addExtension().setExtension(extensions);


So the key bit I was missing was to call addExtension first, and then chain setExtension and pass in the ArrayList.


And that gives me this:

{
"resourceType": "Observation",
"text": {
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">This observation resource contains the industry and occupation for a patient</div>"
},
"extension": [
{
"url": null,
"extension": [
{
"id": "odh-Employer-extension",
}
]
}
],
....

Larry Shields

unread,
Jun 30, 2020, 5:30:48 PM6/30/20
to HAPI FHIR
This might not be doing what you want it to do.  Based on the Code and Json, you're adding an Extension to an Extension.  Extensions by themselves can have other extensions.  If your intent is to add an extension to the Observation Resource and not to an extension inside the Observation Resource, you should be able to do what you were by calling the addExtension Method on the Observation and passing in the observation.  What HAPI FHIR Version are you working with?

Tone Southerland

unread,
Jun 30, 2020, 7:58:27 PM6/30/20
to HAPI FHIR
Ah good point, I don't think I want to do this. I'm implementing the PastorPresentJob Profile. It looks like a single extension on the observation. You mentioned passing in the observation to the extension, but I think I would pass in the extension to the .addExtension method ON the observation, no? I am using version 4.1.0.

So the following code results in nothing being serialized..  

Here's a link to the .addExtension().setExtension(...) version -- https://dev.fhir.onerecord.com/Observation/d92adee0-9991-4a21-8c89-a74bd42a9798 - and it's nested as you mentioned. 

List<Extension> extensions = new ArrayList<Extension>();
Extension ext = new Extension();
ext.setUrl("http://hl7.org/fhir/us/odh/StructureDefinition/odh-Employer-extension");
ext.setId("odh-Employer-extension");
extensions.add(ext);
//obs.addExtension().setExtension(extensions);
obs.addExtension(ext);

Tone Southerland

unread,
Jun 30, 2020, 8:06:40 PM6/30/20
to HAPI FHIR
Below is where the instance is being filled in debug (in IntelliJ)

I tried both:

obs.addExtension(ext);

and

obs.setExtension(extensions);

.. the former passing in the single extension object and the latter passing in the list. 

but neither results in serialized JSON. 

obs_ext.png

Larry Shields

unread,
Jul 1, 2020, 6:37:18 AM7/1/20
to HAPI FHIR
That was a typo on my part.  You're doing everything right, but it looks like you need to set a value for the extension in order for it to appear in serialized Json.  Without a value, it treats it as an empty field.

Observation obs = new Observation();
obs.setId("MYObservation");
Extension ext = new Extension("http://hl7.org/fhir/us/odh/StructureDefinition/odh-Employer-extension");
ext.setId("odh-Employer-extension");
ext.setValue(new StringType("MyValue"));
obs.addExtension(ext);

String json = ctx.newJsonParser().encodeResourceToString(obs);


This adheres then to the following rule in the FHIR spec concerning the Extension datatype:

An extension SHALL have either a value (i.e. a value[x] element) or sub-extensions, but not both. If present, the value[x] element SHALL have content (value attribute or other elements)

Tone Southerland

unread,
Jul 1, 2020, 9:09:04 AM7/1/20
to HAPI FHIR
Ah this works great - thank you! 

Here's the final code snippet where I added a Reference value for the employer (in lieu of a primitive). 

/* Load organization */
Organization org = new Organization();
org.setId("#localOrganization");
org.getNameElement().setValue("Awesome Sauce Software Co");
org.addAddress().setState("TN");
org.addAddress().setCity("Somewhereville");
org.addAddress().setPostalCode("37174");

List<StringType> lineList = new ArrayList<StringType>();
StringType lineItems = new StringType("12 Radical St");
lineList.add(lineItems);
org.addAddress().setLine(lineList);

/* Create extension and set to observation */

Extension ext = new Extension("http://hl7.org/fhir/us/odh/StructureDefinition/odh-Employer-extension");
ext.setId("odh-Employer-extension");
ext.setValue(new Reference(org));
obs.addExtension(ext);


And it results in this json:

json.png
Reply all
Reply to author
Forward
0 new messages