Skip to content

Commit af31ef1

Browse files
[Fix] Do not dispatch a post write event when data is already a response (#1057)
| Q | A | --------------- | ----- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT When data returned by the write processor is already a response, we does not have to dispatch a post write event. <img width="914" height="267" alt="image" src="https://github.com/user-attachments/assets/5a949988-3571-498d-b8cd-4a8bf0361124" />
2 parents 3a0f21b + 78af13b commit af31ef1

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/Component/src/Metadata/ResourceMetadata.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function __construct(
2828
private ?string $pluralName = null,
2929
private ?string $applicationName = null,
3030
private ?string $identifier = null,
31-
protected ?array $normalizationContext = null,
32-
protected ?array $denormalizationContext = null,
33-
protected ?array $validationContext = null,
31+
private ?array $normalizationContext = null,
32+
private ?array $denormalizationContext = null,
33+
private ?array $validationContext = null,
3434
private ?string $class = null,
3535
private string|false|null $driver = null,
3636
private ?array $vars = null,

src/Component/src/Symfony/EventDispatcher/State/DispatchPostWriteEventProcessor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sylius\Resource\State\ProcessorInterface;
1919
use Sylius\Resource\Symfony\EventDispatcher\OperationEventDispatcherInterface;
2020
use Sylius\Resource\Symfony\EventDispatcher\OperationEventHandlerInterface;
21+
use Symfony\Component\HttpFoundation\Response;
2122

2223
/**
2324
* @experimental
@@ -38,6 +39,10 @@ public function process(mixed $data, Operation $operation, Context $context): mi
3839
{
3940
$data = $this->processor->process($data, $operation, $context);
4041

42+
if ($data instanceof Response) {
43+
return $data;
44+
}
45+
4146
$operationEvent = $this->operationEventDispatcher->dispatchPostEvent($data, $operation, $context);
4247

4348
$eventResponse = $this->eventHandler->handlePostProcessEvent(

src/Component/src/Symfony/ExpressionLanguage/ArgumentParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class ArgumentParser implements ArgumentParserInterface
2525
public function __construct(
2626
private readonly ExpressionLanguage $expressionLanguage,
2727
private readonly VariablesCollectionInterface $variablesCollection,
28-
readonly ?iterable $providers = null,
28+
?iterable $providers = null,
2929
) {
3030
foreach ($providers ?? [] as $provider) {
3131
Assert::isInstanceOf($provider, ExpressionFunctionProviderInterface::class);

src/Component/tests/Symfony/EventDispatcher/State/DispatchPostWriteEventProcessorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,25 @@ public function it_returns_post_event_response(): void
9191
$result = $this->dispatchPostWriteEventProcessor->process($data, $operation, $context);
9292
$this->assertEquals($response, $result);
9393
}
94+
95+
/** @test */
96+
public function it_does_nothing_if_the_decorated_processor_returns_a_response(): void
97+
{
98+
$data = new \stdClass();
99+
100+
$operation = new Create(processor: '\App\Processor');
101+
$context = new Context();
102+
103+
$response = new Response();
104+
105+
$this->processor->process($data, $operation, $context)->willReturn($response)->shouldBeCalled();
106+
107+
$postEvent = new OperationEvent();
108+
109+
$this->operationEventDispatcher->dispatchPostEvent($response, $operation, $context)->willReturn($postEvent)->shouldNotBeCalled();
110+
111+
$this->eventHandler->handlePostProcessEvent($postEvent, $context)->willReturn(null)->shouldNotBeCalled();
112+
113+
$this->dispatchPostWriteEventProcessor->process($data, $operation, $context);
114+
}
94115
}

0 commit comments

Comments
 (0)