magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.

Home Page:http://www.magento.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

davidmolinacano opened this issue · comments

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”.

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.

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 🙂

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).

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.

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.

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.

Hello @davidmolinacano,

Thanks for the report and collaboration!

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

Thanks

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

✅ 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.

@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)

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
            );
        }

Hello @ihor-sviziev, do you have any news about it? Could you test it, we are really block due to we can't put a if statement to differentiate the version...

@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)

@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.

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....

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

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

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.

@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?

@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 = [];
    }
}