Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fhirpath toString function not to spec #1794

Open
brianpos opened this issue Oct 30, 2024 · 2 comments
Open

Fhirpath toString function not to spec #1794

brianpos opened this issue Oct 30, 2024 · 2 comments
Labels
bug Something isn't working fhirpath

Comments

@brianpos
Copy link
Contributor

  • Returns the text "null" when it encounters a primitive datatype that has no value and just an extension
  • Returns a comma seperated list of values when run on a collection (spec says to throw an error)

Tracker to clarify behaviour in the core spec here:
https://jira.hl7.org/browse/FHIR-48737

@brianpos
Copy link
Contributor Author

Unit tests for the expected result: (will also add a new one to the fhirpath test suite...)

  @Test
  public void testEvaluate_ToStringOnDateValue() {
    Patient input = new Patient();
    var dtv = new DateType("2024");
    input.setBirthDateElement(dtv);
    List<Base> results = fp.evaluate(input, "Patient.birthDate.toString()");
    assertEquals(1, results.size());
    assertEquals("2024", results.get(0).toString());
  }

  @Test
  public void testEvaluate_ToStringOnExtensionOnlyValue() {
    Patient input = new Patient();
    var dtv = new DateType();
    input.setBirthDateElement(dtv);
    List<Base> results = fp.evaluate(input, "Patient.birthDate.toString()");
    assertEquals(0, results.size());
  }

@brianpos
Copy link
Contributor Author

And the actual implementation to resolve this issue: (assuming that the empty set/exception are the correct results)

  private List<Base> funcToString(ExecutionContext context, List<Base> focus, ExpressionNode exp) {
    List<Base> result = new ArrayList<Base>();
    for (Base item : focus) {
      String value = convertToString(item);
      if (value != null)
        result.add(new StringType(value).noExtensions());
    }

    if (result.size() > 1) {
      throw makeException(exp, I18nConstants.FHIRPATH_NO_COLLECTION, "toString", result.size());
    }
    return result;
  }

And should be cloned in R4, R4B and R5
(happy to create the PR if this is all good)

Have tested locally

brianpos added a commit to brianpos/org.hl7.fhir.core that referenced this issue Oct 30, 2024
Update the implementation of the `toString` fhirpath function to: (pending FHIR-48737)
* not return the text 'null' when no primitive value exists (when extension only)
* throw an error if more than 1 item is to be returned (doesn't support collections)
* return an empty set when no primitive values exist (when extension only)
@dotasek dotasek added bug Something isn't working fhirpath labels Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fhirpath
Projects
None yet
Development

No branches or pull requests

2 participants