diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3275-fix-patient-id-partition-interceptor-issue.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3275-fix-patient-id-partition-interceptor-issue.yaml new file mode 100644 index 00000000000..ab5eba52965 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/5_7_0/3275-fix-patient-id-partition-interceptor-issue.yaml @@ -0,0 +1,5 @@ +--- +type: fix +issue: 3275 +title: "In rare cases where patient ID String happened to have hashCode equal to Integer.MIN_VALUE, + PatientIdPartitionInterceptor would generate an invalid partition ID. This has been fixed." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptor.java index 8a8b6121cd3..02d0f976908 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptor.java @@ -236,7 +236,7 @@ public class PatientIdPartitionInterceptor { */ @SuppressWarnings("unused") protected int providePartitionIdForPatientId(RequestDetails theRequestDetails, String theResourceIdPart) { - return Math.abs(theResourceIdPart.hashCode()) % 15000; + return Math.abs(theResourceIdPart.hashCode() % 15000); } /** diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptorTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptorTest.java index 238fe66f179..08f6cd79b2d 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptorTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/interceptor/PatientIdPartitionInterceptorTest.java @@ -91,6 +91,23 @@ public class PatientIdPartitionInterceptorTest extends BaseJpaR4SystemTest { }); } + /** + * This is an edge case where a client assigned ID has a Java hashCode equal to Integer.MIN_VALUE. + */ + @Test + public void testCreatePatient_polygenelubricants() { + Patient patient = new Patient(); + patient.setId("Patient/polygenelubricants"); + patient.setActive(true); + DaoMethodOutcome update = myPatientDao.update(patient); + + runInTransaction(() -> { + ResourceTable pt = myResourceTableDao.findAll().iterator().next(); + assertEquals("polygenelubricants", pt.getIdDt().getIdPart()); + assertEquals(8648, pt.getPartitionId().getPartitionId()); + }); + } + @Test public void testCreatePatient_NonClientAssignedId() { Patient patient = new Patient();