diff --git a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties index 02e641d515b..017c0a5650b 100644 --- a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties +++ b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties @@ -103,6 +103,8 @@ ca.uhn.fhir.jpa.dao.BaseStorageDao.unableToDeleteNotFound=Unable to find resourc ca.uhn.fhir.jpa.dao.BaseStorageDao.successfulCreate=Successfully created resource "{0}" in {1}ms ca.uhn.fhir.jpa.dao.BaseStorageDao.successfulUpdate=Successfully updated resource "{0}" in {1}ms ca.uhn.fhir.jpa.dao.BaseStorageDao.successfulDeletes=Successfully deleted {0} resource(s) in {1}ms +ca.uhn.fhir.jpa.dao.BaseStorageDao.deleteResourceNotExisting=Not deleted, resource {0} does not exist. +ca.uhn.fhir.jpa.dao.BaseStorageDao.deleteResourceAlreadyDeleted=Not deleted, resource {0} was already deleted. ca.uhn.fhir.jpa.dao.BaseStorageDao.invalidSearchParameter=Unknown search parameter "{0}" for resource type "{1}". Valid search parameters for this search are: {2} ca.uhn.fhir.jpa.dao.BaseStorageDao.invalidSortParameter=Unknown _sort parameter value "{0}" for resource type "{1}" (Note: sort parameters values must use a valid Search Parameter). Valid values for this search are: {2} ca.uhn.fhir.jpa.dao.BaseStorageDao.updateWithNoId=Can not update resource of type {0} as it has no ID diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3897-deleting-non-exist-resources-will-provide-a-response-successfully-deleted.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3897-deleting-non-exist-resources-will-provide-a-response-successfully-deleted.yaml new file mode 100644 index 00000000000..4871a6b6aca --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3897-deleting-non-exist-resources-will-provide-a-response-successfully-deleted.yaml @@ -0,0 +1,4 @@ +--- +type: fix +issue: 3897 +title: "Providing a meaningful response message when deleting non-existing or already deleted resources." 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 6fa91281ad0..52a3c95ca28 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 @@ -513,7 +513,7 @@ public abstract class BaseHapiFhirResourceDao extends B * * @param theId - the id of the object being deleted. Eg: Patient/123 */ - private DaoMethodOutcome createMethodOutcomeForDelete(String theId) { + private DaoMethodOutcome createMethodOutcomeForDelete(String theId, String theKey) { DaoMethodOutcome outcome = new DaoMethodOutcome(); IIdType id = getContext().getVersion().newIdType(); @@ -521,7 +521,7 @@ public abstract class BaseHapiFhirResourceDao extends B outcome.setId(id); IBaseOperationOutcome oo = OperationOutcomeUtil.newInstance(getContext()); - String message = getContext().getLocalizer().getMessage(BaseStorageDao.class, "successfulDeletes", 1, 0); + String message = getContext().getLocalizer().getMessage(BaseStorageDao.class, theKey, id); String severity = "information"; String code = "informational"; OperationOutcomeUtil.addIssue(getContext(), oo, severity, message, null, code); @@ -546,7 +546,7 @@ public abstract class BaseHapiFhirResourceDao extends B // if not found, return an outcome anyways. // Because no object actually existed, we'll // just set the id and nothing else - DaoMethodOutcome outcome = createMethodOutcomeForDelete(theId.getValue()); + DaoMethodOutcome outcome = createMethodOutcomeForDelete(theId.getValue(), "deleteResourceNotExisting"); return outcome; } @@ -556,7 +556,7 @@ public abstract class BaseHapiFhirResourceDao extends B // Don't delete again if it's already deleted if (isDeleted(entity)) { - DaoMethodOutcome outcome = createMethodOutcomeForDelete(entity.getIdDt().getValue()); + DaoMethodOutcome outcome = createMethodOutcomeForDelete(entity.getIdDt().getValue(), "deleteResourceAlreadyDeleted"); // used to exist, so we'll set the persistent id outcome.setPersistentId(new ResourcePersistentId(entity.getResourceId())); diff --git a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java index 483ba3a3825..7b1e12f7595 100644 --- a/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java +++ b/hapi-fhir-jpaserver-test-utilities/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java @@ -1737,6 +1737,32 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { assertThat(oo.getIssueFirstRep().getDiagnostics(), startsWith("Successfully deleted 1 resource(s) in ")); } + @Test + public void testDeleteNonExistingResourceReturnsOperationOutcome() { + String resourceType = "Patient"; + String logicalID = "12345"; + + MethodOutcome resp = myClient.delete().resourceById(resourceType, logicalID).execute(); + + OperationOutcome oo = (OperationOutcome) resp.getOperationOutcome(); + assertThat(oo.getIssueFirstRep().getDiagnostics(), startsWith("Not deleted, resource " + resourceType + "/" + logicalID + " does not exist.")); + } + + @Test + public void testDeleteAlreadyDeletedReturnsOperationOutcome() { + Patient p = new Patient(); + IIdType id = myClient.create().resource(p).execute().getId(); + + MethodOutcome resp = myClient.delete().resourceById(id).execute(); + OperationOutcome oo = (OperationOutcome) resp.getOperationOutcome(); + assertThat(oo.getIssueFirstRep().getDiagnostics(), startsWith("Successfully deleted 1 resource(s) in ")); + + resp = myClient.delete().resourceById(id).execute(); + oo = (OperationOutcome) resp.getOperationOutcome(); + assertThat(oo.getIssueFirstRep().getDiagnostics(), startsWith("Not deleted, resource ")); + assertThat(oo.getIssueFirstRep().getDiagnostics(), endsWith("was already deleted.")); + } + /** * See issue #52 */