Skip to content

Commit

Permalink
🐛 avoid read-only relationships in POST requests
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tini committed Dec 16, 2024
1 parent a1e130b commit af771b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public function resolveDynamicServiceRates(
ShipmentInterface|array $shipmentData,
?ServiceRateInterface $dynamicServiceRate = null,
): array {
$data = ($shipmentData instanceof ShipmentInterface) ? $shipmentData->jsonSerialize() : $shipmentData;
$data = ($shipmentData instanceof ShipmentInterface) ? $shipmentData->getData() : $shipmentData;

if (!isset($data['relationships'])) {
$data['relationships'] = [];
Expand Down Expand Up @@ -584,7 +584,7 @@ public function createAndRegisterShipment(
'/registered-shipments?' . http_build_query(['include' => Shipment::RELATIONSHIP_FILES]),
'post',
[
'data' => $shipment,
'data' => $shipment->getData(),
'meta' => array_filter($shipment->getMeta()),
],
$this->authenticator->getAuthorizationHeader() + [
Expand Down Expand Up @@ -648,7 +648,7 @@ public function createAndRegisterMultiColliShipment(
]),
'post',
[
'data' => $shipment,
'data' => $shipment->getData(),
'meta' => array_merge(
[
'colli' => array_map(
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/Proxy/ShipmentProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ShipmentProxy implements ShipmentInterface, ResourceProxyInterface

private string $type = ResourceInterface::TYPE_SHIPMENT;

public function getData(): array
{
return $this->getResource()->getData();
}

public function getMeta(): array
{
return $this->getResource()->getMeta();
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ class Shipment implements ShipmentInterface
/** @var callable */
private $statusHistoryCallback = null;

/**
* Prepare the data for a request to our API. This filters the read-only relationships to avoid validation errors.
*/
public function getData(): array
{
$data = $this->jsonSerialize();

// Remove read-only relationships.
unset($data['relationships']['colli']);
unset($data['relationships']['files']);
unset($data['relationships']['shipment_status']);
unset($data['relationships']['shipment_surcharges']);

return $data;
}

public function getMeta(): array
{
return $this->meta;
Expand Down

0 comments on commit af771b1

Please sign in to comment.