Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed empty string value for LabelFormat enum property #158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/AmazonPHP/SellingPartner/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmazonPHP\SellingPartner;

use AmazonPHP\SellingPartner\Model\MerchantFulfillment\LabelFormat;

final class ObjectSerializer
{
private static string $dateTimeFormat = \DateTimeInterface::ATOM;
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions tests/AmazonPHP/SellingPartner/Tests/Unit/ObjectSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = <<<JSON
{
"DeliveryExperience": "DeliveryConfirmationWithoutSignature",
"CarrierWillPickUp": false,
"LabelFormat": ""
}
JSON;

$object = ObjectSerializer::deserialize(
Configuration::forIAMUser('clientId', 'clientSecret', 'accessKey', 'secretKey'),
$response,
ShippingServiceOptions::class
);

$this->assertInstanceOf(ShippingServiceOptions::class, $object);
$this->assertNull($object->getLabelFormat());
}
}