I have a custom IResourceProvider that serves Measure (Measure.class).
In that code, I have the following extended operation. (from http://hapifhir.io/doc_rest_operations.html#_toc_extended_operations )
@Operation(name = "$humptydumpty")
public org.hl7.fhir.dstu3.model.Bundle acceptHumptyDumpty(HttpServletRequest servletRequest,
@IdParam(optional = true) IdType theId,
@OperationParam(name = "request") org.hl7.fhir.dstu3.model.Measure item) {
String fakeMessage;
if (null == item) {
fakeMessage = "org.hl7.fhir.dstu3.model.Measure item is null. Sad face. :( ";
} else {
fakeMessage = "org.hl7.fhir.dstu3.model.Measure item is not null. Happy face. :) ";
}
Bundle retVal = new Bundle();
retVal.setId(fakeMessage);
return retVal;
}
If I pass in the example JSON from
http://hl7.org/fhir/STU3/measure-exclusive-breastfeeding.json.html
POST
http://localhost:8080/fhir/Measure/MyMeasureName123/$humptydumpty
Everything works fine. I get back.
{
"resourceType": "Bundle",
"id": "org.hl7.fhir.dstu3.model.Measure item is not null. Happy face. :) "
}
So I understand the basics of how the $myExtendedMethod works.
NOW, when I try the same for .Parameters....
Java code (same MyResourceProvider as above)
@Operation(name = "$robinhood")
public Bundle acceptRobinHood(HttpServletRequest servletRequest,
@IdParam(optional = true) IdType theId,
@OperationParam(name = "request") org.hl7.fhir.dstu3.model.Parameters item) {
String fakeMessage;
if (null == item) {
fakeMessage = "org.hl7.fhir.dstu3.model.Parameters item is null. Sad face. :( ";
} else {
fakeMessage = "org.hl7.fhir.dstu3.model.Parameters item is not null. Happy face. :) ";
}
Bundle retVal = new Bundle();
retVal.setId(fakeMessage);
return retVal;
}
POST
http://localhost:8080/fhir/Measure/MyMeasureName123/$robinhood
I've sent in the "example" from http://hl7.org/fhir/STU3/parameters-example.json .
{
"resourceType": "Parameters",
"id": "example",
"parameter": [
{
"name": "start",
"valueDate": "2010-01-01"
},
{
"name": "end",
"resource": {
"resourceType": "Binary",
"contentType": "text/plain",
"content": "VGhpcyBpcyBhIHRlc3QgZXhhbXBsZQ=="
}
}
]
}
And if I send in ... the most basic json.
{
"resourceType": "Parameters",
"id": "MyParameterId234"
}
I get the sad face. :(
I've tried everything.
The "item" is always null. Aka, I get this back.
{
"resourceType": "Bundle",
"id": "org.hl7.fhir.dstu3.model.Parameters item is null. Sad face. :( "
}
I've tried many things, and finally went back to the ".Measure" just to prove I wasn't crazy.
But I cannot figure why one will populate (.Measure resource), and the other (.Parameters) will not. #help
My hapi fhir version:
<properties>
<hapi.version>3.6.0</hapi.version>
</properties>
<!-- FHIR dependencies -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu3</artifactId>
<version>${hapi.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-server</artifactId>
<version>${hapi.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>${hapi.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-validation-resources-dstu3</artifactId>
<version>${hapi.version}</version>
</dependency>
APPEND:
I did one for patient
@Operation(name = "$teddybear")
public org.hl7.fhir.dstu3.model.Bundle acceptTeddyBear(HttpServletRequest servletRequest,
@IdParam(optional = true) IdType theId,
@OperationParam(name = "request") org.hl7.fhir.dstu3.model.Patient item) {
String fakeMessage;
if (null == item) {
fakeMessage = "org.hl7.fhir.dstu3.model.Patient item is null. Sad face. :( ";
} else {
fakeMessage = "org.hl7.fhir.dstu3.model.Patient item is not null. Happy face. :) ";
}
Bundle retVal = new Bundle();
retVal.setId(fakeMessage);
return retVal;
}
POST
http://localhost:8080/fhir/Measure/MyMeasureName123/$teddybear
and it works fine.
{
"resourceType": "Bundle",
"id": "org.hl7.fhir.dstu3.model.Patient item is not null. Happy face. :) "
}
Its only the .Parameters resource that is hurting me.
APPEND
As per James A's answer and work around hint, I have put in the below.
Work Around Code: (aka, the "ANSWER" in the sense of a work around answer)
@Operation(name = "$robinhood")
public Bundle acceptRobinHood(HttpServletRequest servletRequest,
@IdParam(optional = true) IdType theId,
/*@OperationParam(name = "request") org.hl7.fhir.dstu3.model.Parameters item*/ @ResourceParam String theRawBody) {
String fakeMessage;
if (null == theRawBody || StringUtils.isBlank(theRawBody)) {
fakeMessage = "theRawBody is null or isBlank. Sad face. :( ";
} else {
fakeMessage = "theRawBody is not null and is not isBlank. Happy face. :) ";
}
org.hl7.fhir.dstu3.model.Parameters paramsObject = null;
FhirContext ctx = FhirContext.forDstu3();// this.getContext(); /* prefer encapsulated over hard coding, but for SOF, put in the hard code */
IParser parser = ctx.newJsonParser();
IBaseResource res = parser.parseResource(theRawBody);
paramsObject = (org.hl7.fhir.dstu3.model.Parameters) res;
if (null != paramsObject) {
fakeMessage += " org.hl7.fhir.dstu3.model.Parameters was serialized from theRawBody. Super Happy face. :) :)";
}
else
{
fakeMessage += " org.hl7.fhir.dstu3.model.Parameters was NOT serialized from theRawBody (is null). Super Sad face. :( :( ";
}
Bundle retVal = new Bundle();
retVal.setId(fakeMessage);
return retVal;
}
and the response from execution of the code:
{
"resourceType": "Bundle",
"id": "theRawBody is not null and is not isBlank. Happy face. :) org.hl7.fhir.dstu3.model.Parameters was serialized from theRawBody. Super Happy face. :) :)"
}
To be honest, this looks like a bug in HAPI FHIR. If you wanted to report it on the GitHub tracker that would be great.
You could probably work around it by adding a parameter along the lines of:
@ResourceParam String theRawBody
And using HAPI FHIR's parser to parse the Parameters resource. This is annoying for sure, but I believe it would work.