diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java index 034190ef837..62260be1029 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/bulk/provider/BulkDataExportProvider.java @@ -50,7 +50,9 @@ import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class BulkDataExportProvider { @@ -109,10 +111,21 @@ public class BulkDataExportProvider { ) { validatePreferAsyncHeader(theRequestDetails); BulkDataExportOptions bulkDataExportOptions = buildGroupBulkExportOptions(theOutputFormat, theType, theSince, theTypeFilter, theIdParam, theMdm); + validateResourceTypesAllContainPatientSearchParams(bulkDataExportOptions.getResourceTypes()); IBulkDataExportSvc.JobInfo outcome = myBulkDataExportSvc.submitJob(bulkDataExportOptions); writePollingLocationToResponseHeaders(theRequestDetails, outcome); } + private void validateResourceTypesAllContainPatientSearchParams(Set theResourceTypes) { + List badResourceTypes = theResourceTypes.stream() + .filter(resourceType -> !myBulkDataExportSvc.getPatientCompartmentResources().contains(resourceType)) + .collect(Collectors.toList()); + + if (!badResourceTypes.isEmpty()) { + throw new IllegalArgumentException(String.format("Resource types [] are invalid for this type of export, as they do not contain search parameters that refer to patients.", String.join(",", badResourceTypes))); + } + } + /** * Patient/$export */ diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java index 377bec8ba97..40bed8c795f 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/bulk/BulkDataExportProviderTest.java @@ -373,6 +373,26 @@ public class BulkDataExportProviderTest { assertThat(options.getFilters(), containsInAnyOrder(immunizationTypeFilter1, immunizationTypeFilter2, observationFilter1)); } + + @Test + public void testInitiateGroupExportWithInvalidResourceTypesFails() throws IOException { + IBulkDataExportSvc.JobInfo jobInfo = new IBulkDataExportSvc.JobInfo() + .setJobId(A_JOB_ID); + when(myBulkDataExportSvc.submitJob(any())).thenReturn(jobInfo); + + String url = "http://localhost:" + myPort + "/" + JpaConstants.OPERATION_EXPORT + + "?" + JpaConstants.PARAM_EXPORT_OUTPUT_FORMAT + "=" + UrlUtil.escapeUrlParam(Constants.CT_FHIR_NDJSON) + + "&" + JpaConstants.PARAM_EXPORT_TYPE + "=" + UrlUtil.escapeUrlParam("StructureDefinition"); + + HttpGet get = new HttpGet(url); + get.addHeader(Constants.HEADER_PREFER, Constants.HEADER_PREFER_RESPOND_ASYNC); + myClient.execute(get); + + verify(myBulkDataExportSvc, times(1)).submitJob(myBulkDataExportOptionsCaptor.capture()); + BulkDataExportOptions options = myBulkDataExportOptionsCaptor.getValue(); + + } + @Test public void testInitiateWithPostAndMultipleTypeFilters() throws IOException { @@ -419,7 +439,7 @@ public class BulkDataExportProviderTest { Parameters input = new Parameters(); input.addParameter(JpaConstants.PARAM_EXPORT_OUTPUT_FORMAT, new StringType(Constants.CT_FHIR_NDJSON)); - input.addParameter(JpaConstants.PARAM_EXPORT_STYLE, new StringType("Immunization, Observation")); + input.addParameter(JpaConstants.PARAM_EXPORT_TYPE, new StringType("Immunization, Observation")); input.addParameter(JpaConstants.PARAM_EXPORT_SINCE, now); input.addParameter(JpaConstants.PARAM_EXPORT_TYPE_FILTER, new StringType("Immunization?vaccine-code=foo"));