Skip to content

Commit e11306c

Browse files
authored
Fix unmarshalling of ScheduleSpec with null jitter (#563)
* Fix unmarshalling of ScheduleSpec with null jitter Search attributes command: force objects in JSON * Add tests
1 parent e70ae3b commit e11306c

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

src/Internal/Marshaller/Type/DurationJsonType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public function parse($value, $current): CarbonInterval
7676
{
7777
if (\is_array($value) && isset($value['seconds']) && isset($value['nanos'])) {
7878
// The highest precision is milliseconds either way.
79-
$value = $value['seconds'] * 1_000_000_000 + $value['nanos'];
80-
return DateInterval::parse($value, DateInterval::FORMAT_NANOSECONDS);
79+
$value = $value['seconds'] * 1_000_000 + (int) \round($value['nanos'] / 1000);
80+
return DateInterval::parse($value, DateInterval::FORMAT_MICROSECONDS);
81+
}
82+
83+
if ($value === null) {
84+
return CarbonInterval::create();
8185
}
8286

8387
return DateInterval::parse($value, $this->fallbackFormat);

src/Internal/Transport/Request/UpsertSearchAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class UpsertSearchAttributes extends Request
1616
public function __construct(
1717
private readonly array $searchAttributes,
1818
) {
19-
parent::__construct(self::NAME, ['searchAttributes' => $searchAttributes]);
19+
parent::__construct(self::NAME, ['searchAttributes' => (object) $searchAttributes]);
2020
}
2121

2222
/**

src/Internal/Transport/Request/UpsertTypedSearchAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class UpsertTypedSearchAttributes extends Request
1818
public function __construct(
1919
private readonly array $searchAttributes,
2020
) {
21-
parent::__construct(self::NAME, ['search_attributes' => $this->prepareSearchAttributes()]);
21+
parent::__construct(self::NAME, ['search_attributes' => (object) $this->prepareSearchAttributes()]);
2222
}
2323

2424
/**

tests/Unit/DTO/Type/DurationJsonType/DurationJsonTestCase.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,25 @@ class DurationJsonTestCase extends AbstractDTOMarshalling
2121
public function testMarshalAndUnmarshalDuration(): void
2222
{
2323
$dto = new DurationJsonDto();
24-
$dto->duration = DateInterval::parse(1);
24+
$dto->duration = DateInterval::parse(100);
25+
$dto->durationProto = DateInterval::parse(12000);
2526

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

2930
self::assertInstanceOf(\DateInterval::class, $unmarshal->duration);
31+
self::assertInstanceOf(\DateInterval::class, $unmarshal->durationProto);
32+
self::assertSame('0 100000', $unmarshal->duration->format('%s %f'));
33+
self::assertSame('12 0', $unmarshal->durationProto->format('%s %f'));
34+
}
35+
36+
public function testUnmarshallEmptyDuration(): void
37+
{
38+
$result = ['duration' => null, 'duration_proto' => null];
39+
$unmarshal = $this->unmarshal($result, new DurationJsonDto());
40+
41+
self::assertSame('0.0', $unmarshal->duration->format('%s.%f'));
42+
self::assertSame('0.0', $unmarshal->durationProto->format('%s.%f'));
3043
}
3144

3245
protected function getTypeMatchers(): array

tests/Unit/DTO/Type/DurationJsonType/Stub/DurationJsonDto.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111

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

14+
use Google\Protobuf\Duration;
15+
use Temporal\Internal\Marshaller\Meta\Marshal;
16+
1417
class DurationJsonDto
1518
{
1619
public \DateInterval $duration;
20+
21+
#[Marshal('duration_proto', of: Duration::class)]
22+
public \DateInterval $durationProto;
1723
}

0 commit comments

Comments
 (0)