Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 277486a

Browse filesBrowse files
committed
feature #59679 [Scheduler] Normalize TriggerInterface as string (valtzu)
This PR was merged into the 7.3 branch. Discussion ---------- [Scheduler] Normalize `TriggerInterface` as `string` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | yes | Deprecations? | no | Issues | Fix #53562 | License | MIT Fix the issue with Scheduler & Serializer like `@kbond` described in #53562 (comment). Fixes the following error: ``` Could not decode stamp: The type of the "trigger" attribute for class "Symfony\Component\Scheduler\Generator\MessageContext" must be one of "Symfony\Component\Scheduler\Trigger\TriggerInterface" ("array" given). ``` Commits ------- 3aa47e0 Normalize `TriggerInterface` as `string`
2 parents a1126e7 + 3aa47e0 commit 277486a
Copy full SHA for 277486a

File tree

4 files changed

+158
-0
lines changed
Filter options

4 files changed

+158
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/scheduler.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/scheduler.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Scheduler\EventListener\DispatchSchedulerEventListener;
1515
use Symfony\Component\Scheduler\Messenger\SchedulerTransportFactory;
16+
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
1617
use Symfony\Component\Scheduler\Messenger\ServiceCallMessageHandler;
1718

1819
return static function (ContainerConfigurator $container) {
@@ -34,5 +35,7 @@
3435
service('event_dispatcher'),
3536
])
3637
->tag('kernel.event_subscriber')
38+
->set('serializer.normalizer.scheduler_trigger', SchedulerTriggerNormalizer::class)
39+
->tag('serializer.normalizer', ['built_in' => true, 'priority' => -880])
3740
;
3841
};

‎src/Symfony/Component/Scheduler/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Scheduler/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `TriggerNormalizer`
8+
49
7.2
510
---
611

+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Messenger\Serializer\Normalizer;
13+
14+
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
15+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
16+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
17+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18+
19+
final class SchedulerTriggerNormalizer implements DenormalizerInterface, NormalizerInterface
20+
{
21+
public function getSupportedTypes(?string $format): array
22+
{
23+
return [
24+
TriggerInterface::class => false,
25+
];
26+
}
27+
28+
/**
29+
* @param TriggerInterface $data
30+
*/
31+
public function normalize(mixed $data, ?string $format = null, array $context = []): string
32+
{
33+
return (string) $data;
34+
}
35+
36+
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
37+
{
38+
return $data instanceof TriggerInterface && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false);
39+
}
40+
41+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): TriggerInterface
42+
{
43+
return new class($data) implements TriggerInterface {
44+
public function __construct(private readonly string $description)
45+
{
46+
}
47+
48+
public function __toString(): string
49+
{
50+
return $this->description;
51+
}
52+
53+
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
54+
{
55+
throw new \LogicException('Not possible to get next run date from a deserialized trigger.');
56+
}
57+
};
58+
}
59+
60+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
61+
{
62+
return TriggerInterface::class === $type && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false) && \is_string($data);
63+
}
64+
}
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests\Messenger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\Messenger\Serializer\Normalizer\SchedulerTriggerNormalizer;
16+
use Symfony\Component\Scheduler\Trigger\CallbackTrigger;
17+
use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger;
18+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
19+
20+
class SchedulerTriggerNormalizerTest extends TestCase
21+
{
22+
private SchedulerTriggerNormalizer $normalizer;
23+
24+
/**
25+
* @before
26+
*/
27+
protected function setUpNormalizer(): void
28+
{
29+
$this->normalizer = new SchedulerTriggerNormalizer();
30+
}
31+
32+
/**
33+
* @dataProvider normalizeProvider
34+
*/
35+
public function testNormalize(mixed $data, mixed $expected)
36+
{
37+
self::assertSame($expected, $this->normalizer->normalize($data));
38+
}
39+
40+
public static function normalizeProvider(): iterable
41+
{
42+
yield 'CallbackTrigger' => [new CallbackTrigger(fn () => null, 'test1'), 'test1'];
43+
yield 'PeriodicalTrigger' => [new PeriodicalTrigger(5), 'every 5 seconds'];
44+
}
45+
46+
/**
47+
* @dataProvider supportsNormalizationProvider
48+
*/
49+
public function testSupportsNormalization(mixed $data, array $context, bool $expected)
50+
{
51+
self::assertSame($expected, $this->normalizer->supportsNormalization($data, 'json', $context));
52+
}
53+
54+
public static function supportsNormalizationProvider(): iterable
55+
{
56+
yield 'CallbackTrigger, messenger context' => [new CallbackTrigger(fn () => null, 'test1'), ['messenger_serialization' => true], true];
57+
yield 'CallbackTrigger, normal context' => [new CallbackTrigger(fn () => null, 'test1'), [], false];
58+
yield 'PeriodicalTrigger, messenger context' => [new PeriodicalTrigger(5), ['messenger_serialization' => true], true];
59+
yield 'PeriodicalTrigger, normal context' => [new PeriodicalTrigger(5), [], false];
60+
yield 'stdClass, messenger context' => [new \stdClass(), ['messenger_serialization' => true], false];
61+
yield 'stdClass, normal context' => [new \stdClass(), [], false];
62+
}
63+
64+
/**
65+
* @dataProvider supportsDenormalizationProvider
66+
*/
67+
public function testSupportsDenormalization(mixed $data, string $type, array $context, bool $expected)
68+
{
69+
self::assertSame($expected, $this->normalizer->supportsDenormalization($data, $type, 'json', $context));
70+
}
71+
72+
public static function supportsDenormalizationProvider(): iterable
73+
{
74+
yield 'unknown type' => ['test', \stdClass::class, ['messenger_serialization' => true], false];
75+
yield 'string, messenger context' => ['test', TriggerInterface::class, ['messenger_serialization' => true], true];
76+
yield 'string, normal context' => ['test', TriggerInterface::class, [], false];
77+
yield 'array, messenger context' => [['a' => 'b'], TriggerInterface::class, ['messenger_serialization' => true], false];
78+
yield 'array, normal context' => [['a' => 'b'], TriggerInterface::class, [], false];
79+
}
80+
81+
public function testDenormalize()
82+
{
83+
$trigger = $this->normalizer->denormalize('every 5 seconds', TriggerInterface::class);
84+
self::assertSame('every 5 seconds', (string) $trigger);
85+
}
86+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.