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

Extensions in Questionnaire are not validated (using Instance Validator)

Open Harown opened this issue 11 months ago • 0 comments

Describe the bug Validating a QuestionnaireResponse against a Questionnaire using the Instance Validator should consider basic extensions like http://hl7.org/fhir/StructureDefinition/maxValue. Validation seems to ignore it even when setAnyExtensionsAllowed is set to true.

To Reproduce Minimal reproduction code:

public static void main(String[] args) {
        var jsonQuestionnaire = """
            {
              "resourceType": "Questionnaire",
              "url": "http://example.com/q",
              "item": [
                {
                     "linkId": "E_GAF",
                     "text": "Global assessment of functioning",
                     "type": "integer",
                     "required": true,
                     "extension": [
                         {
                             "url": "http://hl7.org/fhir/StructureDefinition/maxValue",
                             "valueInteger": 100
                         }
                     ]
                }
              ]
            }""";

        var jsonResponse = """ 
            {
              "resourceType": "QuestionnaireResponse",
              "questionnaire": "http://example.com/q",
              "status": "completed",
              "item": [
                {
                    "answer": [
                        {
                            "valueInteger": 101
                        }
                    ],
                    "linkId": "E_GAF",
                    "text": "Global assessment of functioning"
                }
              ]
            }""";

        var context = FhirContext.forR4();
        var parser = context.newJsonParser();
        var questionnaire = (Questionnaire) parser.parseResource(jsonQuestionnaire);
        var response = (QuestionnaireResponse) parser.parseResource(jsonResponse);

        var validationSupportChain = new ValidationSupportChain(
                new QuestionnaireValidationSupport(context, questionnaire),
                new InMemoryTerminologyServerValidationSupport(context),
                new DefaultProfileValidationSupport(context),
                new CommonCodeSystemsTerminologyService(context),
                new UnknownCodeSystemWarningValidationSupport(context));

        var validator = context.newValidator();
        var instanceValidator = new FhirInstanceValidator(validationSupportChain);
        validator.registerValidatorModule(instanceValidator);

        instanceValidator.setAnyExtensionsAllowed(true);

        var result = validator.validateWithResult(response);
        result.getMessages().forEach(System.out::println);
    }

QuestionnaireValidationSupport is a class that stems from https://groups.google.com/g/hapi-fhir/c/IVtC4LMAZSQ:

public class QuestionnaireValidationSupport extends BaseStaticResourceValidationSupport {
    private final Questionnaire questionnaire;

    protected QuestionnaireValidationSupport(FhirContext theFhirContext, Questionnaire questionnaire) {
        super(theFhirContext);
        this.questionnaire = questionnaire;
    }

    @Nullable
    @Override
    public <T extends IBaseResource> T fetchResource(@Nullable Class<T> theClass, String theUri) {
        Validate.notBlank(theUri, "theUri must not be null or blank");
        var type = this.getFhirContext().getResourceType(theClass);
        if (type.equals("Questionnaire")) {
            return (T) questionnaire;
        } else {
            return null;
        }
    }
}

Expected behavior Get a validation error: answer 101 should not be allowed when maxValue is 100.

Actual behavior Got no validation error. Omitting the answer for a required field results in an expected validation error. So basic validation works but not the validation of extensions.

Environment (please complete the following information):

  • HAPI FHIR Version 7.0.0 (Maven)
  • OS: Windows, JDK 17

Harown avatar Feb 29 '24 13:02 Harown