Fix error message for HAPI-0302 showing misleading names for known resource types (#5875)

* A solution, but perhaps not the best one

* Make behavior more robust and extendable. Fixes #5874

* Apply spotless

* Add changelog

* 😅

* Add unit test

* Get name from context instead of hard-coding remapping
This commit is contained in:
holly-smile 2024-04-25 13:39:10 -07:00 committed by GitHub
parent 90201095c4
commit 72ff192df8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
type: fix
issue: 5874
jira: SMILE-8149
title: "Fixed a bug where 'List' would be incorrectly shown as 'ListResource' in the error response for a GET for an invalid resource."

View File

@ -1990,7 +1990,8 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
/* perform a 'distinct' in case there are multiple concrete IResourceProviders declared for the same FHIR-Resource. (A concrete IResourceProvider for Patient@Read and a separate concrete for Patient@Search for example */
/* perform a 'sort' to provide an easier to read alphabetized list (vs how the different FHIR-resource IResourceProviders happened to be registered */
List<String> knownDistinctAndSortedResourceTypes = myResourceProviders.stream()
.map(t -> t.getResourceType().getSimpleName())
.map(t ->
myFhirContext.getResourceDefinition(t.getResourceType()).getName())
.distinct()
.sorted()
.collect(toList());

View File

@ -10,13 +10,17 @@ import ca.uhn.fhir.rest.annotation.Metadata;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.api.server.IFhirVersionServer;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseConformance;
import org.hl7.fhir.instance.model.api.IBaseMetaType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.ListResource;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -88,6 +92,20 @@ public class RestfulServerTest {
}
}
@Test
public void exceptionHandling() {
final SystemRequestDetails requestDetails = new SystemRequestDetails();
requestDetails.setRequestType(RequestTypeEnum.GET);
requestDetails.setResourceName("InvalidResourceName");
myRestfulServer.registerProvider(new MyListResourceProvider());
ResourceNotFoundException thrown = assertThrows(
ResourceNotFoundException.class,
() -> myRestfulServer.determineResourceMethod(requestDetails, "1234"),
"Expected request to fail, but it succeeded.");
assertTrue(thrown.getMessage().contains("List"));
assertFalse(thrown.getMessage().contains("ListResource"));
}
//--------- Scaffolding ---------//
private static class MyClassWithoutRestInterface implements Serializable {
@ -141,6 +159,18 @@ public class RestfulServerTest {
}
}
private static class MyListResourceProvider implements IResourceProvider {
@Create
public MethodOutcome create(@ResourceParam IBaseResource theResource) {
return mock(MethodOutcome.class);
}
@Override
public Class<? extends IBaseResource> getResourceType() {
return ListResource.class;
}
}
private static class MyProvider implements IResourceProvider {
@Operation(name = "SHOW_ME_THE_MONEY", typeName = "MyResource")
public IBaseBundle match() {