diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/SearchPreferHandlingInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/SearchPreferHandlingInterceptor.java index d4dab8ed04b..d3192c91d8f 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/SearchPreferHandlingInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/SearchPreferHandlingInterceptor.java @@ -82,6 +82,12 @@ public class SearchPreferHandlingInterceptor { return; } + String resourceName = theRequestDetails.getResourceName(); + if (!theRequestDetails.getFhirContext().getResourceTypes().contains(resourceName)) { + // This is an error. Let the server handle it normally. + return; + } + String preferHeader = theRequestDetails.getHeader(Constants.HEADER_PREFER); PreferHandlingEnum handling = null; if (isNotBlank(preferHeader)) { diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/provider/ServerCapabilityStatementProvider.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/provider/ServerCapabilityStatementProvider.java index 51b688771af..8f6c19a86ed 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/provider/ServerCapabilityStatementProvider.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/provider/ServerCapabilityStatementProvider.java @@ -385,6 +385,7 @@ public class ServerCapabilityStatementProvider implements IServerConformanceProv List allStructureDefinitions = myValidationSupport.fetchAllNonBaseStructureDefinitions(); if (allStructureDefinitions != null) { for (IBaseResource next : allStructureDefinitions) { + String id = next.getIdElement().getValue(); String kind = terser.getSinglePrimitiveValueOrNull(next, "kind"); String url = terser.getSinglePrimitiveValueOrNull(next, "url"); String baseDefinition = defaultString(terser.getSinglePrimitiveValueOrNull(next, "baseDefinition")); @@ -397,7 +398,12 @@ public class ServerCapabilityStatementProvider implements IServerConformanceProv String resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path"); if (isBlank(resourceType)) { - next = myValidationSupport.generateSnapshot(new ValidationSupportContext(myValidationSupport), next, null, null, null); + try { + next = myValidationSupport.generateSnapshot(new ValidationSupportContext(myValidationSupport), next, null, null, null); + } catch (Exception e) { + ourLog.warn("Failure while generating snapshot for StructureDefinition with URL[{}] ID[{}]", url, id, e); + continue; + } if (next != null) { resourceType = terser.getSinglePrimitiveValueOrNull(next, "snapshot.element.path"); } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchPreferHandlingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchPreferHandlingInterceptorTest.java index 012b2e30c76..53718bc6c2d 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchPreferHandlingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchPreferHandlingInterceptorTest.java @@ -14,6 +14,11 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.interceptor.SearchPreferHandlingInterceptor; import ca.uhn.fhir.test.utilities.server.RestfulServerExtension; +import org.apache.commons.io.IOUtils; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.HumanName; @@ -22,6 +27,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -67,6 +74,17 @@ public class SearchPreferHandlingInterceptorTest { } + @Test + public void testSearchWithUnknownResourceType() throws IOException { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + try (CloseableHttpResponse result = client.execute(new HttpGet("http://localhost:" + myPort + "/BadResource?foo=bar"))) { + assertEquals(404, result.getStatusLine().getStatusCode()); + String response = IOUtils.toString(result.getEntity().getContent(), StandardCharsets.UTF_8); + assertThat(response, containsString("Unknown resource type 'BadResource' - Server knows how to handle: [Patient, OperationDefinition]")); + } + } + } + @Test public void testSearchWithInvalidParam_StrictHeader() { try {