Fix #1299 - Return an HTTP 201 when creating a previously deleted

resource
This commit is contained in:
James Agnew 2019-05-06 16:33:23 -04:00
parent 61902da527
commit 702ffd631d
3 changed files with 34 additions and 5 deletions

View File

@ -1266,7 +1266,6 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
IIdType resourceId;
if (isNotBlank(theMatchUrl)) {
StopWatch sw = new StopWatch();
Set<Long> match = myMatchResourceUrlService.processMatchUrl(theMatchUrl, myResourceType);
if (match.size() > 1) {
String msg = getContext().getLocalizer().getMessage(BaseHapiFhirDao.class, "transactionOperationWithMultipleMatchFailure", "UPDATE", theMatchUrl, match.size());
@ -1315,6 +1314,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
* See SystemProviderR4Test#testTransactionReSavesPreviouslyDeletedResources
* for a test that needs this.
*/
boolean wasDeleted = entity.getDeleted() != null;
entity.setDeleted(null);
/*
@ -1325,7 +1325,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
*/
if (!thePerformIndexing) {
theResource.setId(entity.getIdDt().getValue());
DaoMethodOutcome outcome = toMethodOutcome(theRequestDetails, entity, theResource).setCreated(false);
DaoMethodOutcome outcome = toMethodOutcome(theRequestDetails, entity, theResource).setCreated(wasDeleted);
outcome.setPreviousResource(oldResource);
return outcome;
}
@ -1334,7 +1334,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
* Otherwise, we're not in a transaction
*/
ResourceTable savedEntity = updateInternal(theRequestDetails, theResource, thePerformIndexing, theForceUpdateVersion, entity, resourceId, oldResource);
DaoMethodOutcome outcome = toMethodOutcome(theRequestDetails, savedEntity, theResource).setCreated(false);
DaoMethodOutcome outcome = toMethodOutcome(theRequestDetails, savedEntity, theResource).setCreated(wasDeleted);
if (!thePerformIndexing) {
outcome.setId(theResource.getIdElement());

View File

@ -393,12 +393,12 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
@Test
public void testCreateWithClientAssignedId() throws IOException {
// Create with client assigned ID
Patient p = new Patient();
p.setActive(true);
p.setId("AAA");
String encoded = myFhirCtx.newJsonParser().encodeResourceToString(p);
HttpPut httpPut = new HttpPut(ourServerBase + "/Patient/AAA");
httpPut.setEntity(new StringEntity(encoded, ContentType.parse("application/json+fhir")));
try (CloseableHttpResponse status = ourHttpClient.execute(httpPut)) {
@ -410,6 +410,30 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
assertThat(responseContent, containsString("true"));
}
// Delete
HttpDelete httpDelete = new HttpDelete(ourServerBase + "/Patient/AAA");
try (CloseableHttpResponse status = ourHttpClient.execute(httpDelete)) {
assertEquals(200, status.getStatusLine().getStatusCode());
}
// Create it again
p = new Patient();
p.setActive(true);
p.setId("AAA");
encoded = myFhirCtx.newJsonParser().encodeResourceToString(p);
httpPut = new HttpPut(ourServerBase + "/Patient/AAA");
httpPut.setEntity(new StringEntity(encoded, ContentType.parse("application/json+fhir")));
try (CloseableHttpResponse status = ourHttpClient.execute(httpPut)) {
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
ourLog.info(status.getStatusLine().toString());
ourLog.info(responseContent);
assertEquals(201, status.getStatusLine().getStatusCode());
assertThat(responseContent, containsString("true"));
}
}

View File

@ -208,6 +208,11 @@
calling fetchCodeSystem() in order to reduce the work required by chain entries. Thanks to
Anders Havn for the suggestion!
</action>
<action type="fix" issue="1299">
In JPA server when updating a resource using a client assigned ID, if the resource was previously
deleted (meaning that the operation is actually a create), the server will now return
an HTTP 201 instead of an HTTP 200. Thanks to Mario Hyland for reporting!
</action>
</release>
<release version="3.7.0" date="2019-02-06" description="Gale">
<action type="add">