Skip to content

Commit

Permalink
Version bug fix (#3619)
Browse files Browse the repository at this point in the history
* Version bug fix

* version

* fix

* fix

* opt
  • Loading branch information
SergeyGaluzo authored Dec 8, 2023
1 parent e4f0a6d commit 49a9489
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ internal async Task<IDictionary<DataStoreOperationIdentifier, DataStoreOperation
if (!resource.IsDeleted)
{
// check if the new resource data is same as existing resource data
if (ExistingRawResourceIsEqualToInput(resource, existingResource))
if (ExistingRawResourceIsEqualToInput(resource, existingResource, resourceExt.KeepVersion))
{
// Send the existing resource in the response
results.Add(identifier, new DataStoreOperationOutcome(new UpsertOutcome(existingResource, SaveOutcomeType.Updated)));
Expand Down Expand Up @@ -522,26 +522,42 @@ private void ReplaceVersionIdAndLastUpdatedInMeta(ResourceWrapper resourceWrappe
resourceWrapper.RawResource = new RawResource(rawResourceData, FhirResourceFormat.Json, true);
}

private bool ExistingRawResourceIsEqualToInput(ResourceWrapper input, ResourceWrapper existing) // call is not symmetrical, it assumes version = 1 on input.
private bool ExistingRawResourceIsEqualToInput(ResourceWrapper input, ResourceWrapper existing, bool keepVersion)
{
if (!_rawResourceDeduping.IsEnabled())
{
return false;
}

if (keepVersion)
{
return input.RawResource.Data == existing.RawResource.Data;
}

var inputDate = GetJsonValue(input.RawResource.Data, "lastUpdated", false);
var inputVersion = GetJsonValue(input.RawResource.Data, "versionId", true);
var existingDate = GetJsonValue(existing.RawResource.Data, "lastUpdated", true);
var existingVersion = GetJsonValue(existing.RawResource.Data, "versionId", true);
if (existingVersion != InitialVersion)
if (inputVersion == existingVersion)
{
if (inputDate == existingDate)
{
return input.RawResource.Data == existing.RawResource.Data;
}

return input.RawResource.Data == existing.RawResource.Data.Replace($"\"lastUpdated\":\"{existingDate}\"", $"\"lastUpdated\":\"{inputDate}\"", StringComparison.Ordinal);
}
else
{
if (inputDate == existingDate)
{
return input.RawResource.Data == existing.RawResource.Data.Replace($"\"versionId\":\"{existingVersion}\"", $"\"versionId\":\"{inputVersion}\"", StringComparison.Ordinal);
}

return input.RawResource.Data
== existing.RawResource.Data
.Replace($"\"versionId\":\"{existingVersion}\"", $"\"versionId\":\"{InitialVersion}\"", StringComparison.Ordinal)
.Replace($"\"lastUpdated\":\"{existingDate}\"", $"\"lastUpdated\":\"{inputDate}\"", StringComparison.Ordinal);
.Replace($"\"versionId\":\"{existingVersion}\"", $"\"versionId\":\"{inputVersion}\"", StringComparison.Ordinal)
.Replace($"\"lastUpdated\":\"{existingDate}\"", $"\"lastUpdated\":\"{inputDate}\"", StringComparison.Ordinal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ public async Task GivenTheResource_WhenUpdatingAnExistingResourceWithNoDataChang
ValidateUpdateResponse(updateResponseAfterVersionIdAndLastUpdatedTimeChanged, updateResponseAfterVersionIdAndLastUpdatedTimeAndTextChanged, false, HttpStatusCode.OK);
Assert.Contains(ContentUpdated, updateResponseAfterVersionIdAndLastUpdatedTimeAndTextChanged.Resource.Text.Div);

// Try to update the resource with no changes
// Check no new version is created. versionId and lastUpdated remains same
using FhirResponse<Observation> updateSecondTime = await _client.UpdateAsync(updateResponse.Resource, null);
ValidateUpdateResponse(updateResponseAfterVersionIdAndLastUpdatedTimeAndTextChanged, updateSecondTime, true, HttpStatusCode.OK);
Assert.Contains(ContentUpdated, updateSecondTime.Resource.Text.Div);

// Try to update the resource with some changes in meta.Profile/Tag/Security
// Check new version is created. versionId and lastUpdated are updated
updateResponse.Resource.Meta = new Meta
Expand Down

0 comments on commit 49a9489

Please sign in to comment.