Skip to content

Class as request type for message queue with camel-case properties in constructor not instantiated properly #39217

Open
@smartexcan

Description

@smartexcan

Summary

Using a class for the message queue request type, and having the properties in the constructor using camel-case names, the object cannot be instantiated properly as the object data keys are converted to snake-case.

Tested/reporting on 2.4.6-p6, though i don't think it has changed in a while.

Examples

<?php
namespace \Test\Module\Model;

class SyncRequestData
{
    /**
     * @var int
     */
    private $storeId;

    /**
     * @var int[]
     */
    private $sessionIds;

    /**
     * @var int
     */
    private $websiteStoreId;

    /**
     * @param int $storeId
     * @param int[] $sessionIds
     * @param int $websiteStoreId
     */
    public function __construct(
        int $storeId,
        array $sessionIds,
        int $websiteStoreId
    ) {
        $this->storeId = $storeId;
        $this->sessionIds = $sessionIds;
        $this->websiteStoreId = $websiteStoreId;
    }

    /**
     * @return int
     */
    public function getStoreId(): int
    {
        return $this->storeId;
    }

    /**
     * @return int[]
     */
    public function getSessionIds(): array
    {
        return $this->sessionIds;
    }

    /**
     * @return int
     */
    public function getWebsiteStoreId(): int
    {
        return $this->websiteStoreId;
    }
}

(Magento\Framework\MessageQueue\MessageEncoder calls the service input and output processors)

Magento\Framework\Webapi\ServiceOutputProcessor::convertValue converts the class to the following:

{"store_id":3,"session_ids":[1],"website_store_id":1}

Then in \Magento\Framework\Webapi\ServiceInputProcessor::getConstructorData, the keys do not match since they were converted to snake-case, but they are expected in camel-case, so an empty array is passed to objectmanager to create the object, resulting in an error due to not passing any arguments to constructor.

The only way I can currently get around this issue is having the class extend AbstractSimpleObject or DataObject and adding setters (or using the data constructor argument)

Proposed solution

No response

Release note

No response

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: FrameworkComponent: MessageQueueIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: ready for devReported on 2.4.xIndicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions