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

Fix regression in count database helper when counting filtered related fields #22448

Merged
merged 4 commits into from May 13, 2024

Conversation

hanneskuettner
Copy link
Contributor

@hanneskuettner hanneskuettner commented May 9, 2024

Scope

A special case that slipped the cracks of #22297: When counting the items of a related field, that is filtered down using permissions, we incorrectly use the un-aliased collection name for the joins in addJoin.

You can reproduce this by setting up the following collections

  • videos collection
  • battles collection with at least a status field and a o2m relation to the videos collections, called videos, call the foreign key battle

Create a test role and set the permissions:

  • battles collection to All Access for reading
  • videos collection to Custom. Allow all fields and set the Item Permission filter to {"_and":[{"battle_id":{"status":{"_eq":"published"}}}]}

Without this PR you should see the error as described in #22443 for the GraphQL query

{
  battles {
    id
    videos_func {
      count
    }
}

What's changed:

Add the newly aliased related collection to the alias map as parent collection with an empty string as key for the nestedapplyFilter. This will cause addJoin to use this alias as aliasedParentCollection instead of the real name of the collection here, as parentFields is undefined for the first level of nested filter.

const aliasedParentCollection = aliasMap[parentFields ?? '']?.alias || parentCollection;

For additional levels of nested filters parentFields has the parent collections in there as thus the behavior is still the default behavior for nested filters.

Potential Risks / Drawbacks

None, hopefully

Review Notes / Questions

  • Is the changeset understandable?

Fixes #22443

Copy link

changeset-bot bot commented May 9, 2024

🦋 Changeset detected

Latest commit: 5a28241

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

Copy link
Member

@paescuj paescuj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally updated the addWhereClauses function to respect the alias. Required when there's a permission filter on the related collection itself, e.g. on videos:

{
    "_and": [
        {
            "id": {
                "_eq": "1"
            }
        }
    ]
}

Ran the blackbox tests locally as an additional check.

@paescuj paescuj merged commit c2ba3e1 into main May 13, 2024
4 checks passed
@paescuj paescuj deleted the fix-22443-filtered-count-regression branch May 13, 2024 10:28
@github-actions github-actions bot added this to the Next Release milestone May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

collection_func.count is not working
2 participants