jlevers / selling-partner-api

A PHP client library for Amazon's Selling Partner API
BSD 3-Clause "New" or "Revised" License
401 stars 189 forks source link

Issue with ErrorList responses #779

Open 0xSekar opened 1 month ago

0xSekar commented 1 month ago

Problem description:

The commit ec9522aa6b92d9adb05770317d75cbaec78a2d12 fixed an issue with error responses defined as arrays instead of objects, but this change introduced an error in the call to GetListingOffersBatchRequest in ProductPricingV0 API.

In this particular case, The response at GetOffersResponse is an array of errors and not an ErrorList object.

The issue can be reproduced by sending an incorrect SKU or ASIN. I'm not sure yet how to revert the change for this specific response in the generator.

Original Response:

array(1) {
  ["responses"]=>
  array(1) {
    [0]=>
    array(4) {
      ["headers"]=>
      array(2) {
        ["x-amzn-RequestId"]=>
        string(36) "54c21e5e-dfdc-47f5-893a-0cdd3b4217d3"
        ["Date"]=>
        string(29) "Thu, 05 Sep 2024 14:49:21 GMT"
      }
      ["status"]=>
      array(2) {
        ["statusCode"]=>
        int(400)
        ["reasonPhrase"]=>
        string(11) "Bad Request"
      }
      ["body"]=>
      array(1) {
        ["errors"]=>
        array(1) {
          [0]=>
          array(3) {
            ["message"]=>
            string(60) "CCWB2001-FBM is an invalid SKU for marketplace ATVPDKIKX0DER"
            ["details"]=>
            string(0) ""
            ["code"]=>
            string(12) "InvalidInput"
          }
        }
      }
      ["request"]=>
      array(3) {
        ["MarketplaceId"]=>
        string(13) "ATVPDKIKX0DER"
        ["ItemCondition"]=>
        string(3) "New"
        ["SellerSKU"]=>
        string(0) ""
      }
    }
  }
}

Error:

PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function SellingPartnerApi\Seller\ProductPricingV0\Dto\ErrorList::__construct(), 0 passed in /var/www/gorilla/gorilla-cron/vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php on line 76 and exactly 1 expected in /var/www/gorilla/gorilla-cron/vendor/jlevers/selling-partner-api/src/Seller/ProductPricingV0/Dto/ErrorList.php:22
Stack trace:
#0 /var/www/gorilla/gorilla-cron/vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(76): SellingPartnerApi\Seller\ProductPricingV0\Dto\ErrorList->__construct()
#1 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(104): SellingPartnerApi\Dto::deserialize()
#2 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(72): SellingPartnerApi\Response::deserializeValue()
#3 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(104): SellingPartnerApi\Response::deserialize()
#4 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(72): SellingPartnerApi\Dto::deserializeValue()
#5 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(104): SellingPartnerApi\Dto::deserialize()
#6 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(114): SellingPartnerApi\Response::deserializeValue()
#7 vendor/jlevers/selling-partner-api/src/Traits/Deserializes.php(72): SellingPartnerApi\Response::deserializeValue()
#8 vendor/jlevers/selling-partner-api/src/Seller/ProductPricingV0/Requests/GetListingOffersBatch.php(53): SellingPartnerApi\Response::deserialize()
#9 vendor/saloonphp/saloon/src/Http/Response.php(310): SellingPartnerApi\Seller\ProductPricingV0\Requests\GetListingOffersBatch->createDtoFromResponse()
#10 updateProductsLowestPricedOffers_Child.php(169): Saloon\Http\Response->dto()
#11 updateProductsLowestPricedOffers_Child.php(91): process()
#12 {main}
christian71-stack commented 2 days ago

I had the same problem. I then just used ->json() instead of ->dto()

0xSekar commented 1 day ago

Yes, that was my first solution. Then I manually updated the file so I can continue using my internal library (that uses ->dto()), but need to redo the changes on each update, so modifying the generator will be a better solution, just that I'm not sure how to do it. The file /src/Seller/ProductPricingV0/Responses/GetOffersResponse.php should look something like this:


/**
 * This file is auto-generated by Saloon SDK Generator
 * Generator: SellingPartnerApi\Generator\Generators\ResponseGenerator
 * Do not modify it directly.
 */

declare(strict_types=1);

namespace SellingPartnerApi\Seller\ProductPricingV0\Responses;

use SellingPartnerApi\Response;
use SellingPartnerApi\Seller\ProductPricingV0\Dto\Error;
use SellingPartnerApi\Seller\ProductPricingV0\Dto\GetOffersResult;

final class GetOffersResponse extends Response
{
    protected static array $complexArrayTypes = ['errors' => Error::class];

    /**
     * @param  ?ErrorList  $errors  A list of error responses returned when a request is unsuccessful.
     */
    public function __construct(
        public readonly ?GetOffersResult $payload = null,
        public readonly ?array $errors = null,
    ) {}
}