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

Backward compatibility has been lost on Magento\Catalog\Model\ProductRepository class #38669

Closed
1 of 5 tasks
davidmolinacano opened this issue Apr 26, 2024 · 25 comments
Closed
1 of 5 tasks
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: needs update Additional information is require, waiting for response Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: done Reported on 2.4.7 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@davidmolinacano
Copy link

davidmolinacano commented Apr 26, 2024

Summary

I've a module that have a ProductRepository class which extends from the core Magento\Catalog\Model\ProductRepository class. We were calling the parent::__construct passing the Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper class which was required before Magento 2.4.7 as the 2nd parameter of the constructor, but now it has been removed and the second parameter must be Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory class instead, thus losing the backward compatibility.

Examples

image

(Tested on Magento 2.4.3 after having removed the initialization helper from the parent::__construct)

Proposed solution

My proposal is to restore the Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper class as the second parameter. Although it's unused as you stated on the changelog, in my opinion it's required to guarantee the backward compatibility.

This bug can be reproduced by installing our module (version 0.13.13) on a Magento 2.4.7 and then executing a bin/magento setup:di:compile

Release note

Release Line: 2.4.7

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@davidmolinacano davidmolinacano added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Apr 26, 2024
Copy link

m2-assistant bot commented Apr 26, 2024

Hi @davidmolinacano. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@hostep
Copy link
Contributor

hostep commented Apr 26, 2024

Issue was caused by #37868, which actually fixed a bug by removing the dependency. Not sure if it will be easy to keep this fix and not break backwards compatibility.
The class is also not marked with @api, which means BC can be broken (even though we should really try to avoid it as much as we can)

Just my 2 cents, others are free to give their own opinions though 🙂

@davidmolinacano
Copy link
Author

Thank you very much for the explanation @hostep ! Unfortunately, if this fix cannot be reverted we would need a proper solution for our specific case, because we must keep BC in our module since most of our customers are still using versions lower than 2.4.7, but a few of them are currently using the 2.4.7 too. If someone has any proposal about this issue, I'll be glad to hear it (well, in this case to read it).

@hostep
Copy link
Contributor

hostep commented Apr 26, 2024

So doofinder/doofinder-magento2@f9da4d2 is not fixing it?

We did use a solution like this in the past to be compatible with various magento versions with constructors that don't take the same amount of arguments: #30684 (comment)
It's a bit ugly, but it works and doesn't cause issues.

@davidmolinacano
Copy link
Author

Unfortunately it didn't work as expected. Even with this fix, the setup:di:compile was still complaining about this. But thank you very much for pointing it, just in case.

@engcom-Hotel engcom-Hotel added the Reported on 2.4.7 Indicates original Magento version for the Issue report. label Apr 29, 2024
@engcom-Hotel engcom-Hotel self-assigned this Apr 29, 2024
Copy link

m2-assistant bot commented Apr 29, 2024

Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • 5. Add label Issue: Confirmed once verification is complete.
  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Hotel
Copy link
Contributor

Hello @davidmolinacano,

Thanks for the report and collaboration!

By looking into the codebase we are confirming this issue. The changes are Backward incompatible.

Thanks

@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Area: Framework and removed Issue: ready for confirmation labels Apr 29, 2024
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.adobe.com/browse/AC-11874 is successfully created for this GitHub issue.

Copy link

m2-assistant bot commented Apr 29, 2024

✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Apr 30, 2024

@davidmolinacano We can't just add back this parameter since it was already removed in 2.4.7. Just adding it back in 2.4.8 will cause backward incompatible change again. I think we should provide some workaround for this issue for you.

I guess the correct fix would be adding some if statement and compare magento version.

Unfortunately it didn't work as expected. Even with this fix, the setup:di:compile was still complaining about this.

Could you please provide some details which exception do you have? Also, would be great to get output from running a CLI command with the -vvv flag (verbose mode)

@davidmolinacano
Copy link
Author

davidmolinacano commented Apr 30, 2024

Of course, I'm pasting here the results of the command (Tested on Magento 2.4.7 and PHP 8.2):

# bin/magento setup:di:compile -vvv
Compilation was started.
Interception cache generation... 6/9 [==================>---------]  66% 40 secs 426.0 MiBErrors during compilation:
        Doofinder\Feed\Model\ProductRepository
                Incompatible argument type: Required type: \Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory. Actual type: \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; File: 
