Skip to content

Commit

Permalink
Fix unmarshalling of ScheduleSpec with null jitter (#563)
Browse files Browse the repository at this point in the history
* Fix unmarshalling of ScheduleSpec with null jitter
Search attributes command: force objects in JSON

* Add tests
  • Loading branch information
roxblnfk authored Feb 10, 2025
1 parent e70ae3b commit e11306c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Internal/Marshaller/Type/DurationJsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ public function parse($value, $current): CarbonInterval
{
if (\is_array($value) && isset($value['seconds']) && isset($value['nanos'])) {
// The highest precision is milliseconds either way.
$value = $value['seconds'] * 1_000_000_000 + $value['nanos'];
return DateInterval::parse($value, DateInterval::FORMAT_NANOSECONDS);
$value = $value['seconds'] * 1_000_000 + (int) \round($value['nanos'] / 1000);
return DateInterval::parse($value, DateInterval::FORMAT_MICROSECONDS);
}

if ($value === null) {
return CarbonInterval::create();
}

return DateInterval::parse($value, $this->fallbackFormat);
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Transport/Request/UpsertSearchAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class UpsertSearchAttributes extends Request
public function __construct(
private readonly array $searchAttributes,
) {
parent::__construct(self::NAME, ['searchAttributes' => $searchAttributes]);
parent::__construct(self::NAME, ['searchAttributes' => (object) $searchAttributes]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class UpsertTypedSearchAttributes extends Request
public function __construct(
private readonly array $searchAttributes,
) {
parent::__construct(self::NAME, ['search_attributes' => $this->prepareSearchAttributes()]);
parent::__construct(self::NAME, ['search_attributes' => (object) $this->prepareSearchAttributes()]);
}

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/Unit/DTO/Type/DurationJsonType/DurationJsonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ class DurationJsonTestCase extends AbstractDTOMarshalling
public function testMarshalAndUnmarshalDuration(): void
{
$dto = new DurationJsonDto();
$dto->duration = DateInterval::parse(1);
$dto->duration = DateInterval::parse(100);
$dto->durationProto = DateInterval::parse(12000);

$result = $this->marshal($dto);
$unmarshal = $this->unmarshal($result, new DurationJsonDto());

self::assertInstanceOf(\DateInterval::class, $unmarshal->duration);
self::assertInstanceOf(\DateInterval::class, $unmarshal->durationProto);
self::assertSame('0 100000', $unmarshal->duration->format('%s %f'));
self::assertSame('12 0', $unmarshal->durationProto->format('%s %f'));
}

public function testUnmarshallEmptyDuration(): void
{
$result = ['duration' => null, 'duration_proto' => null];
$unmarshal = $this->unmarshal($result, new DurationJsonDto());

self::assertSame('0.0', $unmarshal->duration->format('%s.%f'));
self::assertSame('0.0', $unmarshal->durationProto->format('%s.%f'));
}

protected function getTypeMatchers(): array
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/DTO/Type/DurationJsonType/Stub/DurationJsonDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

namespace Temporal\Tests\Unit\DTO\Type\DurationJsonType\Stub;

use Google\Protobuf\Duration;
use Temporal\Internal\Marshaller\Meta\Marshal;

class DurationJsonDto
{
public \DateInterval $duration;

#[Marshal('duration_proto', of: Duration::class)]
public \DateInterval $durationProto;
}

0 comments on commit e11306c

Please sign in to comment.