hapi-fhir icon indicating copy to clipboard operation
hapi-fhir copied to clipboard

LastN not working when providing max parameter (6.1.0)

Open ZuSe opened this issue 2 years ago • 1 comments

NOTE: Before filing a ticket, please see the following URL: https://github.com/hapifhir/hapi-fhir/wiki/Getting-Help

Describe the bug Trying to use lastn with elastic enabled fails when adding a max parameter.

To Reproduce Steps to reproduce the behavior:

  1. Run latest jpa starter using 6.1.0
  2. Make sure u have elastic and lastn enabled in ur config
  3. add some observations with different codes
  4. Try to get back more than one result per group/code e.g. https://my-server.com/fhir/Observation/$lastn?max=5&patient=Patient/73577357-7357-7357-7357-3b31c2506538&code=9279-1,8867-4,85354-9

Expected behavior It should return 5 entries (if found) per code

Actual behavior We receive an error that max as parameter is not know. Doing the same request without max gives us the latest Observation for each code :/

{
    "resourceType": "OperationOutcome",
    "text": {
        "status": "generated",
        "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">ERROR</td><td>[]</td><td><pre>HAPI-0524: Unknown search parameter &quot;max&quot; for resource type &quot;Observation&quot;. Valid search parameters for this search are: [_content, _id, _lastUpdated, _profile, _security, _source, _tag, _text, based-on, category, code, code-value-concept, code-value-date, code-value-quantity, code-value-string, combo-code, combo-code-value-concept, combo-code-value-quantity, combo-data-absent-reason, combo-value-concept, combo-value-quantity, component-code, component-code-value-concept, component-code-value-quantity, component-data-absent-reason, component-value-concept, component-value-quantity, data-absent-reason, date, derived-from, device, encounter, focus, has-member, identifier, method, part-of, patient, performer, specimen, status, subject, value-concept, value-date, value-quantity, value-string]</pre></td>\n\t\t\t</tr>\n\t\t</table>\n\t</div>"
    },
    "issue": [
        {
            "severity": "error",
            "code": "processing",
            "diagnostics": "HAPI-0524: Unknown search parameter \"max\" for resource type \"Observation\". Valid search parameters for this search are: [_content, _id, _lastUpdated, _profile, _security, _source, _tag, _text, based-on, category, code, code-value-concept, code-value-date, code-value-quantity, code-value-string, combo-code, combo-code-value-concept, combo-code-value-quantity, combo-data-absent-reason, combo-value-concept, combo-value-quantity, component-code, component-code-value-concept, component-code-value-quantity, component-data-absent-reason, component-value-concept, component-value-quantity, data-absent-reason, date, derived-from, device, encounter, focus, has-member, identifier, method, part-of, patient, performer, specimen, status, subject, value-concept, value-date, value-quantity, value-string]"
        }
    ]
}

Environment (please complete the following information):

  • 6.1.0 [R5 Context]
  • OS: Ubuntu 22.04
  • Postman

Additional context Add any other context about the problem here.

ZuSe avatar Sep 02 '22 13:09 ZuSe

Hi @ZuSe, I am running into the same issue. It seems that HapiFhir is matching the $lastn search parameters against the defined search parameters of the Observation resource. Of course max is not a defined search parameter for the normal Observation search request.

The actual code where this goes wrong can be found in the hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/provider/BaseJpaResourceProviderObservation.java file. A small snippet of the code in question.

if (theMax != null) {
  paramMap.setLastNMax(theMax.getValue());
}

...

getDao().translateRawParameters(theAdditionalRawParams, paramMap);

The translateRawParameters will get the theAdditionalRawParams value which includes the max parameter which is not defined as a search parameter for the Observation resource. In theory, the following fix could avoid this error by adding the following line of code:

if (theMax != null) {
  paramMap.setLastNMax(theMax.getValue());
  theAdditionalRawParams.remove("max"); // Possible fix to skip the validation
}

...

getDao().translateRawParameters(theAdditionalRawParams, paramMap);

It will skip the translateRawParameters function for the max parameter, but this should be fine, as the max parameter is just an integer with defined bounds as an operation a little bit above the snippet just sent. For reference, this looks like this:

@Description(shortDefinition="The maximum number of observations to return for each observation code")
@OperationParam(name = "max", typeName = "integer", min = 0, max = 1)
	IPrimitiveType<Integer> theMax	

I have not yet made any contributions to hapi-fhir, so I will see if I can verify this locally and potentially create a PR. Wondering what your view is on these findings.

gijsgroenewegen avatar Feb 21 '24 21:02 gijsgroenewegen