/app/app/code/Doofinder/Feed/Model/ProductRepository.php

Total Errors Count: 1

In Log.php line 92:
                                           
  [Magento\Framework\Validator\Exception]  
  Error during compilation                 
                                           

Exception trace:
  at /app/setup/src/Magento/Setup/Module/Di/Compiler/Log/Log.php:92
 Magento\Setup\Module\Di\Compiler\Log\Log->report() at /app/setup/src/Magento/Setup/Module/Di/Code/Reader/Decorator/Interceptions.php:85
 Magento\Setup\Module\Di\Code\Reader\Decorator\Interceptions->getList() at /app/setup/src/Magento/Setup/Module/Di/App/Task/Operation/InterceptionCache.php:61
 Magento\Setup\Module\Di\App\Task\Operation\InterceptionCache->doOperation() at /app/setup/src/Magento/Setup/Module/Di/App/Task/Manager.php:56
 Magento\Setup\Module\Di\App\Task\Manager->process() at /app/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php:209
 Magento\Setup\Console\Command\DiCompileCommand->execute() at /app/vendor/symfony/console/Command/Command.php:326
 Symfony\Component\Console\Command\Command->run() at /app/vendor/symfony/console/Application.php:1078
 Symfony\Component\Console\Application->doRunCommand() at /app/vendor/symfony/console/Application.php:324
 Symfony\Component\Console\Application->doRun() at /app/vendor/magento/framework/Console/Cli.php:118
 Magento\Framework\Console\Cli->doRun() at /app/vendor/symfony/console/Application.php:175
 Symfony\Component\Console\Application->run() at /app/bin/magento:23

setup:di:compile

image

Even forcing the if with false (just in case) it only takes into account the first statement and ignore the second one:


if (false) {
            // Magento < 2.4.7
            parent::__construct(
                $productFactory,
                $initializationHelper,
                $searchResultsFactory,
                $collectionFactory,
                $searchCriteriaBuilder,
                $attributeRepository,
                $resourceModel,
                $linkInitializer,
                $linkTypeProvider,
                $storeManager,
                $filterBuilder,
                $metadataServiceInterface,
                $extensibleDataObjectConverter,
                $optionConverter,
                $fileSystem,
                $contentValidator,
                $contentFactory,
                $mimeTypeExtensionMap,
                $imageProcessor,
                $extensionAttributesJoinProcessor,
                $collectionProcessor,
                $serializer,
                $cacheLimit,
                $readExtensions
            );
        } else {
            // Magento >= 2.4.7
            parent::__construct(
                $productFactory,
                $searchResultsFactory,
                $collectionFactory,
                $searchCriteriaBuilder,
                $attributeRepository,
                $resourceModel,
                $linkInitializer,
                $linkTypeProvider,
                $storeManager,
                $filterBuilder,
                $metadataServiceInterface,
                $extensibleDataObjectConverter,
                $optionConverter,
                $fileSystem,
                $contentValidator,
                $contentFactory,
                $mimeTypeExtensionMap,
                $imageProcessor,
                $extensionAttributesJoinProcessor,
                $collectionProcessor,
                $serializer,
                $cacheLimit,
                $readExtensions
            );
        }

TuVanDev added a commit to TuVanDev/doofinder-magento2 that referenced this issue May 9, 2024
Backward compatibility has been lost in the Magento\Catalog\Model\ProductRepository class

Check the issue page in the Magento repo for reference: magento/magento2#38669
@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented May 9, 2024

@sofia-doofinder, sorry, no updates
As to me, it looks like a bug in the Magento class validation.
I haven't had such issues.
As workaround, you can do not override constructor and get needed dependencies from object manager. I think that's the only option then unless it's fixed

The workaround example you can see in #38669 (comment)

@TuVanDev
Copy link
Member

TuVanDev commented May 9, 2024

@davidmolinacano @sofia-doofinder @hostep @ihor-sviziev The approach of checking the current Magento version and serving the appropriate arguments of the parent method is working as expected.

I have reviewed the corresponding commit on the Doofinder repository and tested it in my own environment. During testing, I encountered the following error:

