diff --git a/src/AmazonPHP/SellingPartner/ObjectSerializer.php b/src/AmazonPHP/SellingPartner/ObjectSerializer.php index a549786b6..b8bed56e7 100644 --- a/src/AmazonPHP/SellingPartner/ObjectSerializer.php +++ b/src/AmazonPHP/SellingPartner/ObjectSerializer.php @@ -2,6 +2,8 @@ namespace AmazonPHP\SellingPartner; +use AmazonPHP\SellingPartner\Model\MerchantFulfillment\LabelFormat; + final class ObjectSerializer { private static string $dateTimeFormat = \DateTimeInterface::ATOM; @@ -374,11 +376,29 @@ public static function deserialize(Configuration $configuration, $data, string $ } if (isset($data->{$instance::attributeMap()[$property]})) { - $propertyValue = $data->{$instance::attributeMap()[$property]}; + $propertyValue = self::castEmptyStringToNull($data->{$instance::attributeMap()[$property]}, $type); + $instance->{$propertySetter}(self::deserialize($configuration, $propertyValue, $type, null)); } } return $instance; } + + /** + * @note amazon returns empty string here, which is not a valid enum value + * https://github.com/amzn/selling-partner-api-models/issues/226#issuecomment-1075640380 + * + * @param null|array|string $value value that needs to be parsed + * + * @return null|array|string parsed object property + */ + private static function castEmptyStringToNull($value, string $type) + { + if ('' === $value && \is_a(LabelFormat::class, $type, true)) { + $value = null; + } + + return $value; + } } diff --git a/tests/AmazonPHP/SellingPartner/Tests/Unit/ObjectSerializerTest.php b/tests/AmazonPHP/SellingPartner/Tests/Unit/ObjectSerializerTest.php index 64aaeb453..8e208cf9c 100644 --- a/tests/AmazonPHP/SellingPartner/Tests/Unit/ObjectSerializerTest.php +++ b/tests/AmazonPHP/SellingPartner/Tests/Unit/ObjectSerializerTest.php @@ -14,6 +14,7 @@ use AmazonPHP\SellingPartner\Model\FulfillmentInbound\UnitOfMeasurement; use AmazonPHP\SellingPartner\Model\FulfillmentInbound\UnitOfWeight; use AmazonPHP\SellingPartner\Model\FulfillmentInbound\Weight; +use AmazonPHP\SellingPartner\Model\MerchantFulfillment\ShippingServiceOptions; use AmazonPHP\SellingPartner\ObjectSerializer; use PHPUnit\Framework\TestCase; @@ -88,4 +89,24 @@ public function test_serialization_of_object_with_enums(): void ObjectSerializer::deserialize($config, $jsonObject, PutTransportDetailsRequest::class) ); } + + public function test_deserialize_label_format_enum_with_an_empty_string(): void + { + $response = <<assertInstanceOf(ShippingServiceOptions::class, $object); + $this->assertNull($object->getLabelFormat()); + } +} \ No newline at end of file