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

Error When try to remove the last OrderItem of cart by Api #15983

Open
johnnyNiji opened this issue Mar 11, 2024 · 1 comment
Open

Error When try to remove the last OrderItem of cart by Api #15983

johnnyNiji opened this issue Mar 11, 2024 · 1 comment
Assignees
Labels
API APIs related issues and PRs. Potential Bug Potential bugs or bugfixes, that needs to be reproduced.

Comments

@johnnyNiji
Copy link

johnnyNiji commented Mar 11, 2024

Sylius version affected: 1.12.13

Description
When We try to delete the last orderItem from a cart by Api
DELETE /shop/orders/{tokenValue}/items/{itemId}
We have an error

"@context": "/api/v2/contexts/Error", "@type": "hydra:Error", "hydra:title": "An error occurred", "hydra:description": "Expected an instance of Sylius\\Component\\Core\\Model\\OrderInterface. Got: NULL",

Steps to reproduce

example request :
curl --location --request DELETE 'https://sylius.localhost/api/v2/shop/orders/S-WB-2Kdkb/items/53'

In Sylius\Component\Core\Model\Order.php

when we remove the last orderItem from a cart , we pass into the method

`public function removePayment(BasePaymentInterface $payment): void
{
/** @var PaymentInterface $payment */
Assert::isInstanceOf($payment, PaymentInterface::class);

    if ($this->hasPayment($payment)) {
        $this->payments->removeElement($payment);
        $payment->setOrder(null);
    }
}`

where the order of payment object is set to null,

After that, following the processing of the request, an onflush event is launched and read by vendor/api-platform/core/src/Doctrine/EventListener/PurgeHttpCacheListener.php

who accesses the method

`public function getOrder(): ?BaseOrderInterface
{
Assert::isInstanceOf($this->order, OrderInterface::class);

    return $this->order;
}`

from the Sylius\Component\Core\Model\Payment.php

Possible Solution

As we can see, the method signature can return null, but the Assert::isInstanceOf prohibits us from doing so.

a possible solution would be to modify

`public function getOrder(): ?BaseOrderInterface
{
Assert::isInstanceOf($this->order, OrderInterface::class);

    return $this->order;
}`

by

`public function getOrder(): ?BaseOrderInterface
{
Assert::nullOrIsInstanceOf($this->order, OrderInterface::class);

    return $this->order;
}`
@TheMilek
Copy link
Member

Hey @johnnyNiji! 🚀

Tried to reproduce it on 1.12 and 1.13 version and it seems to work fine
Could you provide more information about order? Maybe the state of the order that you want to remove the item from

Cheers! 🍻

@TheMilek TheMilek added the Potential Bug Potential bugs or bugfixes, that needs to be reproduced. label Mar 13, 2024
@Wojdylak Wojdylak added the API APIs related issues and PRs. label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API APIs related issues and PRs. Potential Bug Potential bugs or bugfixes, that needs to be reproduced.
Projects
None yet
Development

No branches or pull requests

3 participants