8000 [Serializer] ensure ChainEncoder/ChainDecoder properly consider the context by Guite · Pull Request #43231 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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 1 commit
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
Next Next commit
[Serializer] ensure ChainEncoder/ChainDecoder properly consider the c…
…ontext
  • Loading branch information
Guite committed Sep 28, 2021
commit 87263d4f0df31e9a7d079928666717f6ecf870cf
11 changes: 1 addition & 10 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
class ChainDecoder implements ContextAwareDecoderInterface
{
protected $decoders = [];
protected $decoderByFormat = [ 8000 ];

public function __construct(array $decoders = [])
{
Expand Down Expand Up @@ -59,18 +58,10 @@ 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
{
if (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;

return $decoder;
}
}
Expand Down
11 changes: 1 addition & 10 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
class ChainEncoder implements ContextAwareEncoderInterface
{
protected $encoders = [];
protected $encoderByFormat = [];

public function __construct(array $encoders = [])
{
Expand Down Expand Up @@ -77,18 +76,10 @@ 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])
&& 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;

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
0