Compilation was started.
Repositories code generation... 1/9 [===>------------------------]  11% < 1 sec 119.0 MiB
In ErrorHandler.php line 62:
                                                                                                                                                                                                           
  [Exception]                                                                                                                                                                                              
  Deprecated Functionality: Optional parameter $cacheLimit declared before required parameter $productMetadataInterface is implicitly treated as a required parameter in /var/www/html/vendor/doofinder/d  
  oofinder-magento2/Model/ProductRepository.php on line 65                                                                                                                                                 
                                                                                                                                                                                                           

Exception trace:
  at /var/www/html/vendor/magento/framework/App/ErrorHandler.php:62
 Magento\Framework\App\ErrorHandler->handler() at /var/www/html/vendor/composer/ClassLoader.php:571
 include() at /var/www/html/vendor/composer/ClassLoader.php:571
 Composer\Autoload\includeFile() at /var/www/html/vendor/composer/ClassLoader.php:428
 Composer\Autoload\ClassLoader->loadClass() at n/a:n/a
 class_exists() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php:134
 Magento\Setup\Module\Di\Code\Reader\ClassesScanner->includeClass() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php:117
 Magento\Setup\Module\Di\Code\Reader\ClassesScanner->extract() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php:87
 Magento\Setup\Module\Di\Code\Reader\ClassesScanner->getList() at /var/www/html/setup/src/Magento/Setup/Module/Di/App/Task/Operation/RepositoryGenerator.php:61
 Magento\Setup\Module\Di\App\Task\Operation\RepositoryGenerator->doOperation() at /var/www/html/setup/src/Magento/Setup/Module/Di/App/Task/Manager.php:56
 Magento\Setup\Module\Di\App\Task\Manager->process() at /var/www/html/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php:209
 Magento\Setup\Console\Command\DiCompileCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:326
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1078
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:324
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/magento/framework/Console/Cli.php:118
 Magento\Framework\Console\Cli->doRun() at /var/www/html/vendor/symfony/console/Application.php:175
 Symfony\Component\Console\Application->run() at /var/www/html/bin/magento:23

setup:di:compile

You may observe that the error message has changed. The issue was resolved by placing the $productMetadataInterface param before the optional params.

I have submitted a pull request to the Doofinder repository to fix this issue. doofinder/doofinder-magento2#317

@davidmolinacano This is my first time hearing about the Doofinder search feature, and it looks great. Keep up the good work!
There is an unused property, stockRegistry, in the Doofinder\Feed\Model\ProductRepository class. I recommend removing it to optimize your class.

@sofia-doofinder
Copy link

sofia-doofinder commented May 9, 2024

Hello @TuVanDev I'm checking you PR but it didn't work, as @davidmolinacano said the if seems not work at all...

I'm using your branch, and I'm using magento 2.4.3, when I run the comman setup:di:compile I got the following error:
image

Interception cache generation... 6/9 [==================>---------]  66% 26 secs 480.0 MiBErrors during compilation:
        Doofinder\Feed\Model\ProductRepository
                Incompatible argument type: Required type: \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper. Actual type: \Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory; File: 
/app/app/code/Doofinder/Feed/Model/ProductRepository.php

Total Errors Count: 1

In Log.php line 92:
                            
  Error during compilation  
                            

So it is saying that expect as required type \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper, however is getting \Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory

So it looks like is trying to load the if statment instead of access by the else, becuase is the first statment where is ignored the initializationHelper. So, looks like the if (version_compare($productMetadataInterface->getVersion(), '2.4.7', '>=')) return true, what can't be due to I'm using 2.4.3
image

The weired thing is that if I change the if to if (false) it still access by the first clausule beucase I receive the same error:
image

seems that the if is not workingn as expected because always tries to load the first statment....

@ihor-sviziev
Copy link
Contributor

@TuVanDev @sofia-doofinder Can you try removing constructor override and try something like this instead?
#30684 (comment)

@sofia-doofinder
Copy link

thank you @ihor-sviziev I'm going to check

TuVanDev added a commit to TuVanDev/doofinder-magento2 that referenced this issue May 10, 2024
…t in the Magento\Catalog\Model\ProductRepository class

Check the issue page in the Magento repo for reference: magento/magento2#38669
TuVanDev added a commit to TuVanDev/doofinder-magento2 that referenced this issue May 10, 2024
…t in the Magento\Catalog\Model\ProductRepository class

