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

Next-34765 - Disallow empty id filters in entity search #3696

Conversation

Benedikt-Brunner
Copy link
Contributor

@Benedikt-Brunner Benedikt-Brunner commented May 3, 2024

1. Why is this change necessary?

When fetching entities it is currently possible to pass the ids-filter as an empty list resulting in no filter being applied, therefore all entities are part of the result. Then all entities are loaded into memory, since the ids-filter disables the page and limit filters.

2. What does this change do, exactly?

This adds a check to see if the ids-filter is an empty list and throws an exception informing the user of the api that "ids may not be empty".

3. Describe each step to reproduce the issue or behaviour.

  1. Send a request to ...api/search/{entity} with roughly this body:

    {
       ...
       limit: 1,
       ids: [],
       ...
    }
    
  2. Verify that all entities have been loaded / the program has run out of memory

4. Please link to the relevant issues (if any).

https://issues.shopware.com/issues/NEXT-34765

5. Checklist

  • I have rebased my changes to remove merge conflicts
  • I have written tests and verified that they fail without my change
  • I have created a changelog file with all necessary information about my changes
  • I have written or adjusted the documentation according to my changes
  • This change has comments for package types, values, functions, and non-obvious lines of code
  • I have read the contribution requirements and fulfil them.

copilot:summary

copilot:walkthrough

@Benedikt-Brunner Benedikt-Brunner changed the title disallow empty id filters in entity search Next-34765 - Disallow empty id filters in entity search May 3, 2024
@Benedikt-Brunner
Copy link
Contributor Author

Note

At first i wanted to implement this check directly in the criteria class, however it turned out that at least in tests the ids-filter is set to an empty list quite often. If desired we could also refactor this PR to enforce this check directly in the criteria class.

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner yeah can you please move the check in the Criteria class to the setIds method and then just call setIds in the constructor please :)

@AydinHassan AydinHassan self-assigned this May 8, 2024
@AydinHassan
Copy link
Contributor

@Benedikt-Brunner thanks for updating! I guess we can remove the check from the constructor now as it's duplicated?

@Benedikt-Brunner
Copy link
Contributor Author

@AydinHassan that's right, i hadn't catched that the check was there 😅 . The move is however not complete as there are surely still failing tests. I am having a little trouble getting the tests to run correctly on my system so it would be great if we could enable the CI to run again 🙏 .

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner running now :)

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner looks like we need another empty check in \Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityReader::loadManyToManyWithCriteria where it calls setIds()

@Benedikt-Brunner
Copy link
Contributor Author

@AydinHassan these new changes fixed all of the unit tests at least locally, however for some reason there was a migration that was constantly failing, i assume it was a problem on my machine as the migration did not use the criteria object.

@AydinHassan
Copy link
Contributor

Nice thanks @Benedikt-Brunner I will import it now :)

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner can you maybe rebase and squash the commits? Our importer is not able to process the PR atm.

@CLAassistant
Copy link

CLAassistant commented May 14, 2024

CLA assistant check
All committers have signed the CLA.

@Benedikt-Brunner Benedikt-Brunner changed the base branch from trunk to 6.4.15.2 May 14, 2024 13:21
@Benedikt-Brunner Benedikt-Brunner changed the base branch from 6.4.15.2 to trunk May 14, 2024 13:22
@maximilianruesch maximilianruesch force-pushed the NEXT-34765/disallow-empty-id-filters-in-entity-search branch from 55ae77d to 55b75fe Compare May 14, 2024 13:45
Move empty ids check into criteria object

fix entity reader id access

refactor empty id initialisations
@maximilianruesch maximilianruesch force-pushed the NEXT-34765/disallow-empty-id-filters-in-entity-search branch from 55b75fe to 60eab29 Compare May 14, 2024 13:46
@Benedikt-Brunner
Copy link
Contributor Author

@AydinHassan should be fixed now 🤞

@shopware-github-importer
Copy link

Hello,

thank you for creating this pull request.
I have opened an issue on our Issue Tracker for you. See the issue link: https://issues.shopware.com/issues/NEXT-34765

Please use this issue to track the state of your pull request.

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner many thanks! Now it's imported. I will let you know when it's merged 💙

try {
$criteria = $this->createCriteria($request, $context);
$response = $this->productListRoute->load($criteria, $context);
} catch (DataAbstractionLayerException $e) {
Copy link
Member

Choose a reason for hiding this comment

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

why did you changed it like this? the logic is different now.

we should probably fix the throwing of this exception instead

Copy link
Contributor

Choose a reason for hiding this comment

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

@mitelg I think it's the same. Because now setIds() on criteria throws an exception whens ids is empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case the code specifically checked for the ids being empty, so i assumed it was expected behaviour. Therefore it seemed to be most correct to run the code behind that check when instantiating the criteria object fails.

I do however not know much about the code.

Copy link
Member

Choose a reason for hiding this comment

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

first the criteria is created and then the event is dispatched. that is now changed to dispatching the event after the loading of the product list. which is wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

ah yeah, thanks. I will take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case the event probably needs to be dispatched in both the try and catch closures.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's a bit tricky because we no longer have a valid Criteria object to dispatch GuestWishListPageletProductCriteriaEvent with

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should i do something here or will you handle it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Benedikt-Brunner I made the changes and its internal review now. Will ping you when it's merged :)

@AydinHassan
Copy link
Contributor

@Benedikt-Brunner thanks for your contribution, it's merged now!

@Benedikt-Brunner Benedikt-Brunner deleted the NEXT-34765/disallow-empty-id-filters-in-entity-search branch May 17, 2024 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants