Skip to content

Commit 6f605cd

Browse files
committed
Added AlwaysReturnResourceOnCreateUpdate
Added setting to IJsonApiOptions to always return a serialized resource on PATCH and POST, regardless if implicit changes occurred on server.
1 parent 705b65f commit 6f605cd

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/JsonApiDotNetCore/Configuration/IJsonApiOptions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,13 @@ internal NamingStrategy SerializerNamingStrategy
181181
/// If true, will automatically sort resources by Id, if no other sort was specified. Default is true.
182182
/// </summary>
183183
bool EnableDefaultSortById { get; set; }
184+
185+
/// <summary>
186+
/// If true, will return the serialized updated resource on POST 201 Created and PATCH 200 OK. Otherwise, the serialized resource is only returned
187+
/// if any field is changed on the server that was not included in the original request. Default is false
188+
/// <see href="https://jsonapi.org/format/#crud-updating-responses-200">https://jsonapi.org/format/#crud-updating-responses-200</see>
189+
/// <see href="https://jsonapi.org/format/#crud-creating-responses-204">https://jsonapi.org/format/#crud-creating-responses-204</see>
190+
/// </summary>
191+
bool AlwaysReturnResourceOnCreateUpdate { get; set; }
184192
}
185193
}

src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public sealed class JsonApiOptions : IJsonApiOptions
8585
/// <inheritdoc />
8686
public bool EnableDefaultSortById { get; set; } = true;
8787

88+
/// <inheritdoc />
89+
public bool AlwaysReturnResourceOnCreateUpdate { get; set; } = false;
90+
8891
/// <inheritdoc />
8992
public JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings
9093
{

src/JsonApiDotNetCore/Services/JsonApiResourceService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public virtual async Task<TResource> CreateAsync(TResource resource, Cancellatio
229229

230230
bool hasImplicitChanges = _resourceChangeTracker.HasImplicitChanges();
231231

232-
if (!hasImplicitChanges)
232+
if (!_options.AlwaysReturnResourceOnCreateUpdate && !hasImplicitChanges)
233233
{
234234
return null;
235235
}
@@ -383,7 +383,7 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource, Can
383383

384384
bool hasImplicitChanges = _resourceChangeTracker.HasImplicitChanges();
385385

386-
if (!hasImplicitChanges)
386+
if (!_options.AlwaysReturnResourceOnCreateUpdate && !hasImplicitChanges)
387387
{
388388
return null;
389389
}

0 commit comments

Comments
 (0)