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

[Serializer] ensure ChainEncoder/ChainDecoder properly consider the context #43231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions 12 src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ public function supportsDecoding($format, array $context = []): bool
*
* @throws RuntimeException if no decoder is found
*/
private function getDecoder(string $format, array $context): DecoderInterface
public function getDecoder(string $format, array $context): DecoderInterface
Guite marked this conversation as resolved.
Show resolved Hide resolved
{
if (isset($this->decoderByFormat[$format])
$hasContext = 0 < \count($context);
if (
true !== $hasContext
Guite marked this conversation as resolved.
Show resolved Hide resolved
&& isset($this->decoderByFormat[$format])
&& isset($this->decoders[$this->decoderByFormat[$format]])
) {
return $this->decoders[$this->decoderByFormat[$format]];
}

foreach ($this->decoders as $i => $decoder) {
if ($decoder->supportsDecoding($format, $context)) {
$this->decoderByFormat[$format] = $i;
if (true !== $hasContext) {
// cache decoder if no dynamic context is given
$this->decoderByFormat[$format] = $i;
}

return $decoder;
}
Expand Down
12 changes: 9 additions & 3 deletions 12 src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,23 @@ public function needsNormalization(string $format, array $context = []): bool
*
* @throws RuntimeException if no encoder is found
*/
private function getEncoder(string $format, array $context): EncoderInterface
public function getEncoder(string $format, array $context): EncoderInterface
{
if (isset($this->encoderByFormat[$format])
$hasContext = 0 < \count($context);
if (
true !== $hasContext
&& isset($this->encoderByFormat[$format])
&& isset($this->encoders[$this->encoderByFormat[$format]])
) {
return $this->encoders[$this->encoderByFormat[$format]];
}

foreach ($this->encoders as $i => $encoder) {
if ($encoder->supportsEncoding($format, $context)) {
$this->encoderByFormat[$format] = $i;
if (true !== $hasContext) {
// cache encoder if no dynamic context is given
$this->encoderByFormat[$format] = $i;
}

return $encoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function setUp(): void
[self::FORMAT_2, [], false],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], true],
[self::FORMAT_3, ['foo' => 'bar2'], false],
]);

$this->decoder2 = $this->createMock(DecoderInterface::class);
Expand All @@ -45,6 +46,8 @@ protected function setUp(): void
[self::FORMAT_1, [], false],
[self::FORMAT_2, [], true],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], false],
[self::FORMAT_3, ['foo' => 'bar2'], true],
]);

$this->chainDecoder = new ChainDecoder([$this->decoder1, $this->decoder2]);
Expand All @@ -53,9 +56,18 @@ protected function setUp(): void
public function testSupportsDecoding()
{
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
$this->assertSame($this->decoder1, $this->chainDecoder->getDecoder(self::FORMAT_1, []));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
$this->assertSame($this->decoder2, $this->chainDecoder->getDecoder(self::FORMAT_2, []));

$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
$this->assertSame($this->decoder1, $this->chainDecoder->getDecoder(self::FORMAT_3, ['foo' => 'bar']));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
$this->assertSame($this->decoder2, $this->chainDecoder->getDecoder(self::FORMAT_3, ['foo' => 'bar2']));
}

public function testDecode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function setUp(): void
[self::FORMAT_2, [], false],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], true],
[self::FORMAT_3, ['foo' => 'bar2'], false],
]);

$this->encoder2 = $this->createMock(EncoderInterface::class);
Expand All @@ -46,6 +47,8 @@ protected function setUp(): void
[self::FORMAT_1, [], false],
[self::FORMAT_2, [], true],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], false],
[self::FORMAT_3, ['foo' => 'bar2'], true],
]);

$this->chainEncoder = new ChainEncoder([$this->encoder1, $this->encoder2]);
Expand All @@ -54,9 +57,18 @@ protected function setUp(): void
public function testSupportsEncoding()
{
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
$this->assertSame($this->encoder1, $this->chainEncoder->getEncoder(self::FORMAT_1, []));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
$this->assertSame($this->encoder2, $this->chainEncoder->getEncoder(self::FORMAT_2, []));

$this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar']));
$this->assertSame($this->encoder1, $this->chainEncoder->getEncoder(self::FORMAT_3, ['foo' => 'bar']));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar2']));
$this->assertSame($this->encoder2, $this->chainEncoder->getEncoder(self::FORMAT_3, ['foo' => 'bar2']));
}

public function testEncode()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.