Skip to content
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

OnInsert/OnUpdate hooks can modify the new resource item, Etag not recalculated #255

Closed
Dragomir-Ivanov opened this issue Aug 19, 2019 · 7 comments

Comments

@Dragomir-Ivanov
Copy link
Contributor

During POST/PUT/PATCH new resource item Etag generation is done in item, err := resource.NewItem(doc) from the document supplied from the request. However after that hooks will be applied, that may or may not modify this item further. Modifying the item in the hook, doesn't recalculate the Etag.
One obvious solution suggested by @smyrman , is moving Etag calculation right before hitting the Store.

@smyrman
Copy link
Collaborator

smyrman commented Aug 19, 2019

Let's try to find a few problem cases, to see how they are handled by any given solution:

E.g. what happens if:

  • A resource with an updateAt timestamp field is PATCHED with no changes. Is the resource (and hence the E-tag) updated?

@Dragomir-Ivanov
Copy link
Contributor Author

Dragomir-Ivanov commented Aug 19, 2019

Okay, if you are referring to the invisible _updated field, that is produced by rest-layer then no, it is not included in the Etag sum.

func NewItem(payload map[string]interface{}) (*Item, error) {
	id, found := payload["id"]
	if !found {
		return nil, errors.New("Missing ID field")
	}
	etag, err := genEtag(payload)
	if err != nil {
		return nil, err
	}
	item := &Item{
		ID:      id,
		ETag:    etag,
		Updated: time.Now(),
		Payload: payload,
	}
	return item, nil
}

However if you are including your own:
"updated": schema.UpdatedField,, I am afraid it is a modification.

@smyrman
Copy link
Collaborator

smyrman commented Aug 19, 2019

I am referring to when you include your own field; or indeed any other field that is generated (by a field or resource hook) on update.

Another important question is:

  • Is there any cases where it's important for a hook to get an updated/accurate E-tag? (can't think of any myself)

The reason for coming up with these cases is to see which one we want to priortize before settling on the exact solution; there are trade-offs we can do to prioritize the most important use-cases if we can figure out which ones that is.

@smyrman
Copy link
Collaborator

smyrman commented Aug 19, 2019

Example trade-off:

However if you are including your own: "updated": schema.UpdatedField,, I am afraid it is a modification.

We can avoid this by aborting the update if there are no changes to a resource after a patch or put operation.

Trade-off: It won't be possible to force resource updates (via hooks) by updating (without changing) a field value.

What ever we end up on, the solution must of course be documented. Writing the documentation will also be a good test for the solution; if we can't easily formulate the behaviour in words, probably our solution is too complex.

@Dragomir-Ivanov
Copy link
Contributor Author

Dragomir-Ivanov commented Aug 19, 2019

In my opinion, force update via HTTP call should be counted as "update" even if we don't change anything in the resource item. Think of touch semantics.

If one doesn't want that behavior, then he should make his own hook that can be executed last, that should inspect all relevant fields, and if no changes there, he can just make item=original and revert all changes, including any "updatedAt" fields, etc.

Sadly we can't modify HTTP response from hooks, so one can return 304 if no changes.

@smyrman
Copy link
Collaborator

smyrman commented Aug 19, 2019

In my opinion, force update via HTTP call should be counted as "update" even if we don't change anything in the resource item. Think of touch semantics.

OK, let's go with this for now then; it sounds easier to document.

The only feature I can think of where an early abort sounds useful, is for list PATCH operations, but that isn't a feature that we currently got. We can revisit it at that point.

The remaining question is then, is there a case for updating the E-tag between each hook (e.g. to check for changes between original and item), or is updating of the E-tag right before storing the resource sufficient?

@Dragomir-Ivanov
Copy link
Contributor Author

I think updating right before storing is just fine for now. Will make a PR today or tomorrow.

Dragomir-Ivanov added a commit to Dragomir-Ivanov/rest-layer that referenced this issue Aug 19, 2019
Dragomir-Ivanov added a commit to Dragomir-Ivanov/rest-layer that referenced this issue Aug 21, 2019
E-tags used to be set after parsing an item only, before potential hooks
alter the item content. This change adds a final step to resource Update
and Create operations to recalculate E-tags right before passing items
to the Storer back-end.

Fixes rs#255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants