Avoid a crash reading an unknown ID on partitioned server (#2451)

* Avoid a crash reading an unknown ID on partitioned server. Fix #1953.

* Attempt 2 at fixing #1953
This commit is contained in:
James Agnew 2021-03-07 10:16:30 -05:00 committed by GitHub
parent eb3c155598
commit 62df320ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
type: fix
issue: 1953
title: "A crash was fixed when performing a FHIR read on a partitioned server, where the requested ID is not known. Thanks
to Umberto Cappellini for reporting!"

View File

@ -1149,7 +1149,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
BaseHasResource entity = myEntityManager.find(ResourceTable.class, pid.getIdAsLong()); BaseHasResource entity = myEntityManager.find(ResourceTable.class, pid.getIdAsLong());
// Verify that the resource is for the correct partition // Verify that the resource is for the correct partition
if (!requestPartitionId.isAllPartitions()) { if (entity != null && !requestPartitionId.isAllPartitions()) {
if (entity.getPartitionId() != null && entity.getPartitionId().getPartitionId() != null) { if (entity.getPartitionId() != null && entity.getPartitionId().getPartitionId() != null) {
if (!requestPartitionId.hasPartitionId(entity.getPartitionId().getPartitionId())) { if (!requestPartitionId.hasPartitionId(entity.getPartitionId().getPartitionId())) {
ourLog.debug("Performing a read for PartitionId={} but entity has partition: {}", requestPartitionId, entity.getPartitionId()); ourLog.debug("Performing a read for PartitionId={} but entity has partition: {}", requestPartitionId, entity.getPartitionId());

View File

@ -975,6 +975,58 @@ public class PartitioningSqlR4Test extends BasePartitioningR4Test {
} }
} }
@Test
public void testRead_PidId_UnknownResourceId() {
// Read in specific Partition
{
addReadPartition(1);
try {
myPatientDao.read(new IdType("Patient/1"), mySrd);
fail();
} catch (ResourceNotFoundException e) {
// expected
}
}
// Read in null Partition
{
addReadDefaultPartition();
try {
myPatientDao.read(new IdType("Patient/1"), mySrd);
fail();
} catch (ResourceNotFoundException e) {
// expected
}
}
}
@Test
public void testRead_PidId_ResourceIdOnlyExistsInDifferentPartition() {
IIdType id = createPatient(withPartition(2), withActiveTrue());
// Read in specific Partition
{
addReadPartition(1);
try {
myPatientDao.read(id, mySrd);
fail();
} catch (ResourceNotFoundException e) {
// expected
}
}
// Read in null Partition
{
addReadDefaultPartition();
try {
myPatientDao.read(id, mySrd);
fail();
} catch (ResourceNotFoundException e) {
// expected
}
}
}
@Test @Test
public void testRead_ForcedId_SpecificPartition() { public void testRead_ForcedId_SpecificPartition() {
IIdType patientIdNull = createPatient(withPutPartition(null), withActiveTrue(), withId("NULL")); IIdType patientIdNull = createPatient(withPutPartition(null), withActiveTrue(), withId("NULL"));