Check the issue page in the Magento repo for reference: magento/magento2#38669
@TuVanDev
Copy link
Member

Hi @sofia-doofinder , thanks for letting me know. I have reviewed the interception generation code and discovered that the ConstructorIntegrity validator class compares arguments between the current class and the parent class when a call parent::__construct() occurs.

As a result, the if else statements within the __construct() method were never reached while the ConstructorIntegrity class was validating class constructors.
Therefore, I have implemented @hostep 's solution, which involves removing the __construct() method and instantiating the necessary instances. I submitted another pull request to doofinder/doofinder-magento2 repository.

I have another solution, which is to rebuild your ProductRepository class based on the Magento\Catalog\Model\ProductRepository class without actually extending it. However, this approach has a drawback as it would involve manually implementing any changes made to the Magento\Catalog\Model\ProductRepository class into your own class whenever that file is updated.

davidmolinacano added a commit to doofinder/doofinder-magento2 that referenced this issue May 13, 2024
…t in the Magento\Catalog\Model\ProductRepository class (#318)

* Compatible with Magento 2.4.7, as backward compatibility has been lost in the Magento\Catalog\Model\ProductRepository class

Check the issue page in the Magento repo for reference: magento/magento2#38669

* Update Model/ProductRepository.php

Changed inventoryHelper to inventoryHelperFactory

Co-authored-by: sofia-doofinder <92720455+sofia-doofinder@users.noreply.github.com>

---------

Co-authored-by: David Molina Cano <128705267+davidmolinacano@users.noreply.github.com>
Co-authored-by: sofia-doofinder <92720455+sofia-doofinder@users.noreply.github.com>
@sofia-doofinder
Copy link

@TuVanDev thank you for your PR, we've try to upload to magento market but the code sniffer fails with the following reason:

image

So we can't do on that way... any help please?

@TuVanDev
Copy link
Member

@sofia-doofinder In this case, your team can go with the approach I suggested previously #38669 (comment)

I have another solution, which is to rebuild your ProductRepository class based on the Magento\Catalog\Model\ProductRepository class without actually extending it. However, this approach has a drawback as it would involve manually implementing any changes made to the Magento\Catalog\Model\ProductRepository class into your own class whenever that file is updated.

Starting from Magento version 2.4.7, the class Magento\Catalog\Model\ProductRepository now includes the implementation of Magento\Framework\ObjectManager\ResetAfterRequestInterface.

To support both older Magento versions and Magento version 2.4.7+, you should avoid using this implementation. Therefore, your class should look like this:

<?php
declare(strict_types=1);

namespace Doofinder\Feed\Model;

class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterface
{
    // More code goes here

    /**
     * Resets mutable state and/or resources in objects that need to be cleaned after a response has been sent.
     *
     * @return void
     */
    public function _resetState(): void
    {
        $this->instances = [];
        $this->instancesById = [];
    }
}

@engcom-Bravo
Copy link
Contributor

Hello,

Internal team has started to work on it

Thanks

@engcom-Bravo
Copy link
Contributor

Hi @davidmolinacano,

Thanks for your reporting and collaboration.

we have analysed the issue on the 2.4 develop and installed extension doofinder-magento2 for reproducing the issue. We have found that the issue is not reproducible. I have attached the screenshot for reference.

Screenshot 2024-06-27 at 2 43 38 PM

Thanks.

@engcom-Bravo engcom-Bravo added the Issue: needs update Additional information is require, waiting for response label Jun 28, 2024
@TuVanDev
Copy link
Member

@engcom-Bravo this issue has been confirmed by @engcom-Hotel #38669 (comment).

The doofinder-magento2 module has resolved this issue by implement a workaround as per my suggestion at #38669 (comment)

If you want to reproduce the issue with that module, you can install version 0.13.13, which didn't apply the fix.

@hostep
Copy link
Contributor

hostep commented Jun 28, 2024

I still stand by my point from my first comment, that we should just leave this be and don't attempt to fix this in core Magento.

@engcom-Bravo
Copy link
Contributor

Hi @davidmolinacano,

@hostep @TuVanDev Thanks for your contribution!!

As per this #38669 (comment) We are closing this issue.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: needs update Additional information is require, waiting for response Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: done Reported on 2.4.7 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Development

No branches or pull requests

8 participants