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

[Issue] Webapi product custom attribute preprocessor #38633

Open
gustavosolomon opened this issue Apr 17, 2024 · 9 comments · May be fixed by #38657
Open

[Issue] Webapi product custom attribute preprocessor #38633

gustavosolomon opened this issue Apr 17, 2024 · 9 comments · May be fixed by #38657
Assignees
Labels
feature request Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR in progress Reported on 2.4.x Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@gustavosolomon
Copy link

gustavosolomon commented Apr 17, 2024

Description

This issue happens when you add a pre-processor object in "customAttributePreprocessors" array to "Magento\Framework\Webapi\ServiceInputProcessor" class by using di.xml.

The injected processors should implement the interface "Magento\Framework\Webapi\CustomAttribute\PreprocessorInterface".

The issue is: The added processor simply doesn't run. Because it looks there's an issue on how those processors are "validated" before run.

For what I've checked, this Webapi input pre-processors is not commonly used. Actually, is not used by Magento Community modules at all (at least for modules I have installed), but, is supported by the Magento codebase, so I guess it should be working correctly.

Preconditions

  1. Inject an processor object, using "customAttributePreprocessors" array in ServiceInputProcessor class.
  2. The processor injected to "customAttributePreprocessors" array should implement PreprocessorInterface interface.
  3. The attributes that will be processed by the processor should be returned in processor's "getAffectedAttributes" method.

Pre-processor code example:

<?php

declare(strict_types=1);

namespace Vendor\Module\Webapi\CustomAttribute;

use Magento\Framework\Webapi\CustomAttribute\PreprocessorInterface;

class TestProcessor implements PreprocessorInterface
{
    public function shouldBeProcessed(string $key, $attribute): bool
    {
        return true;
    }

    public function process(string $key, &$attribute)
    {
        // Do something with the attribute
    }

    public function getAffectedAttributes(): array
    {
        return ['manufacturer'];
    }
}

Steps to reproduce

  1. Inject the processor to a custom modules di.xml and clear cache / generated code.
  2. Send a POST request to product API endpoint, in order to create/update some product data.
  3. Include manufacturer value inside custom_attributesdata

Expected result

  1. The processor should be run for each attributes defined in processor's "getAffectedAttributes" method.
  2. Processor should be run for this custom attribute "manufacturer" because I sent manufacturer data inside custom_attributes node data.
  3. I want to be able to convert the value sent to the API, from an option label to it's option id... This would allow me to send the manufacturer label instead of manufacturer option id.

Actual result

  1. The processor is not run at all, even if "getAffectedAttributes" method returns the rights attribute codes array.

For what I've checked, the "issue" is happening in this context:

$this->runCustomAttributePreprocessors($key, $customAttribute);
if (!is_array($customAttribute)) {
$customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
}
list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);

I think it should be called after the following line:

list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);

Instead of calling the runCustomAttributePreprocessors function using $key, like this:
$this->runCustomAttributePreprocessors($key, $customAttribute);

it should be calling it using $customAttributeCode, like this:
$this->runCustomAttributePreprocessors($customAttributeCode, $customAttribute);

Because $key here, refers to the sort order of the attribute inside "custom_attributes" data, when it should be based on attribute_code.

This will ensure that validator mechanism (which relies on attribute code) works correctly and runs the processor.

Additional code reference on how Magento validates those processors run:

private function getAttributesPreprocessorsMap(): array
{
if (!$this->attributesPreprocessorsMap) {
foreach ($this->customAttributePreprocessors as $attributePreprocessor) {
foreach ($attributePreprocessor->getAffectedAttributes() as $attributeKey) {
$this->attributesPreprocessorsMap[$attributeKey][] = $attributePreprocessor;
}
}
}
return $this->attributesPreprocessorsMap;
}

Copy link

m2-assistant bot commented Apr 17, 2024

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

@gustavosolomon
Copy link
Author

I found that if I send "custom_attributes" data as object instead of array of objects, it works..

So if I send like this, it works:

{
    "custom_attributes": {
        "manufacturer": "test"
    }
}

that's because $key (referenced above in the issue), in this case, will be the attribute code itself.

but if I send like this, it doesn't:

