Skip to content

Commit 9363545

Browse files
authored
(#162) Added additional read call at end of replace operation to get current state of object (#180)
1 parent 8065bff commit 9363545

File tree

5 files changed

+18
-28
lines changed

5 files changed

+18
-28
lines changed

src/CommunityToolkit.Datasync.Server/Controllers/TableController.Create.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ public virtual async Task<IActionResult> CreateAsync(CancellationToken cancellat
2727
Logger.LogInformation("CreateAsync: {entity}", entity.ToJsonString());
2828

2929
await AuthorizeRequestAsync(TableOperation.Create, entity, cancellationToken).ConfigureAwait(false);
30-
3130
await AccessControlProvider.PreCommitHookAsync(TableOperation.Create, entity, cancellationToken).ConfigureAwait(false);
32-
3331
await Repository.CreateAsync(entity, cancellationToken).ConfigureAwait(false);
34-
3532
await PostCommitHookAsync(TableOperation.Create, entity, cancellationToken).ConfigureAwait(false);
3633

3734
Logger.LogInformation("CreateAsync: created {entity}", entity.ToJsonString());

src/CommunityToolkit.Datasync.Server/Controllers/TableController.Replace.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public virtual async Task<IActionResult> ReplaceAsync([FromRoute] string id, Can
5353
await Repository.ReplaceAsync(entity, version, cancellationToken).ConfigureAwait(false);
5454
await PostCommitHookAsync(TableOperation.Update, entity, cancellationToken).ConfigureAwait(false);
5555

56-
Logger.LogInformation("ReplaceAsync: replaced {entity}", entity.ToJsonString());
57-
return Ok(entity);
56+
// Under certain (repository specific) circumstances, the entity may not be modified by the ReplaceAsync
57+
// operation, so we have to do an additional GET to ensure we are getting the right version of the entity
58+
TEntity? updatedEntity = await Repository.ReadAsync(id, cancellationToken).ConfigureAwait(false);
59+
60+
Logger.LogInformation("ReplaceAsync: replaced {entity}", updatedEntity.ToJsonString());
61+
return Ok(updatedEntity);
5862
}
5963
}

tests/CommunityToolkit.Datasync.Server.Test/Controllers/TableController_Replace_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public async Task ReplaceAsync_Works(bool includeIfMatch, bool includeLastModifi
181181
await accessProvider.Received(1).PostCommitHookAsync(TableOperation.Update, Arg.Any<TableData>(), Arg.Any<CancellationToken>());
182182
firedEvents.Should().ContainSingle();
183183

184-
await repository.Received(1).ReadAsync(entity.Id, Arg.Any<CancellationToken>());
184+
await repository.Received(2).ReadAsync(entity.Id, Arg.Any<CancellationToken>());
185185
await repository.Received(1).ReplaceAsync(Arg.Any<TableData>(), Arg.Any<byte[]>(), Arg.Any<CancellationToken>());
186186
}
187187
}

tests/CommunityToolkit.Datasync.Server.Test/Service/Replace_Tests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#define CAPTURE_STRING_JSON
6+
57
using CommunityToolkit.Datasync.TestCommon;
68
using CommunityToolkit.Datasync.TestCommon.Databases;
79
using CommunityToolkit.Datasync.TestCommon.Models;
8-
using CommunityToolkit.Datasync.TestService.AccessControlProviders;
9-
using Microsoft.AspNetCore.Localization;
1010
using System.Net;
1111
using System.Net.Http.Json;
1212
using System.Text;
@@ -24,9 +24,14 @@ public async Task Replace_Returns200()
2424
HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.MovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions);
2525
response.Should().HaveStatusCode(HttpStatusCode.OK);
2626

27+
#if CAPTURE_STRING_JSON
28+
string jsonContent = await response.Content.ReadAsStringAsync();
29+
ClientMovie clientMovie = JsonSerializer.Deserialize<ClientMovie>(jsonContent, this.serializerOptions);
30+
#else
2731
ClientMovie clientMovie = await response.Content.ReadFromJsonAsync<ClientMovie>(this.serializerOptions);
32+
#endif
33+
2834
clientMovie.Should().NotBeNull().And.HaveChangedMetadata(existingMovie, this.StartTime).And.BeEquivalentTo<IMovie>(existingMovie);
29-
3035
InMemoryMovie inMemoryMovie = this.factory.GetServerEntityById<InMemoryMovie>(clientMovie.Id);
3136
clientMovie.Should().HaveEquivalentMetadataTo(inMemoryMovie).And.BeEquivalentTo<IMovie>(inMemoryMovie);
3237
response.Headers.ETag.Should().BeETag($"\"{clientMovie.Version}\"");

tests/CommunityToolkit.Datasync.TestService/Properties/launchSettings.json

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
{
22
"$schema": "https://json.schemastore.org/launchsettings.json",
3-
"iisSettings": {
4-
"windowsAuthentication": false,
5-
"anonymousAuthentication": true,
6-
"iisExpress": {
7-
"applicationUrl": "http://localhost:6523",
8-
"sslPort": 44367
9-
}
10-
},
113
"profiles": {
124
"http": {
135
"commandName": "Project",
146
"dotnetRunMessages": true,
157
"launchBrowser": true,
16-
"launchUrl": "weatherforecast",
8+
"launchUrl": "api/in-memory/movies",
179
"applicationUrl": "http://localhost:5082",
1810
"environmentVariables": {
1911
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -23,16 +15,8 @@
2315
"commandName": "Project",
2416
"dotnetRunMessages": true,
2517
"launchBrowser": true,
26-
"launchUrl": "weatherforecast",
27-
"applicationUrl": "https://localhost:7186;http://localhost:5082",
28-
"environmentVariables": {
29-
"ASPNETCORE_ENVIRONMENT": "Development"
30-
}
31-
},
32-
"IIS Express": {
33-
"commandName": "IISExpress",
34-
"launchBrowser": true,
35-
"launchUrl": "weatherforecast",
18+
"launchUrl": "api/in-memory/movies",
19+
"applicationUrl": "https://localhost:5001;http://localhost:5082",
3620
"environmentVariables": {
3721
"ASPNETCORE_ENVIRONMENT": "Development"
3822
}

0 commit comments

Comments
 (0)