hapifhir / hapi-fhir

šŸ”„ HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2.02k stars 1.32k forks source link

Error when using chained parameters in conditional references #6316

Open KeironO opened 3 weeks ago

KeironO commented 3 weeks ago

Describe the bug We are attempting to use chained parameters in match URLs within a FHIR resource (e.g., PractitionerRole?practitioner.identifier=myValue), but HAPI FHIR is returning an error indicating that the query part of the conditional reference is not valid.

To Reproduce Steps to reproduce the behavior:

  1. Create a FHIR resource, such as a Communication, that includes a reference with a chained parameter for conditional matching.
  2. Use a reference in the format PractitionerRole?practitioner.identifier=myValue.
  3. POST the resource to the HAPI FHIR server.
  4. Observe the error returned by the server.

Minimal Example

This is a minimal Communication resource that demonstrates the issue:

{
  "resourceType": "Communication",
  "status": "completed",
  "sender": { "reference": "PractitionerRole?practitioner.identifier=myValue" }
}

Expected behavior We expected the server to correctly process the chained parameter in the match URL, resolving the reference as PractitionerRole where the associated practitioner.identifier matches the given value (myValue).

Error Response

The server returns the following error:

Error
Line: 4, Col: 78
The query part of the conditional reference is not a valid query string (PractitionerRole?practitioner.identifier=myValue)

Environment (please complete the following information):

Additional context We are trying to use chained parameters to reference resources based on identifiers, which should be valid according to FHIR chaining rules. The issue may be related to how the query part of the reference is parsed and validated.

KeironO commented 3 weeks ago

I've double checked, and it looks to be an issue with the Validator.

package wales.nhs.ctmuhb;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.validation.*;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.common.hapi.validation.validator.*;
import org.hl7.fhir.common.hapi.validation.support.*;
import ca.uhn.fhir.context.support.*;

public class Main {
    public static void main(String[] args) {

        FhirContext ctx = FhirContext.forR4();

        FhirValidator validator = ctx.newValidator();

        ValidationSupportChain validationSupportChain = new ValidationSupportChain(
                new DefaultProfileValidationSupport(ctx),
                new InMemoryTerminologyServerValidationSupport(ctx),
                new CommonCodeSystemsTerminologyService(ctx),
                new SnapshotGeneratingValidationSupport(ctx));

        FhirInstanceValidator instanceValidator = new FhirInstanceValidator(validationSupportChain);
        validator.registerValidatorModule(instanceValidator);

        Communication communication = new Communication();
        communication.setStatus(Communication.CommunicationStatus.COMPLETED);
        communication.setSender(new Reference("PractitionerRole?practitioner.identifier=myValue"));

        ValidationResult result = validator.validateWithResult(communication);

        if (result.isSuccessful()) {
            System.out.println("Validation passed");
        } else {
            System.out.println("Validation failed");
            result.getMessages().forEach(msg -> System.out.println(msg.getLocationString() + " " + msg.getMessage()));
        }
    }
}

Returns:

Validation failed
Communication.sender The query part of the conditional reference is not a valid query string (PractitionerRole?practitioner.identifier=myValue)