-
-
Notifications
You must be signed in to change notification settings - Fork 158
PATCH operations don't return updated copy of resource if resource was changed by custom business logic #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @DumpsterDoofus , thanks for bringing this up. In all honestly: we can (and should) provide a better way to set modification dates. What we offer today:
How it works today:
The best place to intercept would be between 4 and 5: By doing the changes on the stored version, they get persisted and the change tracker will properly detect them. And optionally, code could inspect targetted fields and choose not to overwrite values from the request. Solutions:
We've recently added the same change tracking mechanism for POST requests, resulting in similar challenges. Eventually we'd like to implement the last option, so we are hesitant to add things now we'll be removing later. On the other hand, we feel your pain and would like to provide something today that somehow enables your use case. Suggestions are welcome! |
Today we merged changes that make it at least possible to accomplish what you need, though we may provide better ways in the future. Using the example below, change tracking works properly, you don't need special handling for targeted fields and can change both exposed and non-exposed properties. The downside is you need to copy/paste the contents of the repository UpdateAsync method and put your own changes in-between. Example: public class ArticleRepository : EntityFrameworkCoreRepository<Article>
{
private readonly ITargetedFields _targetedFields;
private readonly IResourceFactory _resourceFactory;
private readonly DbContext _dbContext;
public ArticleRepository(ITargetedFields targetedFields, IDbContextResolver contextResolver,
IResourceGraph resourceGraph, IResourceFactory resourceFactory,
IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory)
: base(targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory)
{
_targetedFields = targetedFields;
_resourceFactory = resourceFactory;
_dbContext = contextResolver.GetContext();
}
public override async Task UpdateAsync(Article resourceFromRequest, Article resourceFromDatabase)
{
using var collector = new PlaceholderResourceCollector(_resourceFactory, _dbContext);
foreach (var relationship in _targetedFields.Relationships)
{
var rightResources = relationship.GetValue(resourceFromRequest);
AssertIsNotClearingRequiredRelationship(relationship, resourceFromDatabase, rightResources);
await UpdateRelationshipAsync(relationship, resourceFromDatabase, rightResources, collector);
}
// Start: Custom business logic (overwritten by request data)
resourceFromDatabase.IsInStock = true;
// End: Custom business logic (overwritten by request data)
foreach (var attribute in _targetedFields.Attributes)
{
attribute.SetValue(resourceFromDatabase, attribute.GetValue(resourceFromRequest));
}
// Start: Custom business logic (overwrites request data)
resourceFromDatabase.LastModifiedAt = DateTimeOffset.UtcNow;
// End: Custom business logic (overwrites request data)
await SaveChangesAsync();
}
} Hope this helps. |
Closed in favor of #934. |
Description
Not sure if this is a bug or expected behavior, but figured I'd ask. I have a custom
IResourceService
that modifies some data when a PATCH request comes through. When I send a PATCH request, the updated resource does not come back in the response, even though the updated resource has data that the client has never seen before.I sort of expected that during a PATCH, if a resource has been updated with data that the client has never seen before, then the updated resource would be sent in the response so that the client was aware that something had changed.
Example code:
The response to a PATCH operation:
An empty response is returned regardless of whether the client did not send
lastModifiedOn
in the request, or did send it in the request and the client value was overwritten by the custom logic.As a workaround, you can do a GET after each PATCH.
Environment
The text was updated successfully, but these errors were encountered: