Skip to content

Adding the ability to have nested objects that are not DataObject #36689

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

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

MeCapron
Copy link
Contributor

@MeCapron MeCapron commented Dec 30, 2022

Description (*)

This PR will allow other objects that do not extend the \Magento\Framework\DataObject class to be instantiated through this system. For the moment, you cannot instantiate any external Magento objects because the ObjectManager will try to set the first argument named data even if there is no arguments possible

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes External DTOs should be usable as part of ExtensionAttributes system #34989

Manual testing scenarios (*)

** You can find the repository of the module describe just here : https://github.com/MeCapron/34989_magento2**

  1. Declare an extension attribute for the customer
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="loyaltyCards" type="Module\DataObject\Model\LoyaltyCard[]"/>
    </extension_attributes>
</config>
  1. Define the class value, and its subchild :

The class LoyaltyCard

<?php

declare(strict_types=1);

namespace Module\DataObject\Model;

class LoyaltyCard
{
    private string $label;

    /**
     * @var \Module\DataObject\Model\Sub\Card[]
     */
    private array $cards;

    /**
     * @return string
     */
    public function getLabel(): string
    {
        return $this->label;
    }

    /**
     * @param string $label
     *
     * @return void
     */
    public function setLabel(string $label): void
    {
        $this->label = $label;
    }

    /**
     * @return \Module\DataObject\Model\Sub\Card[]
     */
    public function getCards(): array
    {
        return $this->cards;
    }

    /**
     * @param \Module\DataObject\Model\Sub\Card[] $cards
     *
     * @return void
     */
    public function setCards(array $cards): void
    {
        $this->cards = $cards;
    }
}

The card class

<?php

declare(strict_types=1);

namespace Module\DataObject\Model\Sub;

class Card
{
    private \Module\DataObject\Model\Sub\Nested\CardData $cardData;

    /**
     * @return \Module\DataObject\Model\Sub\Nested\CardData
     */
    public function getCardData(): \Module\DataObject\Model\Sub\Nested\CardData
    {
        return $this->cardData;
    }

    /**
     * @param \Module\DataObject\Model\Sub\Nested\CardData $cardData
     *
     * @return void
     */
    public function setCardData(\Module\DataObject\Model\Sub\Nested\CardData $cardData): void
    {
        $this->cardData = $cardData;
    }
}

The CardData class

<?php

declare(strict_types=1);

namespace Module\DataObject\Model\Sub\Nested;

class CardData
{
    private ?int $cardNumber = null;

    /**
     * @return int|null
     */
    public function getCardNumber(): ?int
    {
        return $this->cardNumber;
    }

    /**
     * @param int $cardNumber
     *
     * @return void
     */
    public function setCardNumber(int $cardNumber): void
    {
        $this->cardNumber = $cardNumber;
    }
}

Then create an observer to make your tests easier :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch">
        <observer name="test_dataobject" instance="Module\DataObject\Observer\TestDataObjectNestedObject"/>
    </event>
</config>

The observer

<?php

declare(strict_types=1);

namespace Module\DataObject\Observer;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Module\DataObject\Model\LoyaltyCard;
use Module\DataObject\Model\Sub\Card;

class TestDataObjectNestedObject implements ObserverInterface
{
    private DataObjectHelper $dataObjectHelper;

    private CustomerInterfaceFactory $customerFactory;

    public function __construct(
        DataObjectHelper $dataObjectHelper,
        CustomerInterfaceFactory $customerFactory
    ) {
        $this->dataObjectHelper = $dataObjectHelper;
        $this->customerFactory = $customerFactory;
    }

    public function execute(Observer $observer): void
    {
        $customer = $this->customerFactory->create();

        $this->dataObjectHelper->populateWithArray(
            $customer,
            [
                'extension_attributes' => [
                    'loyaltyCards' => [
                        [
                            'label' => 'My first card',
                            'cards' => [
                                [
                                    'card_data' => [
                                        'card_number' => 157
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            CustomerInterface::class
        );

        /** @var LoyaltyCard $loyaltyCard */
        $loyaltyCard = current($customer->getExtensionAttributes()->getLoyaltyCards());

        /** @var Card $innerCard */
        $innerCard = current($loyaltyCard->getCards());

        echo sprintf('The card number is %d', $innerCard->getCardData()->getCardNumber());
    }
}

As you see, even though the card number has been defined to 157 its value will be null or 0.

This is because of the way nested objects are created :

$object = $this->objectFactory->create($returnType, $value);

They are created supposing that they have the values as arguments (in the constructor) which is not true every time.

After setting this fix, the value of the card number is now displayed 157. Because the current object we are trying to create is not a Magento\Framework\DataObject we will just instantiate it through the objectmanager and then we will fill this object with the same method.

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Dec 30, 2022

Hi @MeCapron. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests 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 Pull Requests 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.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@MeCapron
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch 2 times, most recently from 15c09eb to 50c2042 Compare December 30, 2022 20:59
@MeCapron
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch from 50c2042 to 0a1c832 Compare January 1, 2023 20:52
@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 1, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch from 0a1c832 to fc76f9d Compare January 1, 2023 21:35
@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 1, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch 2 times, most recently from a4f2e7d to 3977059 Compare January 1, 2023 22:51
@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 1, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch from 3977059 to 71bc462 Compare January 2, 2023 00:09
@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 2, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 2, 2023

@magento run Integration Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 2, 2023

@magento run Functional Tests CE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jan 2, 2023

@magento run Functional Tests CE

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Hotel
Copy link
Contributor

We are closing this issue as we have no updates on its related issue. Please refer the below comment:

#34989 (comment)

@MeCapron
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@MeCapron
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

Pull Requests are not mergeable to the mainline. Please merge the latest mainlines to your Pull Requests and restart the builds.

@Den4ik
Copy link
Contributor

Den4ik commented Jun 29, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch 2 times, most recently from 71bc462 to b7da2ec Compare July 3, 2023 13:50
@MeCapron MeCapron force-pushed the feature/allow_nested_objects_from_dto_2 branch from b7da2ec to e9ce4f4 Compare July 3, 2023 13:52
@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 3, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 4, 2023

@magento run Functional Tests B2B, WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 4, 2023

@magento run WebAPI Tests, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 4, 2023

@magento run WebAPI Tests, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 5, 2023

@magento run WebAPI Tests, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 5, 2023

@magento run Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@MeCapron
Copy link
Contributor Author

MeCapron commented Jul 6, 2023

@magento run WebAPI Tests, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@Den4ik
Copy link
Contributor

Den4ik commented Nov 17, 2023

@magento run all tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: review
Projects
Status: Review in Progress
Development

Successfully merging this pull request may close these issues.

External DTOs should be usable as part of ExtensionAttributes system
3 participants