{
    "custom_attributes": [
        {
            "attribute_code": "manufacturer",
            "value": "test"
        }
    ]
}

because $key in this case, will be an auto-assigned array key (0 => []).

so that's why I think we should change the runCustomAttributePreprocessors call and use $customAttributeCode (which is always the attribute code, instead of $key (which can be an auto-assigned array key)

@engcom-Delta engcom-Delta added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Apr 18, 2024
@engcom-Hotel engcom-Hotel added the Reported on 2.4.x Indicates original Magento version for the Issue report. label Apr 18, 2024
@engcom-Hotel engcom-Hotel self-assigned this Apr 18, 2024
Copy link

m2-assistant bot commented Apr 18, 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 @gustavosolomon,

Thanks for the report and collaboration!

We have referred to the redocly on the below URL:

https://developer.adobe.com/commerce/webapi/rest/quick-reference/

According to the documentation of API V1/products we should pass the custom attributes in the below way only:

{
    "custom_attributes": [
        {
            "attribute_code": "manufacturer",
            "value": "test"
        }
    ]
}

Please refer to the below screenshot for reference:
image

Thanks

@engcom-Hotel engcom-Hotel added Issue: needs update Additional information is require, waiting for response and removed Issue: ready for confirmation labels Apr 18, 2024
@m2-community-project m2-community-project bot moved this from Ready for Confirmation to Needs Update in Issue Confirmation and Triage Board Apr 18, 2024
@m2-community-project m2-community-project bot moved this from Ready for Confirmation to Needs Update in Issue Confirmation and Triage Board Apr 18, 2024
@gustavosolomon
Copy link
Author

Hi @engcom-Hotel ! Thanks for your review.

Yes, that seems correct. So let's just stick with this API documentation example.

And that's the one where the described problem happens.

Maybe I could send a PR so you can better visualize where the problem occurs?

@engcom-Hotel
Copy link
Contributor

Sure @gustavosolomon,

We will wait for the PR.

@gustavosolomon
Copy link
Author

gustavosolomon commented Apr 19, 2024

Hey @engcom-Hotel, I sent the PR so you can take a look.

Some points to look at while analyzing the changes:

  1. I changed the way processCustomAttribute function works, because there's no point in return $customAttributeValue inside a array since will not be used ($customAttributeValue it will be processed in the line after) by $this->runCustomAttributePreprocessors($customAttributeCode, $customAttribute);. This function will process the attribute value, and then, in the line after $customAttributeValue = $customAttribute[AttributeValue::VALUE]; will get the processed attribute value.

And since processCustomAttribute is a private function, it won't affect anything else, anyone has implemented a custom version of this class must have been copied the entire processCustomAttribute function and it's call.

Reference (This is from my PR):

$customAttributeCode = $this->processCustomAttributeCode($customAttribute);
$this->runCustomAttributePreprocessors($customAttributeCode, $customAttribute);
$customAttributeValue = $customAttribute[AttributeValue::VALUE];

  1. Where I changed from $key to $attributeCode, is because we are dealing with attribute codes. And I only did this change to a an Interface because I checked no class in Magento is implementing this interface.

  2. There was some doc block space changes, which I don't know would be allowed to pass, so I can reverse this specific change, if necessary.

@m2-community-project m2-community-project bot added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Apr 23, 2024
@engcom-Hotel
Copy link
Contributor

Thanks @gustavosolomon for the PR,

I am marking this issue as a feature request, also suggesting to check the BIC failures on the below link for your PR and fix those:

https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/38635/78b4d9e7f55cef44089ce796e055a682/SemanticVersionChecker/report-magento2.html

Thanks

@engcom-Hotel engcom-Hotel added feature request and removed Issue: needs update Additional information is require, waiting for response labels Apr 24, 2024
@m2-community-project m2-community-project bot added this to Pull Request in Progress in Feature Requests Backlog Apr 24, 2024
@m2-community-project m2-community-project bot moved this from Pull Request in Progress to In Progress in Feature Requests Backlog Apr 24, 2024
@gustavosolomon
Copy link
Author

Hi @engcom-Hotel, thanks for the update.

I just sent another PR, which should be good to go.

Can you please take a look? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR in progress Reported on 2.4.x Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Feature Requests Backlog
  
Pull Request in Progress
3 participants