hapifhir / hapi-fhir

🔥 HAPI FHIR - Java API for HL7 FHIR Clients and Servers
http://hapifhir.io
Apache License 2.0
2.03k stars 1.33k forks source link

Constraints applied for CareTeam.participant.onBehalfOf even if member is a Practitioner #1037

Open iordachemc opened 6 years ago

iordachemc commented 6 years ago

Describe the bug Constraints applies to all values ctm-1: On CareTeam.participant: CareTeam.participant.onBehalfOf can only be populated when CareTeam.participant.member is a Practitioner (expression on CareTeam.participant: onBehalfOf.exists() implies (member.resolve() is Practitioner))

To Reproduce Steps to reproduce the behavior:

  1. Create PUT operation for CareTeam
  2. In body add CareTeam.participant.member with value Practitioner
  3. In body add CareTeam.participant.onBehalfOf with value Organization
  4. Execute PUT operation
  5. Returns a status of 422 -Unprocessable Entity and constraint error message

Expected behavior Should return a Successful created message

Environment (please complete the following information):

Timbo925 commented 5 years ago

I noticed the same problem while validating custom resources. Wrote a small unit test to reproduce the problem with the default HAPI FHIR resources and the default validator.

To Reproduce

@Test
public void validateFHIRResource() {
    final org.hl7.fhir.dstu3.model.CareTeam careTeam = new org.hl7.fhir.dstu3.model.CareTeam();
    careTeam.addParticipant(new org.hl7.fhir.dstu3.model.CareTeam.CareTeamParticipantComponent() //
            .setMember(new Reference(new Practitioner())) //
            .setOnBehalfOf(new Reference(new Organization().setName("Organizer"))));

    final FhirContext ctx = FhirContext.forDstu3();
    final FhirValidator validator = ctx.newValidator();
    final FhirInstanceValidator instanceValidator = new FhirInstanceValidator();
        ValidationSupportChain support = new ValidationSupportChain(new DefaultProfileValidationSupport());
    instanceValidator.setValidationSupport(support);
    validator.registerValidatorModule(instanceValidator);

    final ValidationResult result = validator.validateWithResult(careTeam);

    result.getMessages().forEach(v -> logger.info("validateResource - " + v.toString()));
    MatcherAssert.assertThat(result.isSuccessful(), equalTo(true));
    MatcherAssert.assertThat(result.getMessages().size(), equalTo(0));
}

Environment: