From a824ba263bf6f99dbb90f697605df74413ff1b74 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Sat, 22 Oct 2022 12:16:27 -0400 Subject: [PATCH] package loader npe (#4186) * begin with failing test * fixed * change log * change log Co-authored-by: Ken Stevens --- .../6_2_0/4185-packageloader-npe.yaml | 6 +++ .../fhir/jpa/dao/BaseHapiFhirResourceDao.java | 3 +- .../jpa/packages/PackageInstallerSvcImpl.java | 4 +- ...ageInstallerSvcImplRewriteHistoryTest.java | 48 +++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4185-packageloader-npe.yaml create mode 100644 hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplRewriteHistoryTest.java diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4185-packageloader-npe.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4185-packageloader-npe.yaml new file mode 100644 index 00000000000..b69eac476d0 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4185-packageloader-npe.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 4185 +jira: SMILE-5340 +title: "When DaoConfig UpdateWithHistoryRewriteEnabled is enabled, the package loader throws a NullPointerException. +This has been corrected." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java index e10ba2de5ec..b1dcb49d8b7 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java @@ -114,7 +114,6 @@ import ca.uhn.fhir.validation.IValidatorModule; import ca.uhn.fhir.validation.ValidationOptions; import ca.uhn.fhir.validation.ValidationResult; import com.google.common.annotations.VisibleForTesting; -import com.google.gson.Gson; import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseMetaType; @@ -1686,7 +1685,7 @@ public abstract class BaseHapiFhirResourceDao extends B Runnable onRollback = () -> theResource.getIdElement().setValue(id); // Execute the update in a retryable transaction - if (myDaoConfig.isUpdateWithHistoryRewriteEnabled() && theRequest.isRewriteHistory()) { + if (myDaoConfig.isUpdateWithHistoryRewriteEnabled() && theRequest != null && theRequest.isRewriteHistory()) { return myTransactionService.execute(theRequest, theTransactionDetails, tx -> doUpdateWithHistoryRewrite(theResource, theRequest, theTransactionDetails), onRollback); } else { return myTransactionService.execute(theRequest, theTransactionDetails, tx -> doUpdate(theResource, theMatchUrl, thePerformIndexing, theForceUpdateVersion, theRequest, theTransactionDetails), onRollback); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java index d9b8cba3508..5918f00accf 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImpl.java @@ -393,12 +393,12 @@ public class PackageInstallerSvcImpl implements IPackageInstallerSvc { } } - private DaoMethodOutcome updateResource(IFhirResourceDao theDao, IBaseResource theResource) { + DaoMethodOutcome updateResource(IFhirResourceDao theDao, IBaseResource theResource) { if (myPartitionSettings.isPartitioningEnabled()) { SystemRequestDetails requestDetails = newSystemRequestDetails(); return theDao.update(theResource, requestDetails); } else { - return theDao.update(theResource); + return theDao.update(theResource, new SystemRequestDetails()); } } diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplRewriteHistoryTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplRewriteHistoryTest.java new file mode 100644 index 00000000000..a97a46d43fb --- /dev/null +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/packages/PackageInstallerSvcImplRewriteHistoryTest.java @@ -0,0 +1,48 @@ +package ca.uhn.fhir.jpa.packages; + +import ca.uhn.fhir.jpa.test.BaseJpaR4Test; +import ca.uhn.fhir.model.primitive.IdDt; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.ConceptMap; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class PackageInstallerSvcImplRewriteHistoryTest extends BaseJpaR4Test { + public static final IIdType CONCEPT_MAP_TEST_ID = new IdDt("ConceptMap/PackageInstallerSvcImplRewriteHistoryTest"); + @Autowired + PackageInstallerSvcImpl mySvc; + + + @AfterEach + void after() { + myDaoConfig.setUpdateWithHistoryRewriteEnabled(false); + } + + @Test + void svc_notnull() { + assertNotNull(mySvc); + } + + @Test + void updateWithHistoryRewriteEnabled() { + // setup + myDaoConfig.setUpdateWithHistoryRewriteEnabled(true); + + ConceptMap conceptMap = new ConceptMap(); + conceptMap.setId(CONCEPT_MAP_TEST_ID); + conceptMap.setUrl("http://example.com/ConceptMap/testcm"); + + // execute + // red-green this threw a NPE before the fix + mySvc.updateResource(myConceptMapDao, conceptMap); + + // verify + ConceptMap readConceptMap = myConceptMapDao.read(CONCEPT_MAP_TEST_ID); + assertEquals(CONCEPT_MAP_TEST_ID.toString(), readConceptMap.getIdElement().toVersionless().toString()); + + } +}