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

Api / Services / Fields: Only emitFilter if emitEvents is not set to false #21670

Merged

Conversation

joselcvarela
Copy link
Member

Scope

The purpose of this PR is to fix eventual recursivity when using Fields Service.
More specifically, if we are using a filter hook for fields.update, fields.create or fields.delete, then use FieldsService.updateField,FieldsService.createField or FieldsService.deleteField, this may cause recursion because the emitFilter is always called.
In order to prevent this, we just need to do the same as other services which is check if emitEvents is set to false. If so
do not emitFilter and use original payload.

What's changed:

  • Use emitEvents to prevent emitFilter to be called on fields.update, fields.create and fields.delete

Potential Risks / Drawbacks

  • I think that no one is relying on this recursion mechanism using emitEvents: false option, because it's not correct. But if there are, this will be impacting them. In order to keep the recursion, they can just put remove emitEvents: false option.

Review Notes / Questions

  • This recursion seems to be infinite since there are no stop condition, which means project will go down after some iterations.

Copy link

changeset-bot bot commented Mar 4, 2024

🦋 Changeset detected

Latest commit: 4980a41

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@directus/api Patch
directus Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@joselcvarela joselcvarela changed the title Api / Services / Fields: Only emitFilter if emitEvents is not set to false Api / Services / Fields: Only emitFilter if emitEvents is not set to false Mar 4, 2024
@joselcvarela
Copy link
Member Author

joselcvarela commented Mar 4, 2024

Here is a reproduction sample that will crash the instance once a field is updated. Create it under extensions/hooks/fields/index.js:

export default ({ filter }, { services, database, getSchema }) => {
	filter('fields.update', async (field) => {
		const fields = new services.FieldsService({ knex: database, schema: await getSchema() });

		await fields.updateField(field.collection, { field: field.field, sort: 1 });
		console.log('Updating field!');
	});
};

@br41nslug
Copy link
Member

br41nslug commented Mar 4, 2024

Does that example not crash because the getSchema function is not await instead of being emitEvents related? 🤔

@joselcvarela
Copy link
Member Author

@br41nslug Nope and just updated the code 👍

@rijkvanzanten rijkvanzanten merged commit 86b42a9 into directus:main Mar 4, 2024
4 checks passed
@github-actions github-actions bot added this to the Next Release milestone Mar 4, 2024
@joselcvarela joselcvarela deleted the api/fields/fix-recursive-emitFilter branch March 4, 2024 16:56
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants